I need to be notified when an element with the class "nav" is created when the document is loaded. Googling I found MutationObservers and thought they would be perfect, but I can't get it to work.
// ==UserScript== // @name ii-shortcuts // @namespace https://github.com/RedHatter // @include * // @version 1 // @run-at document-start // ==/UserScript== var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.target.getAttribute('class') == 'nav') GM_log('nav creation'); }); }); observer.observe(document, {subtree: true, attributes: true, attributeFilter: ['class']});
I also tried.
// ==UserScript== // @name ii-shortcuts // @namespace https://github.com/RedHatter // @include * // @version 1 // @run-at document-start // ==/UserScript== var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.addedNodes[0].getAttribute('class') == 'nav') GM_log('nav creation'); }); }); observer.observe(document, {subtree: true, childList: true});
But in the latter case, "nav creation" was registered on the page load. What am I missing?
source share