0

After some googling and looking at tutorials, I have code along these lines:

File: background.js:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete' && tab.active) {

    chrome.tabs.executeScript(tab.ib, {
        file: 'inject.js'
    });
  }
})

File: inject.js

(function() {

    function remove_by_class(className) {
        var elem = document.getElementsByClassName(className)[0];
        elem.parentNode.removeChild(elem);
    }
    
    function remove_by_title(itemTitle) {
        var elem = document.querySelector('[title = itemTitle]');
        elem.parentNode.removeChild(elem);
    }
    
    if (url.includes('SOME_URL_SUBSTRING')){
        remove_by_class('CLASS_NAME');
        remove_by_title('ITEM_TITLE');
    }
    
})();

This removes certain elements, usually a second or two after I see them load in. And if I understand correctly, it can only remove elements from the page as it 'initially' exists, when it is first loaded - elements created later (by future Javascript actions on the page) are unaffected, because my extension's code is only injected & executed once.

What I'm looking for instead is some sort of 'always-on' extension that proactively watches for the loading of certain elements. Basically, I want to have a function which is called every time an HTML element is loaded/created in the page, and only allow the element to actually be placed if the function returns 'true'.

What is the easiest way to accomplish something like this?

EDIT: as an example, say I wanted to block the YouTube logo (class name style-scope ytd-topbar-logo-renderer from loading). I guess I would like a MWE that stops it from loading.

(For context: I am completely new to both Chrome extensions in particular and Javascript in general, but otherwise somewhat familiar with programming. I am mostly just curious/playing around right now, but there is a vague goal of making a kind of 'productivity tool' for myself, allowing me to use Facebook, Youtube etc for exactly what I need them for, with distracting or extraneous (to me) elements, such as Recommended Videos, redacted.)

Drubbels
  • 327
  • 2
  • 4
  • 11
  • 1
    Remove background.js and [declare a content script](https://developer.chrome.com/extensions/content_scripts) in manifest.json, inside use MutationObserver, [example](https://stackoverflow.com/a/32537455). – wOxxOm Sep 15 '21 at 16:14
  • Hi, thank you. Unfortunately I don't (yet) understand most of what is in that comment. Am I correct in understanding that I should simply replace the '.header-nav-item, #topchapter, #wrapper_header' string at the top with the class name I want to remove? – Drubbels Sep 15 '21 at 16:36
  • With a selector for the class name i.e. with a dot like `.CLASS_NAME`. – wOxxOm Sep 15 '21 at 16:42
  • Thanks, I have tried that. Unfortunately it does not seem to work. – Drubbels Sep 15 '21 at 16:46
  • To convert a multi-token class name you need to replace all spaces with dots: `.style-scope.ytd-topbar-logo-renderer`. Also make sure that `matches` indeed matches the target site's [real URL](https://superuser.com/questions/1333575/chrome-address-bar-no-longer-shows-protocol-or-www-subdomain/1498561). – wOxxOm Sep 15 '21 at 16:53
  • I have tried this for YouTube (and some of the first elements I tried it on on other sites did not even have spaces in the names), and unfortunately it does not work. What's this about URL matching? I don't see anything about URL matching in the answer you linked. – Drubbels Sep 15 '21 at 17:01
  • You need to specify `matches` in manifest.json, see the documentation article I linked for more info. – wOxxOm Sep 15 '21 at 17:26
  • Thank you so much. I totally missed that the first time around. It works like a charm now! – Drubbels Sep 15 '21 at 18:39
  • Maybe you can overwrite the css class .ytd-topbar { display:none !important }. And inject it with the manifest – Johan Hoeksma Sep 16 '21 at 08:54

0 Answers0