跳转到内容

MediaWiki:Gadget-libCachedCode.js:修订间差异

来自萌娘共享
代码变动:103d1a56 - feat: rename (#594) by U:AnnAngela, co-authored-by: GH:github-actions[bot]
标签由机器人或全自动脚本执行的操作
代码变动:103d1a56 - feat: rename (#594) by U:AnnAngela, co-authored-by: GH:github-actions[bot]
标签由机器人或全自动脚本执行的操作
 
第8行: 第8行:
/* <pre> */
/* <pre> */


"use strict";
"use strict";(()=>{const e=new LocalObjectStorage("AnnTools-libCachedCode");for(const e of Object.keys(localStorage))e.startsWith("AnnTools-libCachedCode")&&localStorage.removeItem(e);const getCachedCode=async t=>{let{code:o}=e.getItem(`${t}`)||{};return o||(o=await(await fetch(t)).text()),e.setItem(`AnnTools-libCachedCode:${t}`,{code:o,timestamp:Date.now()}),o},getCachedCodeUrl=async e=>(e=>{const t=new Blob([e],{type:"text/plain"});return URL.createObjectURL(t)})(await getCachedCode(e)),injectCachedCode=async(e,t)=>{const o=t.toLowerCase();if(["script","javascript","js"].includes(o)){const t=document.createElement("script");return t.src=await getCachedCodeUrl(e),await new Promise(e=>{t.addEventListener("load",()=>{e()}),document.head.append(t)})}["css","style"].includes(o)&&mw.loader.addStyleTag(await getCachedCode(e))},t=Date.now()-2592e6;for(const o of e.getAllKeys())e.getItem(o).timestamp<t&&e.removeItem(o);window.libCachedCode={getCachedCode,getCachedCodeUrl,injectCachedCode,batchInjectCachedCode:(e,t)=>Promise.all(e.map(e=>injectCachedCode(e,t)))}})();  
(() => {
    const localObjectStorage = new LocalObjectStorage("AnnTools-libCachedCode");
    for (const i of Object.keys(localStorage)) {
        if (i.startsWith("AnnTools-libCachedCode")) {
            localStorage.removeItem(i);
        }
    }
    const codeToUrl = (code) => {
        const blob = new Blob([code], { type: "text/plain" });
        return URL.createObjectURL(blob);
    };
    const getCachedCode = async (url) => {
        let { code } = localObjectStorage.getItem(`${url}`) || {};
        if (!code) {
            code = await (await fetch(url)).text();
        }
        localObjectStorage.setItem(`AnnTools-libCachedCode:${url}`, { code, timestamp: Date.now() });
        return code;
    };
    const getCachedCodeUrl = async (url) => codeToUrl(await getCachedCode(url));
    const injectCachedCode = async (url, _type) => {
        const type = _type.toLowerCase();
        if (["script", "javascript", "js"].includes(type)) {
            const script = document.createElement("script");
            script.src = await getCachedCodeUrl(url);
            return await new Promise((res) => {
                script.addEventListener("load", () => {
                    res();
                });
                document.head.append(script);
            });
        }
        if (["css", "style"].includes(type)) {
            mw.loader.addStyleTag(await getCachedCode(url));
            return;
        }
    };
    const batchInjectCachedCode = (urls, type) => Promise.all(urls.map((url) => injectCachedCode(url, type)));
    const expired = Date.now() - 30 * 24 * 60 * 60 * 1000;
    for (const k of localObjectStorage.getAllKeys()) {
        if (localObjectStorage.getItem(k).timestamp < expired) {
            localObjectStorage.removeItem(k);
        }
    }
    window.libCachedCode = {
        getCachedCode,
        getCachedCodeUrl,
        injectCachedCode,
        batchInjectCachedCode,
    };
})();  


/* </pre> */
/* </pre> */

2025年7月26日 (六) 00:11的最新版本

/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/libCachedCode/Gadget-libCachedCode.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]|longId=103d1a563ea4ccc8ff29fb55c9bcd88329a56eb5|shortId=103d1a56|summary=feat: rename (#594)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com></nowiki>}}'; 

/* <pre> */

"use strict";(()=>{const e=new LocalObjectStorage("AnnTools-libCachedCode");for(const e of Object.keys(localStorage))e.startsWith("AnnTools-libCachedCode")&&localStorage.removeItem(e);const getCachedCode=async t=>{let{code:o}=e.getItem(`${t}`)||{};return o||(o=await(await fetch(t)).text()),e.setItem(`AnnTools-libCachedCode:${t}`,{code:o,timestamp:Date.now()}),o},getCachedCodeUrl=async e=>(e=>{const t=new Blob([e],{type:"text/plain"});return URL.createObjectURL(t)})(await getCachedCode(e)),injectCachedCode=async(e,t)=>{const o=t.toLowerCase();if(["script","javascript","js"].includes(o)){const t=document.createElement("script");return t.src=await getCachedCodeUrl(e),await new Promise(e=>{t.addEventListener("load",()=>{e()}),document.head.append(t)})}["css","style"].includes(o)&&mw.loader.addStyleTag(await getCachedCode(e))},t=Date.now()-2592e6;for(const o of e.getAllKeys())e.getItem(o).timestamp<t&&e.removeItem(o);window.libCachedCode={getCachedCode,getCachedCodeUrl,injectCachedCode,batchInjectCachedCode:(e,t)=>Promise.all(e.map(e=>injectCachedCode(e,t)))}})(); 

/* </pre> */