Discussions » Creation Requests

Library question

§
Posted: 2015-03-08

Library question

Hello, admin, i have a question, if i use in a script a library from GitHub, but when i posted the script it was somethinf with GreasyFork whitelist, i wanted to upload library here of course with all the credits and link to author, but i get 404 error, please upload the library here if this it's better

link https://gist.github.com/BrockA/2625891/

§
Posted: 2015-03-08

P.S. just found posted https://greasyfork.org/en/scripts/1003-wait-for-key-elements but i hardly wish that this will be in GreasyFork Library

wOxxOmMod
§
Posted: 2015-03-08

The upload bug aside but that function looks so wrong... Isn't MutationObserver the correct method of detecting any changes to DOM?

§
Posted: 2015-03-08

well, i found that library more easy to use, i just used for Одноклассники to remove ads blocks from middle of page, and when i scroll the content is loading and waitforkeyelements quick remove the blocks, **wOxxOm ** can you show me a simple example if it's possible to use MutationObserver in this case? my script is on GreasyFork

wOxxOmMod
§
Posted: 2015-03-08
Edited: 2015-03-08

MutationObserver is better because it allows an immediate action on the modified/added content unlike the timer method.

I'm using on other site something like this:

var rxExclude = /Реклама|Промо|spravky\.ru|эпиляци|омоложение|варикоз|зрелые женщины|Зайди в нашу игру|Сыграем в покер/;

setMutationHandler(document, 'div.feed-list > div', function(observer, node) {
    if (node.textContent.match(rxExclude))
        node.style.setProperty('display', 'none');
});

function setMutationHandler(baseNode, selector, cb) {
  var ob = new MutationObserver(function(mutations){
    for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
      for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
        if (n.nodeType == 1) 
          if (n.matches(selector) || (n = n.querySelector(selector)))
            if (!cb(ob, n))
              return;
  });
  ob.observe(baseNode, {subtree:true, childList:true}); 
}
  1. In each mutation notification it finds just the first matching node but this can be easily altered: instead of if (n.matches(selector) || (n = n.querySelector(selector))) use if ((n.matches(selector) && (n=[n])) || ((n=n.querySelectorAll(selector)) && n.length>0)) and then do a forEach or for enumeration in your callback.

  2. You may want to replace textContent with innerHTML in case you'd like to exclude some urls or attributes.

§
Posted: 2015-03-09

i tried but it's seems not working, or i'm doing something wrong, but why this code is so big for a simple function), i guess i will stick to my code) it's working, that is what i want, thank you wOxxOm for your variant

wOxxOmMod
§
Posted: 2015-03-09
Edited: 2015-03-09

seems not working

Well, mutation handlers should be attached before the document is loaded, so the script needs // @run-at document-start.

this code is so big

Not true: the actual modification is done in 3 lines, the same amount as in your code.

setMutationHandler(document, 'div.feed-list > div', function(observer, node) {
    if (node.textContent.match(rxExclude))
        node.style.setProperty('display', 'none');

The mutation handler has 11 lines, whereas that library has 98 lines.

Post reply

Sign in to post a reply.