Terminal Keep-Alive (with clean input)

通过粘贴字符,保持 SSH 会话活跃

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Terminal Keep-Alive (with clean input)
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  通过粘贴字符,保持 SSH 会话活跃
// @match        https://{jumpserver}/koko/terminal/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    console.log('[KeepAlive] 脚本已加载(粘贴 + 回退)');

    function getTerminalInputTarget() {
        const canvas = document.querySelector('canvas');
        return canvas ? canvas.parentElement : document.body;
    }

    function simulatePasteCleaned(text = '\u200B') {
        const target = getTerminalInputTarget();
        if (!target) return;

        target.focus();

        // 第一次:粘贴不可见字符
        const pasteEvent1 = new ClipboardEvent('paste', {
            clipboardData: new DataTransfer(),
            bubbles: true,
            cancelable: true
        });
        pasteEvent1.clipboardData.setData('text/plain', text);
        target.dispatchEvent(pasteEvent1);
    }

    function getRandomInterval(min = 170000, max = 210000) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function scheduleNextPaste() {
        const delay = getRandomInterval();
        console.log(`[KeepAlive] 下次粘贴将在 ${(delay / 1000).toFixed(0)} 秒后`);

        setTimeout(() => {
            simulatePasteCleaned();
            scheduleNextPaste();
        }, delay);
    }

    scheduleNextPaste();
})();