Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active June 22, 2025 02:51
Show Gist options
  • Select an option

  • Save MohamedElashri/409bb26bd681eefcf8af6b532a661859 to your computer and use it in GitHub Desktop.

Select an option

Save MohamedElashri/409bb26bd681eefcf8af6b532a661859 to your computer and use it in GitHub Desktop.
[Prevent Colab from sleeping]
// ==UserScript==
// @name Google Colab Stay Alive
// @namespace https://gist.github.com/MohamedElashri
// @version 1.0.0
// @author Mohamed Elashri
// @description Keeps alive Colab session (Adds a button for activation.).
// @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;
var observer = new MutationObserver(function(mutations) {
setTimeout(function () {
if(isEnabled === true && document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect") !== null){
console.log('> Colab Stay Alive Detected MutationObserver changes.');
var ok = document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect");
var okTXT = ok.textContent
if(okTXT.indexOf("Reconnect") !== -1 || okTXT.indexOf("RECONNECT") !== -1 || okTXT.indexOf("Connect") !== -1 || okTXT.indexOf("connect") !== -1 || okTXT.indexOf("CONNECT") !== -1 || okTXT.indexOf("Yeniden bağlan") !== -1 || okTXT.indexOf("Bağlan") !== -1 || okTXT.indexOf("Yeniden Bağlan") !== -1) {
console.log('> Colab Stay Alive Reconnecting...');
ok.click();
console.log('> Colab Stay Alive Connected.');
}
if(typeof document.getElementsByTagName('colab-recaptcha-dialog')[0] !== 'undefined'){
document.getElementsByTagName('iron-overlay-backdrop')[0].click();
}
if(typeof document.getElementsByTagName('colab-dialog')[0] !== 'undefined' && typeof document.getElementsByTagName('colab-dialog')[0].textContent !== 'undefined'){
var dialogTXT = document.getElementsByTagName('colab-dialog')[0].textContent;
if(dialogTXT.indexOf("Runtime disconnected") !== -1 || dialogTXT.indexOf("runtime has timed out") !== -1){
document.getElementsByTagName('iron-overlay-backdrop')[0].click();
}
}
}
}, 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.getElementById('connect') !== null){
document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.getElementById('connect').click();
console.log("> Your click on Colab stay alive was loaded 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);
});
})();

run the following code in the console and it will prevent you from disconnecting.

  1. Ctrl+ Shift + i to open inspector view .
  2. go to console.
function ClickConnect(){
    console.log("Working"); 
    document.querySelector("colab-toolbar-button#connect").click() 
}
setInterval(ClickConnect,60000)


Also this can be done by

function ColabReconnect() {
    var dialog = document.querySelector("colab-dialog.yes-no-dialog");
    var dialogTitle = dialog && dialog.querySelector("div.content-area>h2");
    if (dialogTitle && dialogTitle.innerText == "Runtime disconnected") {
        dialog.querySelector("paper-button#ok").click();
        console.log("Reconnecting...");
    } else {
        console.log("ColabReconnect is in service.");
    }
}
timerId = setInterval(ColabReconnect, 60000);

Another and better thing instead of clicking the connect button is click on comment button to keep my session alive

function ClickConnect(){

console.log("Working"); 
document.querySelector("#comments > span").click() 
}
setInterval(ClickConnect,5000)


Another creative option is to keep clicking "Refresh folder" in the file explorer pane

function ClickRefresh(){
  console.log("Working"); 
  document.querySelector("[icon='colab:folder-refresh']").click()
}
const myjob = setInterval(ClickRefresh, 60000)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment