跳转到内容

MediaWiki:Gadget-LocalObjectStorage.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";var __classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,a,c,o){if("m"===c)throw new TypeError("Private method is not writable");if("a"===c&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===c?o.call(e,a):o?o.value=a:t.set(e,a),a},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,a,c){if("a"===a&&!c)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!c:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===a?c:"a"===a?c.call(e):c?c.value:t.get(e)};(()=>{var e,t,a;const c=[{type:"undefined",match:e=>void 0===e,encode:()=>"undefined",decode:()=>{}},{type:"bigint",match:e=>"bigint"==typeof e,encode:e=>`${e}`,decode:e=>BigInt(e)},{type:"date",match:e=>e instanceof Date,encode:e=>e.toISOString(),decode:e=>new Date(e)},{type:"set",match:e=>e instanceof Set,encode:e=>JSON.stringify([...e.values()]),decode:e=>new Set(JSON.parse(e))},{type:"map",match:e=>e instanceof Map,encode:e=>JSON.stringify([...e.entries()]),decode:e=>new Map(JSON.parse(e))},{type:"regexp",match:e=>e instanceof RegExp,encode:e=>`${e}`,decode:e=>new RegExp(e.slice(1,e.length-1))}],o=[];class LocalObjectStorage{constructor(a=""){if(e.add(this),t.set(this,void 0),"default"===a)throw new Error(`LocalObjectStorage can't accept prefix "${a}".`);if(a.includes("/"))throw new Error(`LocalObjectStorage can't accept prefix "${a}" including "/".`);__classPrivateFieldSet(this,t,"AnnTool-localObjectStorage/"+(a?.length>0?`${a}/`:"default/"),"f")}get _keyPrefix(){return __classPrivateFieldGet(this,t,"f")}getAllKeys(){return __classPrivateFieldGet(this,e,"m",a).call(this).map(e=>e.replace(__classPrivateFieldGet(this,t,"f"),""))}get length(){return __classPrivateFieldGet(this,e,"m",a).call(this).length}getItem(e,a){const o=localStorage.getItem(`${__classPrivateFieldGet(this,t,"f")}${e}`);if(null===o)return a||o;for(const{type:t,decode:a}of c.concat(LocalObjectStorage.plugins.transformations.list))if(t.includes("|"))console.error(`LocalObjectStorage can't accept type name "${t}" including "|", skip...`);else if("JSON"!==t){if(o.startsWith(`${t}|`))try{return a(o.replace(`${t}|`,""))}catch(a){console.error(`LocalObjectStorage can's transform value of key "${e}" to type "${t}" and skip...`)}}else console.error(`LocalObjectStorage can't accept type name "${t}", skip...`);try{return JSON.parse(o.replace("JSON|",""))}catch(t){return void console.error(`LocalObjectStorage can's transform value of key "${e}" to JSON and return \`undefined\`...`,t)}}setItem(e,a){for(const{type:o,match:r,encode:n}of c.concat(LocalObjectStorage.plugins.transformations.list))if(o.includes("|"))console.error(`LocalObjectStorage can't accept type name "${o}" including "|", skip...`);else if("JSON"!==o){if(r(a))try{return void localStorage.setItem(`${__classPrivateFieldGet(this,t,"f")}${e}`,`${o}|${n(a)}`)}catch(t){console.error(`LocalObjectStorage can's transform value of key "${e}" from type "${o}" and skip...`,t)}}else console.error(`LocalObjectStorage can't accept type name "${o}", skip...`);try{return void localStorage.setItem(`${__classPrivateFieldGet(this,t,"f")}${e}`,`JSON|${JSON.stringify(a)}`)}catch(t){console.error(`LocalObjectStorage can's transform value of key "${e}" from JSON and skip...`,t)}}removeItem(e){localStorage.removeItem(`${__classPrivateFieldGet(this,t,"f")}${e}`)}clear(){__classPrivateFieldGet(this,e,"m",a).call(this).forEach(e=>{localStorage.removeItem(e)}),this.length=0}key(t){return __classPrivateFieldGet(this,e,"m",a).call(this)[t]}}t=new WeakMap,e=new WeakSet,a=function _LocalObjectStorage_getAllKeys(){return Object.keys(localStorage).filter(e=>e.startsWith(__classPrivateFieldGet(this,t,"f")))},LocalObjectStorage.plugins={transformations:{get list(){return o.map(e=>Object.assign(Object.create(null),e))},add:({type:e,match:t,decode:a,encode:r})=>e.includes("|")?(console.error(`LocalObjectStorage can't accept type name "${e}" including "|", skip...`),!1):["JSON"].includes(e)?(console.error(`LocalObjectStorage can't accept type name "${e}", skip...`),!1):[...c,...LocalObjectStorage.plugins.transformations.list].some(({type:t})=>t===e)?(console.error(`LocalObjectStorage can't accept duplicated type name "${e}", skip...`),!1):"function"!=typeof t||"function"!=typeof a||"function"!=typeof r?(console.error(`LocalObjectStorage can't accept broken transformation [ type: "${e}", match: ${typeof t}, decode: ${typeof a}, encode: ${typeof r} ], skip...`),!1):(o.push({type:e,match:t,decode:a,encode:r}),!0)}},window.LocalObjectStorage=LocalObjectStorage})();  
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
(() => {
    var _LocalObjectStorage_instances, _LocalObjectStorage_keyPrefix, _LocalObjectStorage_getAllKeys;
    const builtinTransformations = [
        {
            type: "undefined",
            match: (t) => typeof t === "undefined",
            encode: () => "undefined",
            decode: () => undefined,
        },
        {
            type: "bigint",
            match: (t) => typeof t === "bigint",
            encode: (b) => `${b}`,
            decode: (b) => BigInt(b),
        },
        {
            type: "date",
            match: (t) => t instanceof Date,
            encode: (d) => d.toISOString(),
            decode: (d) => new Date(d),
        },
        {
            type: "set",
            match: (t) => t instanceof Set,
            encode: (s) => JSON.stringify([...s.values()]),
            decode: (s) => new Set(JSON.parse(s)),
        },
        {
            type: "map",
            match: (t) => t instanceof Map,
            encode: (m) => JSON.stringify([...m.entries()]),
            decode: (m) => new Map(JSON.parse(m)),
        },
        {
            type: "regexp",
            match: (t) => t instanceof RegExp,
            encode: (r) => `${r}`,
            decode: (r) => new RegExp(r.slice(1, r.length - 1)),
        },
    ];
    const externalTransformations = [];
    class LocalObjectStorage {
        constructor(prefix = "") {
            _LocalObjectStorage_instances.add(this);
            _LocalObjectStorage_keyPrefix.set(this, void 0);
            if (prefix === "default") {
                throw new Error(`LocalObjectStorage can't accept prefix "${prefix}".`);
            }
            if (prefix.includes("/")) {
                throw new Error(`LocalObjectStorage can't accept prefix "${prefix}" including "/".`);
            }
            __classPrivateFieldSet(this, _LocalObjectStorage_keyPrefix, `AnnTool-localObjectStorage/${prefix?.length > 0 ? `${prefix}/` : "default/"}`, "f");
        }
        get _keyPrefix() {
            return __classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f");
        }
        getAllKeys() {
            return __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this).map((n) => n.replace(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f"), ""));
        }
        get length() {
            return __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this).length;
        }
        getItem(key, fallback) {
            const value = localStorage.getItem(`${__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")}${key}`);
            if (value === null) {
                return fallback || value;
            }
            for (const { type, decode } of builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list)) {
                if (type.includes("|")) {
                    console.error(`LocalObjectStorage can't accept type name "${type}" including "|", skip...`);
                    continue;
                }
                if (type === "JSON") {
                    console.error(`LocalObjectStorage can't accept type name "${type}", skip...`);
                    continue;
                }
                if (value.startsWith(`${type}|`)) {
                    try {
                        return decode(value.replace(`${type}|`, ""));
                    }
                    catch (_e) {
                        console.error(`LocalObjectStorage can's transform value of key "${key}" to type "${type}" and skip...`);
                    }
                }
            }
            try {
                return JSON.parse(value.replace("JSON|", ""));
            }
            catch (e) {
                console.error(`LocalObjectStorage can's transform value of key "${key}" to JSON and return \`undefined\`...`, e);
                return undefined;
            }
        }
        setItem(key, value) {
            for (const { type, match, encode } of builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list)) {
                if (type.includes("|")) {
                    console.error(`LocalObjectStorage can't accept type name "${type}" including "|", skip...`);
                    continue;
                }
                if (type === "JSON") {
                    console.error(`LocalObjectStorage can't accept type name "${type}", skip...`);
                    continue;
                }
                if (match(value)) {
                    try {
                        localStorage.setItem(`${__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")}${key}`, `${type}|${encode(value)}`);
                        return;
                    }
                    catch (e) {
                        console.error(`LocalObjectStorage can's transform value of key "${key}" from type "${type}" and skip...`, e);
                    }
                }
            }
            try {
                localStorage.setItem(`${__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")}${key}`, `JSON|${JSON.stringify(value)}`);
                return;
            }
            catch (e) {
                console.error(`LocalObjectStorage can's transform value of key "${key}" from JSON and skip...`, e);
            }
        }
        removeItem(key) {
            localStorage.removeItem(`${__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")}${key}`);
        }
        clear() {
            __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this).forEach((key) => {
                localStorage.removeItem(key);
            });
            this.length = 0;
        }
        key(index) {
            return __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this)[index];
        }
    }
    _LocalObjectStorage_keyPrefix = new WeakMap(), _LocalObjectStorage_instances = new WeakSet(), _LocalObjectStorage_getAllKeys = function _LocalObjectStorage_getAllKeys() {
        return Object.keys(localStorage).filter((key) => key.startsWith(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")));
    };
    LocalObjectStorage.plugins = {
        transformations: {
            get list() {
                return externalTransformations.map((transformation) => Object.assign(Object.create(null), transformation));
            },
            add: ({ type, match, decode, encode }) => {
                if (type.includes("|")) {
                    console.error(`LocalObjectStorage can't accept type name "${type}" including "|", skip...`);
                    return false;
                }
                if (["JSON"].includes(type)) {
                    console.error(`LocalObjectStorage can't accept type name "${type}", skip...`);
                    return false;
                }
                if ([...builtinTransformations, ...LocalObjectStorage.plugins.transformations.list].some(({ type: eType }) => eType === type)) {
                    console.error(`LocalObjectStorage can't accept duplicated type name "${type}", skip...`);
                    return false;
                }
                if (typeof match !== "function" || typeof decode !== "function" || typeof encode !== "function") {
                    console.error(`LocalObjectStorage can't accept broken transformation [ type: "${type}", match: ${typeof match}, decode: ${typeof decode}, encode: ${typeof encode} ], skip...`);
                    return false;
                }
                externalTransformations.push({ type, match, decode, encode });
                return true;
            },
        },
    };
    window.LocalObjectStorage = LocalObjectStorage;
})();  


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

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

