Google Colab Stay Alive

Keeps alive Colab session (It adds a button for activating.) / Colab oturumunu aktif tutar.

目前為 2021-05-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Google Colab Stay Alive
// @namespace    https://gist.github.com/mcakici/e418862ca6b448bb04f8aacf680478c0
// @version      1.1
// @description  Keeps alive Colab session (It adds a button for activating.) / Colab oturumunu aktif tutar.
// @include      /^https?:\/\/colab\.research\.google\.com\/.*$/
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    window.addEventListener('load', function() {
        var isEnabled = false;
        var colabKeepAlive = null;
        //Keep Page Active
        Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: true});
        Object.defineProperty(document, 'hidden', {value: false, writable: true});
        document.dispatchEvent(new Event("visibilitychange"));

        //Define MutationObserver to automatically reconnect
        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
        if (MutationObserver) console.log('> Colab Stay Alive Auto reconnector is loaded.');
        var observer = new MutationObserver(function(mutations) {
            console.log('> Colab Stay Alive Detected DOM changes.');
            setTimeout(function () {
                if(isEnabled === true && document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect") !== null){
                    var ok = document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect");
                    if(ok.textContent.includes("Reconnect") || ok.textContent.includes("RECONNECT") || ok.textContent.includes("Connect") || ok.textContent.includes("connect") || ok.textContent.includes("CONNECT") || ok.textContent.includes("Yeniden Bağlan") || ok.textContent.includes("Bağlan")) {
                        console.log('> Colab Stay Alive Reconnecting...');
                        ok.click();
                        console.log('> Colab Stay Alive Connected.');
                    }
                }
            }, 3000);
        });
        observer.observe(document.body, {childList: true});

        function Colab_KeepAlive(){
            if(document.querySelector("#check_KeepAliveColab").checked && colabKeepAlive == null){
                colabKeepAlive = setInterval(function(){
                    if(document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect") !== null){
                        document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
                        console.log("> Colab Stay Alive Connect Button Clicked Successfully.");
                    }
                }, 60000);
                isEnabled = true;
                console.log("> Colab Stay Alive Activated.");
            }else{
                clearInterval(colabKeepAlive);
                colabKeepAlive = null;
                isEnabled = false;
                console.log("> Colab Stay Alive Disabled.");
            }
            document.getElementById('check_KeepAliveColab').blur();
        }
        console.log("> Colab Keep Alive Started.");
        var mydiv = document.createElement('div');
        mydiv.style = 'position:fixed;top:0;left:47%;background:rgba(0,0,0,0.65);color:#000;z-index:999;padding:8px 10px;';
        mydiv.innerHTML = '<paper-checkbox id="check_KeepAliveColab" role="checkbox"><span style="color:#fff">Keep-alive</span></paper-checkbox>';
        document.getElementsByTagName('body')[0].appendChild(mydiv);
        document.getElementById("check_KeepAliveColab").addEventListener("click", Colab_KeepAlive);
    });
})();