繁体   English   中英

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

[英]How do I store an array in Firefox from a Greasemonkey script?

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

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

唯一的方法是访问忽略列表并将数组值存储在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();
    }

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

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

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

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

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

杰里米·J·斯塔彻的答案是正确的:

  1. 您不能使用GM_setValue()这样存储数组。
  2. 问题代码仍然错误地使用了GM_setValue() ,并覆盖了该函数! var GM_setValue = ... )。

其他须知:

  1. GM_setValue()GM_getValue()对字符串以外的任何东西都做GM_getValue()糟糕。 但是,幸运的是,存在一些实用程序可以纠正这些缺陷。 一个很好的是Super_GM_setValue_and_GM_getValue.js

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

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


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

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

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


  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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM