LegacyGM.js

A userscript library for adding support back to GM_ non async functions

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/534637/1581445/LegacyGMjs.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

/* LegacyGM.js
 - Version: 1.0.3
 - Author: Haka
 - Description: A userscript library for adding support back to GM_ non async functions
 - GitHub: https://github.com/Psyyke/A.C.A.S/
*/

async function LOAD_LEGACY_GM_SUPPORT() {
    if(typeof GM !== 'object') return;

    const noLegacyInfo =      typeof GM_info === 'undefined'              && typeof GM?.info !== 'undefined',
          onlyModernSet =     typeof GM?.setValue === 'function'          && typeof GM_setValue === 'undefined',
          onlyModernGet =     typeof GM?.getValue === 'function'          && typeof GM_getValue === 'undefined',
          onlyModernList =    typeof GM?.listValues === 'function'        && typeof GM_listValues === 'undefined',
          deleteValueExists = typeof GM?.deleteValue === 'function',
          openInTabExists =   typeof GM?.openInTab === 'function';

    if(noLegacyInfo) globalThis.GM_info = GM.info;
    if(!onlyModernList && !onlyModernGet && !onlyModernSet) return;

    const gmCache = {};
    const gmFunctions = {
        GM_setValue: (key, value) => {
            GM.setValue(key, value);
            gmCache[key] = value;
        },
        GM_getValue: (key, defaultValue) => {
            return key in gmCache ? gmCache[key] : defaultValue;
        },
        GM_deleteValue: (key) => {
            GM.deleteValue(key);
            delete gmCache[key];
        },
        GM_listValues: () => {
            return Object.keys(gmCache);
        },
        GM_openInTab: (url, options = false) => {
            if(openInTabExists)
                return GM.openInTab(url, options);

            return window.open(url, '_blank');
        },
    };

    setInterval(async () => {
        const keys = await GM.listValues();

        // Load existing
        for(const key of keys) {
            gmCache[key] = await GM.getValue(key);
        }

        // Remove old
        for(const key in gmCache) {
            if(!keys.includes(key)) {
                delete gmCache[key];
            }
        }
    }, 1);


    // Define legacy functions
    for(const [name, func] of Object.entries(gmFunctions)) {
        if(typeof globalThis[name] === 'undefined') {
            Object.defineProperty(globalThis, name, {
                value: func,
                writable: false,
                configurable: false,
                enumerable: false,
            });
        }
    }
}