xelnaga
千年狐狸
千年狐狸
  • UID1911
  • 注册日期2005-01-08
  • 最后登录2022-04-20
  • 发帖数1725
  • 经验85枚
  • 威望0点
  • 贡献值8点
  • 好评度5点
  • 社区居民
阅读:2054回复:2

GM_setValue是不是可以存列表了?

楼主#
更多 发布于:2014-08-31 16:07
http://wiki.greasespot.net/GM_setValue
这里说它只能存字符串、布尔和整数

但我用setvalue存了个列表,console.log一看getvalue取回的是object array

wiki太旧了?
aaaa007cn
千年狐狸
千年狐狸
  • UID23968
  • 注册日期2008-05-03
  • 最后登录2022-03-07
  • 发帖数1924
  • 经验1138枚
  • 威望1点
  • 贡献值232点
  • 好评度164点
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);
hzzhaiqi
火狐狸
火狐狸
  • UID30112
  • 注册日期2009-08-18
  • 最后登录2015-09-15
  • 发帖数279
  • 经验180枚
  • 威望1点
  • 贡献值44点
  • 好评度28点
2楼#
发布于:2014-09-01 10:40
为了更好的兼容性,自己用 JSON.stringify 转换下好了。
游客

返回顶部