k_____k / Filmweb Rankingi - ukryj obejrzane

// ==UserScript==
// @name         Filmweb Rankingi - ukryj obejrzane
// @namespace    http://filmweb.pl/
// @license MIT
// @version      0.3
// @description  Ten skrypt dodaje w sekcji filtrów rankingów checkbox 'Ukryj obejrzane', którego zaznaczenie powoduje ukrycie ocenionych pozycji. Obecnie działa jedynie na załadowanym rankingu, czyli wymagane jest np. szybkie przeskorolowanie przez ranking w celu załadowania wszystkich pozycji i dopiero wtedy użycie checkboxa.
// @author       k_____k
// @match        https://www.filmweb.pl/ranking/*
// @grant        GM_log
// ==/UserScript==
var hideAlreadySeen = false;

function showRankingItem(visible) {
    const unseenScoreValues = ['unchecked', 'nwts', ''];
    let divs = document.querySelectorAll('[itemprop="itemListElement"]');
    let unseenMoviesCount = 0;
    let allMoviesCount = 0;
    const possibleRibbonStates = new Set();
    for (let x = 0; x < divs.length; x++) {
        let div = divs[x];
        let ribbonElement = div.getElementsByClassName('ribbon')[0];
        let userScore = ribbonElement.getAttribute('data-state');
        possibleRibbonStates.add(userScore);
        if(unseenScoreValues.indexOf(userScore) < 0) {
            div.style.display = visible ? '' : 'none';
        } else {
            unseenMoviesCount++;
        }
        allMoviesCount++;
    }
    console.debug(`All possible user score values ${new Array(...possibleRibbonStates).join(' ')}`);
    console.debug(`User have not seen ${unseenMoviesCount}/${allMoviesCount} movies`);
};

var zNode = document.createElement ('div');
zNode.innerHTML = `<label class="control control--checkbox">
<input type="checkbox" name="hideSeen" id="hideSeen" /> <span class="control__title">Ukryj obejrzane</span>
<div class="control__indicator"></div>
</label>`;

var filtersBar = document.querySelectorAll('div.rankingHeader__filters form')[0];
filtersBar.appendChild(zNode);

document.getElementById('hideSeen').addEventListener( 'change', (event) => {
    if (event.target.checked) {
        hideAlreadySeen = true;
    } else {
        hideAlreadySeen = false;
    }
    showRankingItem(!hideAlreadySeen);
});

// Select the node that will be observed for mutations
const targetNode = document.getElementsByClassName('rankingTypeSection')[0];

// Options for the observer (which mutations to observe)
const config = { attributes: false, childList: true, subtree: false };

// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
    for(let mutation of mutationsList) {
        if (mutation.type === 'childList') {
            if(mutation.addedNodes.length > 0) {
                showRankingItem(!hideAlreadySeen);
            }
        }
    }
};

// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);