Change Agent

Monitor any page for element changes and pop an alert if a change is detected.

目前為 2024-05-23 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Change Agent
// @namespace    https://github.com/appel/userscripts
// @version      0.1.1
// @description  Monitor any page for element changes and pop an alert if a change is detected.
// @author       Ap
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function getQueryParams() {
        const params = new URLSearchParams(window.location.search);
        return {
            changeAgentEl: params.get('change_agent_el'),
            changeAgentTime: parseInt(params.get('change_agent_time') || '15', 10)
        };
    }

    function generateStorageKey() {
        return `change_agent_el_${window.location.href}`;
    }

    function storeElementContent(selector) {
        const element = document.querySelector(selector);
        if (element) {
            localStorage.setItem(generateStorageKey(), element.outerHTML);
            console.log(`Stored content for ${selector}:`, element.outerHTML);
        } else {
            console.warn(`Element with selector ${selector} not found`);
        }
    }

    function checkForChange(selector) {
        const storageKey = generateStorageKey();
        const storedContent = localStorage.getItem(storageKey);
        const currentElement = document.querySelector(selector);

        if (currentElement && storedContent) {
            console.log(`Stored content for ${selector} on reload:`, storedContent);
            if (currentElement.outerHTML !== storedContent) {
                alert('The monitored element has changed!');
                console.log("something changed!");
                console.log("Old value: ", storedContent);
                console.log("New value: ", currentElement.outerHTML);
            } else {
                setTimeout(() => location.reload(), changeAgentTime * 60 * 1000);
                document.querySelector('#countdown').textContent = 'No change: ' + document.querySelector('#countdown').textContent;
            }
        } else {
            console.warn(`Element with selector ${selector} not found or no stored content`);
        }
    }

    function createCountdownTimer(duration, display) {
        let timer = duration;
        let paused = false;
        display.textContent = formatTime(timer);

        display.addEventListener('click', () => {
            paused = !paused;
            display.style.backgroundColor = paused ? '#f44336' : '#f44336d4';
        });

        setInterval(() => {
            if (!paused) {
                timer--;
                display.textContent = formatTime(timer);
                if (timer <= 0) {
                    location.reload();
                }
            }
        }, 1000);
    }

    function formatTime(seconds) {
        const minutes = Math.floor(seconds / 60);
        const sec = seconds % 60;
        return `${minutes}:${sec < 10 ? '0' : ''}${sec}`;
    }

    function initCountdown(duration) {
        const countdownDiv = document.createElement('div');
        countdownDiv.id = 'countdown';
        countdownDiv.style.position = 'fixed';
        countdownDiv.style.bottom = '1rem';
        countdownDiv.style.left = '50%';
        countdownDiv.style.padding = '5px 10px';
        countdownDiv.style.backgroundColor = '#f44336d4';
        countdownDiv.style.color = 'white';
        countdownDiv.style.cursor = 'pointer';
        countdownDiv.style.zIndex = 10000;
        document.body.appendChild(countdownDiv);
        createCountdownTimer(duration, countdownDiv);
    }

    const { changeAgentEl, changeAgentTime } = getQueryParams();

    if (changeAgentEl) {
        window.addEventListener('load', () => {
            initCountdown(changeAgentTime * 60);

            setTimeout(() => {
                storeElementContent(changeAgentEl);

                setTimeout(() => {
                    location.reload();
                }, 15 * 60 * 1000);
            }, 30 * 1000);

            setTimeout(() => {
                checkForChange(changeAgentEl);
            }, changeAgentTime * 60 * 1000);
        });
    }
})();