自动刷新GitHub页面

在 GitHub 任意非编辑页面首次访问时强制刷新一次,解决某些脚本和扩展不生效的问题

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         自动刷新GitHub页面
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  在 GitHub 任意非编辑页面首次访问时强制刷新一次,解决某些脚本和扩展不生效的问题
// @author       klcb2010
// @match        https://github.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const REFRESH_FLAG = 'gh_auto_full_refresh_done';

    
    const excludeKeywords = ['edit', 'new', 'settings', 'upload', 'releases/new', 'issues/new', 'pull/new'];

    function isExcluded(url = location.href) {
        return excludeKeywords.some(keyword => url.includes(keyword));
    }

    function shouldRefresh() {
        return !sessionStorage.getItem(REFRESH_FLAG) && !isExcluded();
    }

    function forceFullReload() {
        sessionStorage.setItem(REFRESH_FLAG, 'true');
        console.log('[GitHub Auto Refresh] Forcing full reload:', location.href);
        setTimeout(() => {
            try {
                location.reload(true); 
            } catch (e) {
                console.warn('reload(true) failed, fallback to normal reload');
                location.reload();
            }
        }, 1000);
    }

    function resetFlagIfNavigated() {
        sessionStorage.removeItem(REFRESH_FLAG);
        console.log('[GitHub Auto Refresh] URL changed, reset flag');
    }

    function onUrlChange(callback) {
        let lastUrl = location.href;
        new MutationObserver(() => {
            if (location.href !== lastUrl) {
                lastUrl = location.href;
                callback();
            }
        }).observe(document.body, { childList: true, subtree: true });
    }

   
    if (shouldRefresh()) {
        forceFullReload();
    }

   
    onUrlChange(() => {
        resetFlagIfNavigated();
        if (shouldRefresh()) {
            forceFullReload();
        }
    });
})();