Greasy Fork

Greasy Fork is available in English.

Google Shut Up!

Remove annoying cookies popup on google and youtube, sometimes dismiss login popup on youtube

当前为 2020-09-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Shut Up!
// @namespace    http://tampermonkey.net/
// @version      0.1.7.3
// @description  Remove annoying cookies popup on google and youtube, sometimes dismiss login popup on youtube 
// @author       You
// @include      /^https\:\/\/[a-z]*\.(google|youtube)\.[a-z]*/
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    console.log("Google Shut Up: Start");

    //https://stackoverflow.com/a/45956628----
    //youtube wtf events
    //new layout > 2017
    window.addEventListener("yt-navigate-finish", function(event) {
        window.dispatchEvent(new Event('locationchange'))
    });

    //old layout < 2017
    window.addEventListener("spfdone", function(e) {
        window.dispatchEvent(new Event('locationchange'))
    });

    function TryToTrueDismissLogin(){
        //to avoid the return of popup
        if(document.cookie.match(/CONSENT\=YES\+EN\.en\+V13\+BX/) !== null){
            if(localStorage.getItem("dismissed-login") === "pending" &&
                document.URL.match(/^https\:\/\/(accounts\.(google|youtube)\.com|www\.google\.com\/recaptcha)/i) === null){
                localStorage.setItem("dismissed-login", "true")
                console.log("Google Shut Up: dismissed-login: true");
                location.reload();
            } 
        }
    }

    window.addEventListener('locationchange', TryToTrueDismissLogin);

    //if cookie is unset then inject it
    if(document.cookie.match(/CONSENT\=YES\+EN\.en\+V13\+BX/) === null){
        //cookie injection
        document.cookie = "CONSENT=YES+EN.en+V13+BX; expires=Fri, 01 Jan 2038 00:00:00 GMT; domain="+document.URL.match(/^https\:\/\/[a-z]*\.((google|youtube)\.[\.a-z]*)/)[1]+"; path =/; Secure";
        //this prevent use of mutation observer
        if(document.URL.match(/^https\:\/\/www\.youtube\.com/i) !== null){
            console.log("Google Shut Up: dismissed-login: false");
            localStorage.setItem("dismissed-login","false");
        }
        //reload on accounts.google.com pages causes infinite loop
        if(document.URL.match(/^https\:\/\/(accounts\.(google|youtube)\.com|www\.google\.com\/recaptcha)/i) === null){
            //refresh page to avoid cookie's popup
            console.log("cookie refresh");
            location.reload();
        }
    } else {
        TryToTrueDismissLogin();
    }

    var ytdObserver = null;
    var videoObserver = null;
    var currentTime = 0;
    var waitToRun = 0;

    function dismissLoginRequest(){

        //Check if Observer intercepted the login popup
        if(document.querySelector("paper-dialog") !== null &&
           document.querySelector("paper-dialog>yt-upsell-dialog-renderer") !== null){
            console.log("Google Shut Up: waited "+waitToRun*20+" ms");
            var dismiss_button = document.getElementById("dismiss-button");
            //Optimistic click
            if(dismiss_button !== null){
                dismiss_button.click();

                //Pending because this is not the end
                console.log("Google Shut Up: dismissed-login: pending");
                localStorage.setItem("dismissed-login","pending");
                ytdObserver.disconnect();
            }
        } else {
            if(waitToRun < 4){
                waitToRun++;
                setTimeout(function(){dismissLoginRequest();},20);
            }
            return;
        }

        waitToRun = 0;

        var video = document.querySelector(".html5-video-container>video");
        if(video !== null &&
           video.paused === true){
            videoObserver.disconnect();
            //play again the video, I don't know how youtube stop it
            video.click();
            //restore last currentTime saved but it's not perferct
            video.currentTime = currentTime;
            currentTime = 0;
        }
    }

    window.addEventListener("load",function(){
        //this workaround is for youtube only because it asks to login
        if(document.URL.match(/^https\:\/\/www\.youtube\.com/i) !== null){
            //check if there is nothing to do
            if(localStorage.getItem("dismissed-login") === null ||
               localStorage.getItem("dismissed-login") === "false"){
                console.log("Google Shut Up: waiting login popup");
                //Observe page's mutation to intercept login popup
                ytdObserver = new MutationObserver(dismissLoginRequest);
                ytdObserver.observe(
                    document.querySelector("ytd-app"),
                    {childList: true}
                );

                //Observe player mutation because when youtube ask to login reset player currentTime
                //this solution is not perfect but it seems to work
                if(document.querySelector(".html5-video-player") !== null){
                    videoObserver = new MutationObserver(function(){
                        var videoCurrentTime = document.querySelector(".html5-video-container>video").currentTime;
                        currentTime = (videoCurrentTime > currentTime) ? videoCurrentTime : currentTime;
                    });
                    videoObserver.observe(
                        document.querySelector(".html5-video-player"),
                        {attributes: true}
                    );
                }
            }else{
                console.log("Google Shut Up: nothing to do here");
            }
        }
    },{once:true});


    //This part is useless now but I maintain it as comment for further needed
    //commented because if script fails you can still manually allow cookies and dismiss login popup
    //var style = document.createElement("style");
    //style.innerHTML = "#lb, iron-overlay-backdrop.opened[style^='z-index:'], ytd-consent-bump-lightbox[id='consent-bump']/*,paper-dialog.style-scope.ytd-popup-container[style*='z-index:']*/{display: none !important}";
/*
    if(document.URL.match(/https\:\/\/www\.youtube\.com/i) === null){
        style.innerHTML += "html{overflow-y: scroll !important}";
    }

    var headCheckInterval = window.setInterval( function (){
        if(document.head !== null){
            window.clearInterval(headCheckInterval);
            //no more needed
            //document.head.appendChild(style);
        }
    }, 20);
*/

})();