Skip to content

Instantly share code, notes, and snippets.

@Sneppys
Created December 23, 2022 02:46
Show Gist options
  • Save Sneppys/050bf4d156109446838c854152a6f9fb to your computer and use it in GitHub Desktop.
Save Sneppys/050bf4d156109446838c854152a6f9fb to your computer and use it in GitHub Desktop.
Hide Twitter Views userscript
// ==UserScript==
// @name Hide Tweet Views
// @version 0.1
// @description Hide view count in twitter timeline
// @author Snep
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant none
// ==/UserScript==
function hideButtons() {
const tweetButtons = document.querySelectorAll('[data-testid="tweet"] [role="group"]>*:not([style])');
for (const button of tweetButtons) {
for (const child of button.children) {
const attrib = child && child.getAttribute && child.getAttribute("aria-label");
if (attrib && attrib.includes("analytics")) {
button.style = "display: none";
}
}
}
setTimeout(hideButtons, 100);
}
(function() {
'use strict';
hideButtons();
})();
@Emma-Fuller
Copy link

FYI to make the script a bit more efficient, you can run the code inside a mutation observer after the initial run in order to prevent the need to keep running the code on a timeout. something like this:

const observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    if (mutation.addedNodes && mutation.addedNodes.length > 0) {
      // Do stuff when new nodes are added to the dom here
    }
  });
});
observer.observe(document.body, {
  childList: true,
  subtree: true
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment