Greasy Fork

GM_config_lz-string

Refactor GM_config, this version uses lz-string to access data for a Library script

目前为 2018-10-01 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/372760/633308/GM_config_lz-string.js

// ==UserScript==
// @namespace     http://tampermonkey.net/
// @exclude       *
// @require      https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js
// @require      https://openuserjs.org/src/libs/sizzle/GM_config.js

// ==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) {
	let rval, value;
	try {
		value = LZString.decompressFromUTF16(this.getValue(store || this.id, '{}'));
		rval = this.parser(value);
	} 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 {
		let value = LZString.compressToUTF16(this.stringify(obj || values));
		this.setValue(store || this.id, value);
	} catch(e) {
		this.log("GM_config failed to save settings!");
	}

	return forgotten;
}