Conversaciones » Peticiones de scripts

Anyone here know how to use Mutation Observer please help me!

§
Publicado: 20/11/2014

Anyone here know how to use Mutation Observer please help me!

Hi everybody, I'm trying to write a Anti AdBlock Blocker that work like detect element that gonna added to page, if it match my condition then remove it.

I know that there is "DomContentInserted" but it is now outdate, so I really want to use Mutation Observer because it is overall better.

Anyone can give me an example like this, many thank:

Use Mutation Observer to detect img tag going to add to the page, if match then set that img tag outerHTML = ""; to remove, and script should run at document-start for testing only, probably after I figure something then I can dig it and make it become a Anti Ablock Blocker soon, but at this time Mutation Observer, I know nothing about it but only an example that help my situation is enough.

woxxomMod
§
Publicado: 20/11/2014
Editado: 20/11/2014

Here's one from MDN. There are many other googlable examples.

To hide all added images (hiding is arguably faster than deleting) you can use something like this:

var mo = new MutationObserver(function(mutations){
  for (var m, i=0, mlen=mutations.length; i<mlen && (m=mutations[i]); i++)
    for (var nodes=m.addedNodes, n, j=0, nlen=nodes.length; j<nlen && (n=nodes[j]); j++)
      if (n.nodeType == 1) { // Node.ELEMENT_NODE
        var imgs = (n.tagName == 'IMG') ? [n] : n.getElementsByTagName('img');
        for (var img, k=imgs.length-1; (k>=0) && (img=imgs[k]); k--)
          img.style.display = 'none';
      }
});
mo.observe(document, {subtree:true, childList:true});
§
Publicado: 21/11/2014

Thank you very much!

Publicar respuesta

Inicia sesión para responder.