MediaWiki:Gadget-LocalObjectStorage.js

MediaWiki界面页面
一位史蒂夫讨论 | 贡献2024年1月2日 (二) 23:29的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
"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_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 Object.keys(localStorage).filter(function (key) { return key.startsWith(__classPrivateFieldGet(_this, _LocalObjectStorage_keyPrefix, "f")); });
        };
        Object.defineProperty(LocalObjectStorage.prototype, "length", {
            get: function () {
                return this._getAllKeys().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 raw value..."));
                return value;
            }
        };
        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..."));
                        }
                    }
                }
            }
            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 store raw value..."));
                localStorage.setItem("".concat(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")).concat(key), value);
            }
        };
        LocalObjectStorage.prototype.removeItem = function (key) {
            localStorage.removeItem("".concat(__classPrivateFieldGet(this, _LocalObjectStorage_keyPrefix, "f")).concat(key));
        };
        LocalObjectStorage.prototype.clear = function () {
            this._getAllKeys().forEach(function (key) {
                localStorage.removeItem(key);
            });
            this.length = 0;
        };
        LocalObjectStorage.prototype.key = function (index) {
            return this._getAllKeys()[index];
        };
        var _LocalObjectStorage_keyPrefix;
        _LocalObjectStorage_keyPrefix = new WeakMap();
        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 (type === "JSON") {
                        console.error("LocalObjectStorage can't accept type name \"".concat(type, "\", skip..."));
                        return false;
                    }
                    if (builtinTransformations.concat(LocalObjectStorage.plugins.transformations.list).filter(function (_a) {
                        var eType = _a.type;
                        return eType === type;
                    }).length > 0) {
                        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;
})();