Youtube WC

Automatically clicks on the reject cookies button

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

// ==UserScript==
// @name         Youtube WC
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Automatically clicks on the reject cookies button
// @author       Radaman
// @match        https://www.youtube.com/*
// @match        https://consent.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      gpl-2.0
// ==/UserScript==

(function() {
    'use strict';

    // Verbosity
    let consentPage = window.location.href.includes("consent") ? true : false;
    if (consentPage) {
        let rejectButton = document.querySelector('[aria-label="Reject all"]');
        if (rejectButton == null) {
            let allButtons = document.querySelectorAll('button');
            if (allButtons.length == 0) {
                console.log("Could not find buttons.");
            }
            else {
                allButtons[0].click();
            }
        }
        else {
            rejectButton.click();
        }
        return;
    }
    let verbose = false;

    // Set up some variables for maintaining order of appearance
    let buttonDetected = false;
    let dialogDetected = false;

    // Select the node that will be observed for mutations
    const targetNode = document.getRootNode();

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

    // Callback function to execute when mutations are observed
    const callback = (mutationList, observer) => {
        for (const mutation of mutationList) {
            if (mutation.target.id == "dialog") {
                hideDialog();
                dialogDetected = true;
                if (buttonDetected) {
                    rejectCookies();
                    observer.disconnect();
                    return;
                }
            }
            if (mutation.target.className == "yt-spec-touch-feedback-shape__fill") {
                if (dialogDetected) {
                    rejectCookies();
                    observer.disconnect();
                    document.querySelector("overlay").style.display = "none";
                    return;
                }
                else {
                    buttonDetected = true;
                    if (verbose) console.log('Would have rejected cookies.');
                }
            }
        }
    };

    function getElementByXpath(path) {
      return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }

    function hideDialog() {
        document.querySelector("tp-yt-iron-overlay-backdrop").style.display = "none";
        document.getElementById("dialog").style.display = "none";
        if (verbose) console.log('Hidden dialog');
    }

    function rejectCookies() {
        getElementByXpath("/html/body/ytd-app/ytd-consent-bump-v2-lightbox/tp-yt-paper-dialog/div[4]/div[2]/div[6]/div[1]/ytd-button-renderer[1]/yt-button-shape/button/yt-touch-feedback-shape/div/div[2]").click();
        if (verbose) console.log('Rejected cookies');
    }

    // 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);
})();