阅读:2548回复:2
GM_setValue是不是可以存列表了?
http://wiki.greasespot.net/GM_setValue
这里说它只能存字符串、布尔和整数 但我用setvalue存了个列表,console.log一看getvalue取回的是object array wiki太旧了? |
|
1楼#
发布于:2014-08-31 17:15
https://github.com/greasemonkey/greasemonkey/blob/master/modules/miscapis.js
GM_ScriptStorage.prototype.setValue = function(name, val) { if (2 !== arguments.length) { throw new Error(this.stringBundle.GetStringFromName('error.args.setValue')); } var stmt = this.db.createStatement( 'INSERT OR REPLACE INTO scriptvals (name, value) VALUES (:name, :value)'); try { stmt.params.name = name; stmt.params.value = JSON.stringify(val); stmt.execute(); } finally { stmt.reset(); } this._script.changed('val-set', name); }; GM_ScriptStorage.prototype.getValue = function(name, defVal) { var value = null; var stmt = this.db.createStatement( 'SELECT value FROM scriptvals WHERE name = :name'); try { stmt.params.name = name; while (stmt.step()) { value = stmt.row.value; } } catch (e) { dump('getValue err: ' + uneval(e) + '\n'); } finally { stmt.reset(); } if (value == null) return defVal; try { return JSON.parse(value); } catch (e) { dump('JSON parse error? ' + uneval(e) + '\n'); return defVal; } };stmt.params.value = JSON.stringify(val); return JSON.parse(value); |
|
|
2楼#
发布于:2014-09-01 10:40
为了更好的兼容性,自己用 JSON.stringify 转换下好了。
|
|