Action Recorder and Replayer

Records and replays user actions with periodic and on-demand options

当前为 2024-08-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Action Recorder and Replayer
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Records and replays user actions with periodic and on-demand options
// @match        :///*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let actions = [];
    let recording = false;
    let replayInterval;

    // Start recording when 'x' is pressed
    document.addEventListener('keydown', function(event) {
        if (event.key === 'x') {
            recording = true;
            actions = [];
            console.log('Recording started');
        } else if (event.key === 'y') {
            recording = false;
            console.log('Recording stopped');
        }
    });

    // Capture mouse clicks and key presses
    document.addEventListener('click', function(event) {
        if (recording) {
            actions.push({
                type: 'click',
                x: event.clientX,
                y: event.clientY,
                time: Date.now()
            });
        }
    });

    document.addEventListener('keydown', function(event) {
        if (recording) {
            actions.push({
                type: 'keydown',
                key: event.key,
                time: Date.now()
            });
        }
    });

    // Replay actions
    function replayActions() {
        if (actions.length === 0) return;

        let startTime = actions[0].time;

        actions.forEach(action => {
            let delay = action.time - startTime;
            setTimeout(() => {
                if (action.type === 'click') {
                    let clickEvent = new MouseEvent('click', {
                        clientX: action.x,
                        clientY: action.y,
                        bubbles: true
                    });
                    document.dispatchEvent(clickEvent);
                } else if (action.type === 'keydown') {
                    let keyEvent = new KeyboardEvent('keydown', {
                        key: action.key,
                        bubbles: true
                    });
                    document.dispatchEvent(keyEvent);
                }
            }, delay);
        });
    }

    // Set up periodic replay every 4 hours
    function startPeriodicReplay() {
        replayInterval = setInterval(replayActions, 4 * 60 * 60 * 1000); // 4 hours in milliseconds
    }

    // Stop periodic replay
    function stopPeriodicReplay() {
        clearInterval(replayInterval);
    }

    // Trigger replay with 'z' and 'P' keys
    document.addEventListener('keydown', function(event) {
        if (event.key === 'z') {
            replayActions();
            startPeriodicReplay();
        } else if (event.key === 'P') {
            replayActions();
        }
    });

    // Optional: Stop periodic replay when the script is unloaded or at some other condition
    window.addEventListener('beforeunload', function() {
        stopPeriodicReplay();
    });
})();