简体   繁体   English

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

[英]Greasemonkey functions don't work in my script?

I can't get my Greasemonkey script to work... 我不能让我的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(){

})();


Nothing happens with GM_registerMenuCommand . GM_registerMenuCommand没有任何GM_registerMenuCommand

I can throw an alert so I know the script is running, but how do I run GM_registerMenuCommand ? 我可以发出警报,因此我知道脚本正在运行,但如何运行GM_registerMenuCommand

As of version 2.0, Greasemonkey now defaults to @grant none . 从版本2.0开始,Greasemonkey现在默认为@grant none

You have to explicitly add @grant GM_xmlhttpRequest to the userscript metadata block, otherwise GM_xmlhttpRequest won't be available to your userscript. 您必须将@grant GM_xmlhttpRequest显式添加到userscript元数据块,否则GM_xmlhttpRequest将无法用于您的用户脚本。

// ==UserScript==
[...]
// @grant GM_registerMenuCommand
// @grant GM_xmlhttpRequest
// ==/UserScript==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM