persistence - GM_setValue 未定义

标签 persistence greasemonkey scriptish

我正在尝试获取一个相当简单的脚本,该脚本保留一个值并在 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/

相关文章:

java - 在 persistence.xml 中缓存区域配置

javascript - 当在多个选项卡上加载时,用户脚本会自行阻塞

javascript - 循环 GM_xmlhttpRequest 在变量上给出 "TypeError Null"

javascript - JS insideHTML.replace() 行为

javascript - 使用用户脚本设置 tabindex?

ios - 当一个持久存储为只读时,核心数据关系没有反转?

java - 读取 persistence.xml 文件中的环境变量

javascript - 如何只下载/读取文件的前 80KB?

javascript - 是否可以使用 node.js 作为 greasemonkey 的更快、更优雅、支持数据库的替代品?

html - 将 HTML 文档保存到磁盘