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

SOL*_*UNE 0 javascript arrays firefox jquery greasemonkey

我正在更新一个Greasemonkey脚本,该脚本hrefvBulletin的ignore部分中读取被忽略用户列表中的名称.

我将值存储在一个数组中,然后display:nonetd其隐藏在消息板中的被忽略的用户线程中.

执行此操作的唯一方法是访问忽略列表并将数组值存储在其中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();
    }
Run Code Online (Sandbox Code Playgroud)

Jer*_*her 7

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

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

var b = JSON.parse(GM_getValue("key"));
Run Code Online (Sandbox Code Playgroud)

这假设plonk不是一个元素数组 - 没有关于你在那里做什么的暗示.

你为什么要覆盖GM_setValue?别管它了.


Bro*_*ams 6

Jeremy J Starcher 的答案是正确的:

  1. 您不能使用GM_setValue().
  2. 无论如何,问题代码使用GM_setValue()不正确,并且覆盖了该函数!( var GM_setValue = ...)。

其他需要了解的事情:

  1. GM_setValue()GM_getValue()除了弦之外,其他任何东西都做得很糟糕。但幸运的是,有几个实用程序可以纠正这些缺陷。Super_GM_setValue_and_GM_getValue.js是一个不错的选择。

    要使用它,请将此行添加到脚本的元数据块中:

    // @require http://userscripts-mirror.org/scripts/source/107941.user.js
    
    Run Code Online (Sandbox Code Playgroud)


  2. 确保您也在@grant GM_getValueGM_setValue数据块中。

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

    (function() {
        ...
    })();
    
    Run Code Online (Sandbox Code Playgroud)


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


将它们放在一起,该代码片段将类似于:

// @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);
}
Run Code Online (Sandbox Code Playgroud)