dhowe / AdNauseam

AdNauseam: Fight back against advertising surveillance
GNU General Public License v3.0
4.52k stars 189 forks source link

[Parser] Do not process same element twice #2236

Closed mneunomne closed 1 year ago

mneunomne commented 1 year ago

It seems that our parser keeps parsing the same elements over and over every time the filtering mechanism is activated.

I would suggest for us to mark the elements that we have already processed with a process-adn tag or something of that nature. It would in theory save some processing power on the client side.

mneunomne commented 1 year ago

In the PR above I implemented the following line in the process function:

  if(elem.hasAttribute('process-adn')) {
        logP('Element already processed by parser')
        return;
      } else {
        elem.setAttribute('process-adn', true)
        logP('Process(' + elem.tagName + ')',
          elem.tagName === 'IFRAME' && elem.hasAttribute('src')
            ? elem.getAttribute('src') : elem);
      }

I tested and it seems to not affect the amount of collected ads. On one run on pinterest.com I got 378 counts of [PARSER] Element already processed by parser logs, meaning, 378 times in one load that queries, img checks etc etc were not processed again without necessity.

dhowe commented 1 year ago

that's great -- my only concern is that these elements are constantly changing, so we need a mechanism (a hash?) with which we can tell whether the element has changed, and if so, process it again

mneunomne commented 1 year ago

Yes, I was wondering thre same thing. But maybe it will have high cost to create listeners to all these dom elements? Need to test it.

element.addEventListener('DOMNodeInserted', callback);
mneunomne commented 1 year ago

Best solution is to remove any adn-process attribute on the domWatcherInterface itself, here:

https://github.com/dhowe/AdNauseam/blob/3a56e11f4ad35ba1fb89a0c8a336b5c3912a1d80/src/js/contentscript.js#L954-L967

mneunomne commented 1 year ago

Implemented https://github.com/dhowe/AdNauseam/issues/2236