如何从Greasemonkey脚本在Firefox中存储数组?

时间:2012-09-21 21:20:42

标签: javascript jquery arrays firefox greasemonkey

我正在更新a Greasemonkey script which reads the name of the href in the list of ignored users in the ignore section of vBulletin

我将值存储在数组中,然后将display:none存储到td,这将隐藏来自留言板的被忽略的用户线程。

执行此操作的唯一方法是访问忽略列表并将数组值存储在about:config中。但我无法将数组存储在那里。

以下是更新脚本的相关部分:

// @grant          GM_setValue 
// ==/UserScript==

(function() {
    var allT; 
    var allR;
    var plonk = new Array(); 
    var ignore_threads_from = GM_setValue;

    var url = "http://www.site.com/forums/profile.php?do=ignorelist"; //use for iggy list URL
    var currentURL = window.location;

    if (url == currentURL) {
        var GM_setValue = $('#ignorelist.userlist li a').map(function() {
              return $(this).text();
        }).get();
    }

2 个答案:

答案 0 :(得分:7)

您希望将数组转换为字符串,JSON.stringify()最适合。

var a = [1, 2, 3];
GM_setValue("key", JSON.stringify(a));

var b = JSON.parse(GM_getValue("key"));

这假设plonk不是元素数组 - 没有提示你在那里做什么。

为什么要覆盖GM_setValue?别这么说了。

答案 1 :(得分:4)

Jeremy J Starcher的回答是正确的:

  1. 您无法使用GM_setValue()
  2. 以这种方式存储数组
  3. 问题代码无论如何都错误地使用GM_setValue(),并覆盖了该功能! (var GM_setValue = ...)。
  4. 要了解的其他事项:

    1. GM_setValue()GM_getValue()使用除字符串以外的任何内容做一个糟糕的工作。但是,幸运的是,存在一些用于纠正缺陷的实用程序。一个好的是Super_GM_setValue_and_GM_getValue.js

      要使用此功能,请将此行添加到脚本的metadata block

      // @require http://userscripts-mirror.org/scripts/source/107941.user.js
      


    2. 请确保您的元数据块中还有@grant GM_getValueGM_setValue

    3. 将代码包装在匿名函数中是没有意义的,EG:

      (function() {
          ...
      })();
      


    4. 使用window.location.href,而不是window.location

    5. 将所有内容放在一起,该代码段就像:

      // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
      // @require http://userscripts-mirror.org/scripts/source/107941.user.js
      // @grant   GM_setValue 
      // @grant   GM_getValue 
      // ==/UserScript==
      
      var allT; 
      var allR;
      var plonk               = new Array(); 
      var ignore_threads_from = GM_SuperValue.get ("IgnoredUsers", []);
      
      var url         = "http://www.example.com/forums/profile.php?do=ignorelist"; //use for iggy list URL
      var currentURL  = window.location.href;
      
      if (url == currentURL) {
          var ignoreList  = $('#ignorelist.userlist li a').map (function () {
                return $(this).text();
          } ).get ();
      
          GM_SuperValue.set ("IgnoredUsers", ignoreList);
      }