我的 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
?
最佳答案
从 2.0 版开始,Greasemonkey 现在默认为 @grant none
。
您必须显式添加 @grant GM_xmlhttpRequest
到用户脚本元数据 block ,否则 GM_xmlhttpRequest
将无法用于您的用户脚本。
// ==UserScript==
[...]
// @grant GM_registerMenuCommand
// @grant GM_xmlhttpRequest
// ==/UserScript==
关于javascript - Greasemonkey 函数在我的脚本中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249198/