MediaWiki:Gadget-LocalObjectStorage.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/**
* -------------------------------------------------------------------------
* !!! 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 (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> */