Keep Kaggle Notebook Alive

Keep the Kaggle Notebook alive by simulating user activity

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==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);
})();