Skip to content

Commit

Permalink
Support removing trends widgets without cogwheel (#1)
Browse files Browse the repository at this point in the history
Some users are reporting a version of the trends widget without the
cogwheel link. We can try to identify this version (also the other
version, but let’s keep the old code for that) using the “trending
with…” links under some trends, which, if they exist, identify the
source via some URL parameters.
  • Loading branch information
lucaswerkmeister authored Oct 20, 2020
1 parent a914ad8 commit d11a8bf
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions hide-twitter-trends.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* then look at some of the neighboring DOM trying to reduce false positives.
* (The many class names on the elements don’t look stable,
* so we only match on element names, hoping they’re more reliable.)
*
* There is also a version of the Trends widget without the cogwheel.
* We can try to identify this using the “trending with…” links,
* which, if they exist, have &src=trend_click in the URL.
* (The actual trends themselves aren’t links at all, but divs with event handlers.)
* If none of the trends are “trending with” something, we’re out of luck.
*/
function removeTrends() {
for (let trends of document.querySelectorAll('a[href="/settings/trends"]')) {
Expand All @@ -32,6 +38,29 @@ function removeTrends() {
trends = parent;
trends.remove();
}
for (let trends of document.querySelectorAll('a[href*="&src=trend_click"]')) {
if (!document.body.contains(trends)) {
// we already removed the parent trends widget
continue;
}
if (trends.closest('article')) {
// trends link in a tweet
continue;
}
trends = trends.closest('section');
if (!trends) {
// (unclear if this can happen)
continue;
}
if (trends.firstElementChild.tagName !== 'H1') {
// (unclear if this can happen)
continue;
}
let parent;
while ((parent = trends.parentElement).childElementCount == 1)
trends = parent;
trends.remove();
}
}

setTimeout(removeTrends, 3 * 1000);
Expand Down

0 comments on commit d11a8bf

Please sign in to comment.