CPP摊位状态监控

即使后台也每60秒刷新页面,若状态变化则发送通知

目前為 2025-04-23 提交的版本,檢視 最新版本

// ==UserScript==
// @name         CPP摊位状态监控
// @namespace    http://tampermonkey.net/
// @version      1.3.1
// @description  即使后台也每60秒刷新页面,若状态变化则发送通知
// @match        https://www.allcpp.cn/mng/apply.do?t=1&pageNo=1
// @grant        none
// @license      MIT
// @author       liyasan
// ==/UserScript==

(function () {
    'use strict';

    const ID0 = "position-status0";
    const ID1 = "position-status1";
    const REFRESH_INTERVAL = 60 * 1000; // 60秒

    // 初始化记录状态
    let lastStatus0 = localStorage.getItem("cpp_status0") || "";
    let lastStatus1 = localStorage.getItem("cpp_status1") || "";

    // 请求系统通知权限
    if (Notification.permission !== "granted") {
        Notification.requestPermission();
    }

    // 提取状态文本
    function getStatusText(id) {
        const el = document.getElementById(id);
        return el ? el.textContent.trim() : '';
    }

    // 触发系统通知
    function sendNotification(title, body) {
        if (Notification.permission === "granted") {
            new Notification(title, {
                body: body,
                icon: "https://www.allcpp.cn/favicon.ico"
            });
        }
    }

    // 检查内容是否变化
    function detectChange() {
        const current0 = getStatusText(ID0);
        const current1 = getStatusText(ID1);

        let changed = false;
        if (lastStatus0 && current0 !== lastStatus0) {
            sendNotification("摊位状态变动", `position-status0 变为:${current0}`);
            changed = true;
        }
        if (lastStatus1 && current1 !== lastStatus1) {
            sendNotification("摊位状态变动", `position-status1 变为:${current1}`);
            changed = true;
        }

        // 存储当前状态
        localStorage.setItem("cpp_status0", current0);
        localStorage.setItem("cpp_status1", current1);
        lastStatus0 = current0;
        lastStatus1 = current1;

        console.log("✅ 状态记录完毕," + (changed ? "⚠️ 有变动" : "无变化") + "。60秒后刷新页面…");

        setTimeout(() => {
            console.log("🔁 正在强制刷新页面(后台有效)");
            location.reload(true); // 强制从服务器刷新页面
        }, REFRESH_INTERVAL);
    }

    // 页面加载后,延迟 2 秒执行检测
    window.addEventListener("load", () => {
        setTimeout(detectChange, 2000);
    });
})();