Keep Kaggle Notebook Alive

Keep the Kaggle Notebook alive by simulating user activity

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Keep Kaggle Notebook Alive
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Keep the Kaggle Notebook alive by simulating user activity
// @author       liuweiqing
// @match        https://www.kaggle.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";

  // 每隔5分钟模拟一次点击事件,以保持Kaggle Notebook的活动状态
  function keepAlive() {
    console.log("Keeping the Kaggle Notebook alive...");
    const addButton = document.querySelector('button[aria-label="Add cell"]');
    if (addButton) {
      addButton.click(); // 模拟点击添加单元格按钮
    }
    const runButton = document.querySelector(
      'button[aria-label="Run current cell"]'
    );
    if (runButton) {
      runButton.click(); // 模拟点击运行单元格按钮
    }
    const cutButton = document.querySelector('button[aria-label="Cut cell"]');
    if (cutButton) {
      cutButton.click(); // 模拟点击删除单元格按钮
    }

    // 随机生成几分钟的间隔
    const randomInterval =
      Math.floor(Math.random() * (100000 - 50000 + 1)) + 240000;
    console.log(`Next execution in ${randomInterval} milliseconds.`);

    // 重新设置定时器
    setTimeout(keepAlive, randomInterval);
  }

  // 首次调用
  keepAlive();
  // 300000 毫秒 = 5 分钟
  // 这种形式已经无法触发任何按键了
  // const ctrlEnterEvent = new KeyboardEvent("keydown", {
  //   bubbles: true,
  //   cancelable: true,
  //   key: "Enter",
  //   code: "Enter",
  //   location: 0,
  //   ctrlKey: true, // 表示 Ctrl 键被按下
  //   repeat: false,
  // });
  // document.dispatchEvent(EnterEvent);
})();