我正在尝试获取一个相当简单的脚本,该脚本保留一个值并在 GreaseMonkey: Scriptish 的后继者中检索它。
浏览器:火狐9.0
脚本化:0.1.7
在 Windows 7 Ultimate 64 位上
// ==UserScript==
// @id testSerialization
// @name Test Serialization
// @version 1.0
// @namespace com.mobilvox.com
// @author
// @description
// @include http://www.google.com/
// @run-at document-end
// ==/UserScript==
GM_setValue("isCurrent", true);
GM_log("Is this script current? " + GM_getValue("isCurrent", false));
当我运行它时,我得到:
[10:29:59.074] GM_setValue is not defined
@Scratchpad:29
@ Scratchpad:29
最佳答案
我有一个包含 jQuery 和 GM_ 代码的完整工作代码。试试这个:
// ==UserScript==
// @name GM_ debug script
// @description checking GM_setValue and GM_getValue with jQuery
// @namespace eaposztrof
// @include *
// @require http://code.jquery.com/jquery-1.9.1.js
// @grant GM_getValue // [grants][1]
// @grant GM_setValue
// ==/UserScript==
function GM_getValueUTF8(key) { // UTF8 safe
var value = GM_getValue(key);
return (value && value.length) ? decodeURI(value) : '';
}
function GM_setValueUTF8(key, value) {
GM_setValue(key, encodeURI(value));
}
GM_setValueUTF8('asd','GM_setValue, GM_getValue working! press [Alt+X] to check with jQuery');
alert(GM_getValueUTF8('asd'));
(function (){
this.$ = this.jQuery = jQuery.noConflict(true);
$(document).ready(function () {
$ (document).keydown (function (e) {
switch (e.keyCode) {
case 88: // "x"
if (e.altKey) {
setTimeout(function() { // [workaround][2]
GM_setValueUTF8('asd','GM_setValue, GM_getValue working inside jQuery');
alert(GM_getValueUTF8('asd'));
}, 0);
}
}
});
});
}
//
) ();
关于persistence - GM_setValue 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035472/