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 यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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