MediaWiki:Gadget-LocalObjectStorage.js

来自萌娘共享
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/LocalObjectStorage/MediaWiki:Gadget-LocalObjectStorage.js|user=[[U:星海子]]|co-authors=|longId=e1e15bf6ac13a6c997a9a3208dab7f70fa6a77fb|shortId=e1e15bf6|summary=chore: eslint}}'; 

/* <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 __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
(function () {
    var builtinTransformations = [
        {
            type: "undefined",
            match: function (t) { return typeof t === "undefined"; },
            encode: function () { return "undefined"; },
            decode: function () { return undefined; }
        },
        {
            type: "bigint",
            match: function (t) { return typeof t === "bigint"; },
            encode: function (b) { return "".concat(b); },
            decode: function (b) { return BigInt(b); }
        },
        {
            type: "date",
            match: function (t) { return t instanceof Date; },
            encode: function (d) { return d.toISOString(); },
            decode: function (d) { return new Date(d); }
        },
        {
            type: "set",
            match: function (t) { return t instanceof Set; },
            encode: function (s) { return JSON.stringify(__spreadArray([], __read(s.values()), false)); },
            decode: function (s) { return new Set(JSON.parse(s)); }
        },
        {
            type: "map",
            match: function (t) { return t instanceof Map; },
            encode: function (m) { return JSON.stringify(__spreadArray([], __read(m.entries()), false)); },
            decode: function (m) { return new Map(JSON.parse(m)); }
        },
        {
            type: "regexp",
            match: function (t) { return t instanceof RegExp; },
            encode: function (r) { return "".concat(r); },
            decode: function (r) { return new RegExp(r.slice(1, r.length - 1)); }
        },
    ];
    var externalTransformations = [];
    var LocalObjectStorage = (function () {
        function LocalObjectStorage(prefix) {
            if (prefix === void 0) { prefix = ""; }
            _LocalObjectStorage_instances.add(this);
            _LocalObjectStorage_keyPrefix.set(this, void 0);
            if (prefix === "default") {
                throw new Error("LocalObjectStorage can't accept prefix \"".concat(prefix, "\"."));
            }
            if (prefix.includes("/")) {
                throw new Error("LocalObjectStorage can't accept prefix \"".concat(prefix, "\" including \"/\"."));
            }
            __classPrivateFieldSet(this, _LocalObjectStorage_keyPrefix, "AnnTool-localObjectStorage/".concat((prefix === null || prefix === void 0 ? void 0 : prefix.length) > 0 ? "".concat(prefix, "/") : "default/"), "f");
        }
        Object.defineProperty(LocalObjectStorage.prototype, "_keyPrefix", {
            get: function () {
                return __classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f");
            },
            enumerable: false,
            configurable: true
        });
        LocalObjectStorage.prototype.getAllKeys = function () {
            var _this = this;
            return __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this).map(function (n) { return n.replace(__classPrivateFieldGet(_this, _LocalObjectStorage_keyPrefix, "f"), ""); });
        };
        Object.defineProperty(LocalObjectStorage.prototype, "length", {
            get: function () {
                return __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this).length;
            },
            enumerable: false,
            configurable: true
        });
        LocalObjectStorage.prototype.getItem = function (key, fallback) {
            var e_1, _a;
            var value = localStorage.getItem("".concat(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")).concat(key));
            if (value === null) {
                return fallback || value;
            }
            try {
                for (var _b = __values(builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var _d = _c.value, type = _d.type, decode = _d.decode;
                    if (type.includes("|")) {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\" including \"|\", skip..."));
                        continue;
                    }
                    if (type === "JSON") {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\", skip..."));
                        continue;
                    }
                    if (value.startsWith("".concat(type, "|"))) {
                        try {
                            return decode(value.replace("".concat(type, "|"), ""));
                        }
                        catch (_e) {
                            console.error("LocalObjectStorage can's transform value of key \"".concat(key, "\" to type \"").concat(type, "\" and skip..."));
                        }
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
                }
                finally { if (e_1) throw e_1.error; }
            }
            try {
                return JSON.parse(value.replace("JSON|", ""));
            }
            catch (e) {
                console.error("LocalObjectStorage can's transform value of key \"".concat(key, "\" to JSON and return `undefined`..."), e);
                return undefined;
            }
        };
        LocalObjectStorage.prototype.setItem = function (key, value) {
            var e_2, _a;
            try {
                for (var _b = __values(builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var _d = _c.value, type = _d.type, match = _d.match, encode = _d.encode;
                    if (type.includes("|")) {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\" including \"|\", skip..."));
                        continue;
                    }
                    if (type === "JSON") {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\", skip..."));
                        continue;
                    }
                    if (match(value)) {
                        try {
                            localStorage.setItem("".concat(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")).concat(key), "".concat(type, "|").concat(encode(value)));
                            return;
                        }
                        catch (e) {
                            console.error("LocalObjectStorage can's transform value of key \"".concat(key, "\" from type \"").concat(type, "\" and skip..."), e);
                        }
                    }
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
                }
                finally { if (e_2) throw e_2.error; }
            }
            try {
                localStorage.setItem("".concat(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")).concat(key), "JSON|".concat(JSON.stringify(value)));
                return;
            }
            catch (e) {
                console.error("LocalObjectStorage can's transform value of key \"".concat(key, "\" from JSON and skip..."), e);
            }
        };
        LocalObjectStorage.prototype.removeItem = function (key) {
            localStorage.removeItem("".concat(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")).concat(key));
        };
        LocalObjectStorage.prototype.clear = function () {
            __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this).forEach(function (key) {
                localStorage.removeItem(key);
            });
            this.length = 0;
        };
        LocalObjectStorage.prototype.key = function (index) {
            return __classPrivateFieldGet(this, _LocalObjectStorage_instances, "m", _LocalObjectStorage_getAllKeys).call(this)[index];
        };
        var _LocalObjectStorage_instances, _LocalObjectStorage_keyPrefix, _LocalObjectStorage_getAllKeys;
        _LocalObjectStorage_keyPrefix = new WeakMap(), _LocalObjectStorage_instances = new WeakSet(), _LocalObjectStorage_getAllKeys = function _LocalObjectStorage_getAllKeys() {
            var _this = this;
            return Object.keys(localStorage).filter(function (key) { return key.startsWith(__classPrivateFieldGet(_this, _LocalObjectStorage_keyPrefix, "f")); });
        };
        LocalObjectStorage.plugins = {
            transformations: {
                get list() {
                    return externalTransformations.map(function (transformation) { return Object.assign(Object.create(null), transformation); });
                },
                add: function (_a) {
                    var type = _a.type, match = _a.match, decode = _a.decode, encode = _a.encode;
                    if (type.includes("|")) {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\" including \"|\", skip..."));
                        return false;
                    }
                    if (["JSON"].includes(type)) {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\", skip..."));
                        return false;
                    }
                    if (__spreadArray(__spreadArray([], __read(builtinTransformations), false), __read(LocalObjectStorage.plugins.transformations.list), false).some(function (_a) {
                        var eType = _a.type;
                        return eType === type;
                    })) {
                        console.error("LocalObjectStorage can't accept duplicated type name \"".concat(type, "\", skip..."));
                        return false;
                    }
                    if (typeof match !== "function" || typeof decode !== "function" || typeof encode !== "function") {
                        console.error("LocalObjectStorage can't accept broken transformation [ type: \"".concat(type, "\", match: ").concat(typeof match, ", decode: ").concat(typeof decode, ", encode: ").concat(typeof encode, " ], skip..."));
                        return false;
                    }
                    externalTransformations.push({ type: type, match: match, decode: decode, encode: encode });
                    return true;
                }
            }
        };
        return LocalObjectStorage;
    }());
    window.LocalObjectStorage = LocalObjectStorage;
})(); 

/* </pre> */