/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/LocalObjectStorage/Gadget-LocalObjectStorage.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";var __classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,a,c,o){if("m"===c)throw new TypeError("Private method is not writable");if("a"===c&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===c?o.call(e,a):o?o.value=a:t.set(e,a),a},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,a,c){if("a"===a&&!c)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!c:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===a?c:"a"===a?c.call(e):c?c.value:t.get(e)};(()=>{var e,t,a;const c=[{type:"undefined",match:e=>void 0===e,encode:()=>"undefined",decode:()=>{}},{type:"bigint",match:e=>"bigint"==typeof e,encode:e=>`${e}`,decode:e=>BigInt(e)},{type:"date",match:e=>e instanceof Date,encode:e=>e.toISOString(),decode:e=>new Date(e)},{type:"set",match:e=>e instanceof Set,encode:e=>JSON.stringify([...e.values()]),decode:e=>new Set(JSON.parse(e))},{type:"map",match:e=>e instanceof Map,encode:e=>JSON.stringify([...e.entries()]),decode:e=>new Map(JSON.parse(e))},{type:"regexp",match:e=>e instanceof RegExp,encode:e=>`${e}`,decode:e=>new RegExp(e.slice(1,e.length-1))}],o=[];class LocalObjectStorage{constructor(a=""){if(e.add(this),t.set(this,void 0),"default"===a)throw new Error(`LocalObjectStorage can't accept prefix "${a}".`);if(a.includes("/"))throw new Error(`LocalObjectStorage can't accept prefix "${a}" including "/".`);__classPrivateFieldSet(this,t,"AnnTool-localObjectStorage/"+(a?.length>0?`${a}/`:"default/"),"f")}get _keyPrefix(){return __classPrivateFieldGet(this,t,"f")}getAllKeys(){return __classPrivateFieldGet(this,e,"m",a).call(this).map(e=>e.replace(__classPrivateFieldGet(this,t,"f"),""))}get length(){return __classPrivateFieldGet(this,e,"m",a).call(this).length}getItem(e,a){const o=localStorage.getItem(`${__classPrivateFieldGet(this,t,"f")}${e}`);if(null===o)return a||o;for(const{type:t,decode:a}of c.concat(LocalObjectStorage.plugins.transformations.list))if(t.includes("|"))console.error(`LocalObjectStorage can't accept type name "${t}" including "|", skip...`);else if("JSON"!==t){if(o.startsWith(`${t}|`))try{return a(o.replace(`${t}|`,""))}catch(a){console.error(`LocalObjectStorage can's transform value of key "${e}" to type "${t}" and skip...`)}}else console.error(`LocalObjectStorage can't accept type name "${t}", skip...`);try{return JSON.parse(o.replace("JSON|",""))}catch(t){return void console.error(`LocalObjectStorage can's transform value of key "${e}" to JSON and return \`undefined\`...`,t)}}setItem(e,a){for(const{type:o,match:r,encode:n}of c.concat(LocalObjectStorage.plugins.transformations.list))if(o.includes("|"))console.error(`LocalObjectStorage can't accept type name "${o}" including "|", skip...`);else if("JSON"!==o){if(r(a))try{return void localStorage.setItem(`${__classPrivateFieldGet(this,t,"f")}${e}`,`${o}|${n(a)}`)}catch(t){console.error(`LocalObjectStorage can's transform value of key "${e}" from type "${o}" and skip...`,t)}}else console.error(`LocalObjectStorage can't accept type name "${o}", skip...`);try{return void localStorage.setItem(`${__classPrivateFieldGet(this,t,"f")}${e}`,`JSON|${JSON.stringify(a)}`)}catch(t){console.error(`LocalObjectStorage can's transform value of key "${e}" from JSON and skip...`,t)}}removeItem(e){localStorage.removeItem(`${__classPrivateFieldGet(this,t,"f")}${e}`)}clear(){__classPrivateFieldGet(this,e,"m",a).call(this).forEach(e=>{localStorage.removeItem(e)}),this.length=0}key(t){return __classPrivateFieldGet(this,e,"m",a).call(this)[t]}}t=new WeakMap,e=new WeakSet,a=function _LocalObjectStorage_getAllKeys(){return Object.keys(localStorage).filter(e=>e.startsWith(__classPrivateFieldGet(this,t,"f")))},LocalObjectStorage.plugins={transformations:{get list(){return o.map(e=>Object.assign(Object.create(null),e))},add:({type:e,match:t,decode:a,encode:r})=>e.includes("|")?(console.error(`LocalObjectStorage can't accept type name "${e}" including "|", skip...`),!1):["JSON"].includes(e)?(console.error(`LocalObjectStorage can't accept type name "${e}", skip...`),!1):[...c,...LocalObjectStorage.plugins.transformations.list].some(({type:t})=>t===e)?(console.error(`LocalObjectStorage can't accept duplicated type name "${e}", skip...`),!1):"function"!=typeof t||"function"!=typeof a||"function"!=typeof r?(console.error(`LocalObjectStorage can't accept broken transformation [ type: "${e}", match: ${typeof t}, decode: ${typeof a}, encode: ${typeof r} ], skip...`),!1):(o.push({type:e,match:t,decode:a,encode:r}),!0)}},window.LocalObjectStorage=LocalObjectStorage})(); 

/* </pre> */