Action Recorder and Replayer

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

目前為 2024-08-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
    });
})();