admin管理员组

文章数量:1287971

I am updating a Greasemonkey script which reads the name of the href in the list of ignored users in the ignore section of vBulletin.

I store the values in an array and then display:none to the td which will hide the ignored users thread from the message board.

The only way to do this is to visit the ignore list and store the array values in about:config. But I can't get the array to store there.

Here is the relevant part of the updated script:

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

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

    var url = ".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();
    }

I am updating a Greasemonkey script which reads the name of the href in the list of ignored users in the ignore section of vBulletin.

I store the values in an array and then display:none to the td which will hide the ignored users thread from the message board.

The only way to do this is to visit the ignore list and store the array values in about:config. But I can't get the array to store there.

Here is the relevant part of the updated script:

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

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

    var url = "http://www.site./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();
    }
Share Improve this question edited Sep 22, 2012 at 3:53 Brock Adams 93.6k23 gold badges241 silver badges305 bronze badges asked Sep 21, 2012 at 21:20 SOLDIER-OF-FORTUNESOLDIER-OF-FORTUNE 1,6545 gold badges39 silver badges68 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You want to convert the array to a string, JSON.stringify() is best for that.

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

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

This is assuming plonk is not an array of elements -- there is no hint as to what you are doing there.

Why are you overwriting GM_setValue? Leave that alone.

Jeremy J Starcher's answer is correct in that:

  1. You can't store arrays that way, using GM_setValue().
  2. The question code was using GM_setValue() incorrectly anyway, and overwriting the function! (var GM_setValue = ...).

Additional things to know:

  1. GM_setValue() and GM_getValue() do an abysmal job with anything other than strings. But, fortunately, several utilities exist to correct the deficiencies. A good one is Super_GM_setValue_and_GM_getValue.js.

    To use this, add this line to your script's metadata block:

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


  2. Be sure you also @grant GM_getValue and GM_setValue in your metadata block.

  3. There is no point in wrapping the code in an anonymous function, EG:

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


  4. Use window.location.href, not window.location.


Putting it all together, that code snippet would be like:

// @require http://ajax.googleapis./ajax/libs/jquery/1.7.2/jquery.min.js
// @require http://userscripts-mirror/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./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);
}

本文标签: javascriptHow do I store an array in Firefox from a Greasemonkey scriptStack Overflow