Google Shut Up!

try to take over the world!

目前為 2020-09-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google Shut Up!
// @namespace    http://tampermonkey.net/
// @version      0.1.7
// @description  try to take over the world!
// @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 &&
           localStorage.getItem("dismissed-login") === "pending" &&
           document.URL.match(/https\:\/\/accounts\.(google|youtube)\.com/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/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});


    //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);
*/

})();