setMutationHandler

MutationObserver wrapper to wait for the specified CSS selector

< Σχολιασμός για τον κώδικα setMutationHandler

Αναφορά: Καλός - ο κώδικας λειτουργεί

§
Δημοσιεύτηκε: 16/05/2019

This script is magic

Hey @wOxxOm I've been using the 'for' loop version of this forever in lots of scripts, but I never really learned how to get the most out of it. I've left the observer connected unnecessarily in some cases, so I'm trying to figure out how to use it properly. I can get the 'forEach' loops working, but IDEK how to confirm that the observer has disconnected properly though.

What I'm trying:

// ==UserScript==//
// @name         Ololo
// @include      /https?://ololo\.to.*/
// @run-at       document-start
// @require      https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// ==/UserScript==

const searchPage = location.href.match('^https?:\/\/ololo\.to\/s\/.*$');
const videoPage = location.href.match('^https?:\/\/ololo\.to\/video\/.*$');

setMutationHandler({
    processExisting: true, // Process existing elements and watch for mutations from now on
    selector: '.results .item, .item-host[title="Vidcloud" i], .item-host[title="Vidlox" i], .item-host[title="Go Unlimited" i]',
    handler: nodes => nodes.forEach(node => {
        if (searchPage) {
            if (node.classList.contains('low-relevant')) {
                node.remove();
                return false;
            }
            if (node.classList.contains('item-host')) {
                const badHostItem = node.closest('.item');
                badHostItem.remove();
                return false;
            }
        }
    })
});

Not sure if return false; should go after both 'ifs', or inside, or something else entirely. Works either way, but I have no clue if it's actually disconnecting, or if it is, if they're both completing by sheer luck. I've tried adding console logs to the disconnect conditions in the library itself, and get nothing. I can't find anything in event listeners. How can I tell if it disconnected successfully?

Also, are all the options enabled by default? They all seem to work without adding any customizations, so do I even need to add processExisting: true?

§
Δημοσιεύτηκε: 20/05/2019

Nvm. Finally figured out what I was doing wrong. If you follow the documentation instead of making it up as you go along, it disconnects as expected.

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.