Greasy Fork is available in English.
Refactor GM_config, this version uses lz-string to access data for a Library script
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/372760/633316/GM_config_lz-string.js
// ==UserScript==
// @namespace http://tampermonkey.net/
// @exclude *
// ==UserLibrary==
// @name GM_config_lz-string
// @description Refactor GM_config, this version uses lz-string to access data for a Library script
// @author avan
// @license MIT
// @version 0.1
// ==/UserScript==
// ==/UserLibrary==
GM_configStruct.prototype.read = function (store) {
var rval, cKey, dValue;
try {
cKey = LZString.compressToUTF16(store || this.id);
dValue = LZString.decompressFromUTF16(this.getValue(cKey, '{}'));
rval = this.parser(dValue);
} catch(e) {
this.log("GM_config failed to read saved settings!");
rval = {};
}
return rval;
};
GM_configStruct.prototype.write = function (store, obj) {
if (!obj) {
var values = {},
forgotten = {},
fields = this.fields;
for (var id in fields) {
var field = fields[id];
var value = field.toValue();
if (field.save) {
if (value !== null) {
values[id] = value;
field.value = value;
} else
values[id] = field.value;
} else
forgotten[id] = value;
}
}
try {
var cKey = LZString.compressToUTF16(store || this.id),
cValue = LZString.compressToUTF16(this.stringify(obj || values));
this.setValue(cKey, cValue);
} catch(e) {
this.log("GM_config failed to save settings!");
}
return forgotten;
};