Calling Greasemonkey Functions from a Web Page

Is it possible to call the function () of my custom Greasemonkey from my page?

For instance,

I created a GM script containing the do_this () function. I want my-web-site.com to call the do_this () function. But I can not.

I know I can do unsafeWindow.do_this (), but that prevents me from calling GM_xmlhttpRequest ().

Any ideas?

+4
source share
4 answers

here is an example that works, first create an 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()) } 
+6
source

I had the same problem. You can find good information here on the wiki . I would suggest using a script to insert the necessary code into the document. Thus, it will work as in the source code of the page. You also cannot use GM_ functions, but you can use a combination of script injection (for example, to get a variable) and classic greasemonkey scripts with all GM_ functions (for example, you can use the variables you read and POST them with GM_xmlhttpRequest ()) .

In addition, methods such as script injection have several security advantages over unsafeWindow.

I hope this helps.

+3
source

I never used this myself, but here is a 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); }); 
+1
source

No, GM_ * functions are not available from the web page.

0
source

Source: https://habr.com/ru/post/1308441/


All Articles