开发者

calling Greasemonkey functions from web page [duplicate]

This question already has an answer here: 开发者_如何学运维 How to call Greasemonkey's GM_ functions from code that must run in the target page scope? (1 answer) Closed 8 years ago.

Can I call function() of my custom Greasemonkey from my page?

For example,

I created a GM script that contains do_this() function. I want my-web-site.com call the do_this() function. But I can't.

I know, I can by doing unsafeWindow.do_this() but doing so prevents me from calling GM_xmlhttpRequest().

Any ideas?


here the example that working, first create element then addEventListener

// ==UserScript==
// @name           GM addEventListener Function Test
// @namespace      ewwink.com
// @description    GM addEventListener Function Test
// @include        http://*
// ==/UserScript==

document.body.innerHTML+='<input  type="image" id="alertMeID" onclick="do_this()" style="position:fixed;top:0;left:0" src="http://i55.tinypic.com/2nly5wz.gif" />';

document.getElementById('alertMeID').addEventListener('click', do_this, false);

function do_this(){
  alert('hello World!, today is: '+new Date())
}


I just had the same Problem. You can find good information here in the wiki. I would suggest to use script injection to insert the needed code into the document. That way it will run like its in the sourcecode of the page. You can't use GM_ functions there either but you can use a combination of script injection (to retrieve a variable for example) and classic greasemonkey scripting with all the GM_ functions (For example you could use the variables you read and POST them with GM_xmlhttpRequest()).

Furthermore, techniques like script injection have several security-related advantages over unsafeWindow.

I hope that helps.


Never used this myself but here is the workaround http://wiki.greasespot.net/0.7.20080121.0%2B_compatibility.

unsafeWindow.someObject.registerCallback(function() {
    var value = "bar";
    setTimeout(function() {
    GM_setValue("foo", value);
    }, 0);
});


No, GM_* functions are not accessible from webpage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