ColabHelper

A helper to start and terminate the colab session

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         ColabHelper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  A helper to start and terminate the colab session
// @author       You
// @match        https://colab.research.google.com/drive/*
// @icon         https://www.google.com/s2/favicons?domain=colab.research.google.com
// @grant        none
// ==/UserScript==

var zNode = document.createElement ('div');
zNode.innerHTML = '<button id="startButton" type="button">Start</button>  <button id="closeButton" type="button">Close</button>';
zNode.setAttribute ('id', 'myContainer');
document.body.appendChild (zNode);

//--- Activate the newly added button.
document.getElementById ("startButton").addEventListener (
    "click", startButtonClickAction, false
);

document.getElementById ("closeButton").addEventListener (
    "click", endButtonClickAction, false
);

function startButtonClickAction (zEvent) {
    /*--- For our dummy action, we'll just add a line of text to the top
        of the screen.
    */
    var zNode = document.createElement ('p');
    document.getElementById ("myContainer").appendChild (zNode);
    document.querySelector("colab-run-button").click();
}

function endButtonClickAction (zEvent) {
    end();
}

//Sleep function
const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

//End function
const end = async () => {
    //Click on menu button
    document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
    await sleep(500);

    //Click on manage sessions
    document.querySelector("colab-usage-display").shadowRoot.querySelector("div paper-button").click();
    await sleep(2000);

    //Click on terminate session
    //document.querySelector("body > colab-dialog > paper-dialog > colab-sessions-dialog").shadowRoot.querySelector("div.dialog-main-content > div.sessions-content.layout.vertical > div.dialog-table > colab-session > div.button-action-column > paper-icon-button").click()
    document.querySelector("body > colab-dialog > paper-dialog > colab-sessions-dialog").shadowRoot.querySelector("div.dialog-main-content > div.sessions-content.layout.vertical > div.dialog-table > colab-session > div.button-action-column > paper-button").click();
    await sleep(1000);

    //Click on okay buttom if prompted
    try {
      document.querySelector("#ok").click();
      // await sleep(1000);
    }
    catch(err) {
      console.log('No okay button')
    }

    document.querySelector("body > colab-dialog > paper-dialog > colab-sessions-dialog").shadowRoot.querySelector("paper-button.dismiss").click()
    await sleep(1000);
    //document.querySelector("colab-usage-display").shadowRoot.querySelector("div paper-button").click();
}