Smart Page Auto Refresh

Intelligently refreshes web pages only when content changes - no configuration needed

当前为 2024-11-05 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Smart Page Auto Refresh
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Intelligently refreshes web pages only when content changes - no configuration needed
// @author       kequn yang
// @match        *://*
// @match        file:///*
// @grant        none
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    
    let lastContentHash = '';
    const REFRESH_INTERVAL = 1000;  // 1 second default interval
    
    function getContentHash() {
        const content = document.body.innerHTML;
        let hash = 0;
        for (let i = 0; i < content.length; i++) {
            const char = content.charCodeAt(i);
            hash = ((hash << 5) - hash) + char;
            hash = hash & hash;
        }
        return hash.toString();
    }
    
    function hasContentChanged() {
        const currentHash = getContentHash();
        const hasChanged = lastContentHash !== currentHash;
        lastContentHash = currentHash;
        return hasChanged;
    }

    async function checkAndRefresh() {
        try {
            const response = await fetch(window.location.href);
            const text = await response.text();
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = text;
            
            if (hasContentChanged()) {
                window.location.reload();
            }
        } catch (error) {
            window.location.reload();
        }
    }

    function setupAutoRefresh() {
        lastContentHash = getContentHash();
        setInterval(checkAndRefresh, REFRESH_INTERVAL);
    }

    setupAutoRefresh();
})();