[英]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·斯塔徹的答案是正確的:
GM_setValue()
這樣存儲數組。 GM_setValue()
,並覆蓋了該函數! ( var GM_setValue = ...
)。 其他須知:
GM_setValue()
和GM_getValue()
對字符串以外的任何東西都做GM_getValue()
糟糕。 但是,幸運的是,存在一些實用程序可以糾正這些缺陷。 一個很好的是Super_GM_setValue_and_GM_getValue.js 。
要使用此功能,請將此行添加到腳本的元數據塊中 :
// @require http://userscripts-mirror.org/scripts/source/107941.user.js
確保在元數據塊中也@grant
GM_getValue
和GM_setValue
。
將代碼包裝在匿名函數EG中沒有意義:
(function() { ... })();
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.