Greasemonkey函数在我的脚本中不起作用?

时间:2013-01-10 01:02:21

标签: javascript greasemonkey

我无法让我的Greasemonkey脚本工作......

GM_registerMenuCommand("What's My IP Address?", function(){

GM_xmlhttpRequest({
    method: "GET",
    url: "http://tools.ip2location.com/ib2",
    onerror: function(oEvent){ alert("Error " + oEvent.target.status + " occurred while receiving the document."); },
    onload: function(response){
        if (response.readyState !== 4 || response.status !== 200) return;
        // we can parse now
        var myregexp = /<a[^>]*>([\s\S]*?(?:Your IP Address)[\s\S]*?)<\/a>/i;
        var match = myregexp.exec(response.responseText);
        if (match != null) {
            // got match
            subject = match[1];
            // format first line
            subject_2 = subject.replace(/<br><b>/mg, " ");
            // remove html
            subject_3 = subject_2.replace(/<\/?[a-z][a-z0-9]*[^<>]*>|<!--[\s\S]*?-->/ig, "");
            // now remove whitespaces
            result = subject_3.replace(/^[ \s]*/mg, "");
        } else {
            // no match, error
            result = "I couldn't find your IP Address :(";
        }
        alert(result);
    }
});

});

(function(){

})();


GM_registerMenuCommand没有任何结果。

我可以发出警报,因此我知道脚本正在运行,但如何运行GM_registerMenuCommand

1 个答案:

答案 0 :(得分:8)

从版本2.0开始,Greasemonkey现在默认为@grant none

您必须向用户脚本元数据块明确添加@grant GM_xmlhttpRequest,否则您的用户将无法使用GM_xmlhttpRequest

// ==UserScript==
[...]
// @grant GM_registerMenuCommand
// @grant GM_xmlhttpRequest
// ==/UserScript==
相关问题