Greasy Fork

来自缓存

Greasy Fork is available in English.

网站自动刷新

当前网页如果未打开,则一直监听,直到网站能够直接打开,即不再刷新

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         网站自动刷新
// @namespace    https://github.com/webguosai
// @version      0.9
// @icon         https://avatars.githubusercontent.com/u/2083784?v=4
// @description  当前网页如果未打开,则一直监听,直到网站能够直接打开,即不再刷新
// @author       Guo Sai
// @match        http://127.0.0.1:9501/*
// @run-at       document-start
// @grant        none
// @license      GPL-3.0
// ==/UserScript==

(function() {
    'use strict';

    let maxAttempts = 300000; // 最大重试次数
    let currentAttempt = 0; // 当前重试次数

    function refreshIfUnreachable() {
        // console.log('开始执行.');
        currentAttempt++;

        fetch(window.location.href, { method: 'HEAD' })
            .then(response => {
                if (!response.ok) {
                    // console.log(`网站打不开 (${currentAttempt}/${maxAttempts}), refreshing...`);
                    location.reload(true);
                } else {
                    // console.log('停止定时器.');
                    clearInterval(intervalId); // 停止定时器
                }
            })
            .catch(error => {
                // console.error('An error occurred while checking website accessibility:', error);

                if (currentAttempt >= maxAttempts) {
                    // console.log('达到最大重试次数,停止刷新.');
                    clearInterval(intervalId); // 停止定时器
                }
            });
    }

    // 初始化执行,每隔一定时间检查网站的可访问性
    let intervalId = setInterval(refreshIfUnreachable, 300); // 每隔 x 毫秒执行一次检查
})();