Greasy Fork

Greasy Fork is available in English.

ChatGPT HeartBeat

USE AT YOUR OWN RISK!

当前为 2023-03-31 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT HeartBeat
// @namespace    http://tampermonkey.net/
// @version      0.2.3
// @license      GPLv3
// @description  USE AT YOUR OWN RISK!
// @author       https://v2ex.com/t/926890
// @homepage     https://v2ex.com/t/926890
// @homepageURL  https://v2ex.com/t/926890
// @match        https://chat.openai.com/chat*
// @icon         https://chat.openai.com/favicon.ico
// @require      http://greasyfork.icu/scripts/395037-monkeyconfig-modern/code/MonkeyConfig%20Modern.js?version=764968
// @run-at       document-start
// @noframes
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_addStyle
// ==/UserScript==

/*
  需要保持非常久的,可以额外尝试在 chrome://discards 里禁用 `Auto Discardable`,
  或者用 https://github.com/WorldLanguages/DoNotDiscard
  否则就算保持了 Cookies 有效,Chrome 也有可能自动休眠标签页。
*/
(function () {
    var isWindow = function (obj) {
        if (typeof window.constructor !== "undefined") {
            return obj instanceof window.constructor;
        } else {
            return obj.window === obj;
        }
    };

    // https://gist.github.com/fuzmish/bd444b1aadc2d22aada7c9b1a6de56ba
    const rawAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function (...args) {
        const eventName = args[0];
        if (isWindow(this)) {
            // 防止页面通过监听事件强制刷新
            if (["focus", "focusin", "visibilitychange"].includes(eventName)) {
                return;
            }
        }
        return rawAddEventListener.apply(this, args);
    };

    var cfg = new MonkeyConfig({
        title: "Configuration",
        menuCommand: true,
        params: {
            refreshInterval: {
                type: "number",
                default: 30,
            },
            refreshURL: {
                type: "text",
                default:
                "https://chat.openai.com/_next/static/k9OKjvwgjWES7JT3k-6g9/_ssgManifest.js",
            },
        },
    });

    var getRefreshURL = function () {
        var refreshURL = cfg.get("refreshURL");
        // 如果手动配置了 _ssgManifest.js 以外的 URL,就不尝试获取最新的
        if (refreshURL !== "" && refreshURL.indexOf("_ssgManifest.js") === -1) {
            return refreshURL;
        }
        // 获取最新的 _ssgManifest.js 链接
        Array.prototype.slice
            .call(document.getElementsByTagName("script"))
            .every(function (script) {
            if (script.src.indexOf("_ssgManifest.js") !== -1) {
                if (refreshURL !== script.src) {
                    refreshURL = script.src;
                    cfg.set("refreshURL", refreshURL);
                    return false;
                }
            }
            return true;
        });
        return refreshURL;
    };

    var iframe = document.createElement("iframe");
    iframe.id = "heartbeat";
    iframe.style = "display:none";
    iframe.name = "heartbeat";
    iframe.src = getRefreshURL();
    document.head.insertBefore(iframe, document.head.firstChild);

    var count = 0;
    setInterval(function () {
        if (iframe.contentWindow.location.href === getRefreshURL()) {
            count = 0;
            iframe.contentWindow.location.reload(true);
        } else {
            // 如果两分钟还没跳转完毕就强制刷新
            if (count++ * cfg.get("refreshInterval") >= 2 * 60) {
                iframe.src = getRefreshURL();
            }
        }
    }, cfg.get("refreshInterval") * 1000);
})();