I need to be notified when an element with class 'nav' is created as the document is loading. Googling I found MutationObservers and thought they would be perfect, but I can't seem to get it working.
// ==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 nether case was 'nav creation' log on page load. What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…