开发者

Greasemonkey script, call functions from other page script

the page for which I 开发者_运维技巧am writing a greasemonkey script already includes jquery, so I would like to know if/how I can access the jquery functions already included rather than including jquery in my greasemonkey script.

in my attempts, the jquery works fine if I include it in my script. but I'm not finding functions that were included by the server page. I have no control of the server.

mostly just curious if I can access those functions.

worst case scenario I'll include jquery code in my script, so it's not a serious problem.


It's a bit hacky, but sometimes you can trigger functions on the page by creating an triggerfunction HTML element and insert it into the page. Then use Greasemonkey to click on the link when you want to trigger the function.

For example, imagine that there's a function on the page called "runGame()". Early in your Greasemonkey script you have:

 var triggerHTML = "<a id=\"triggerfunction\" onclick=\"runGame()\">triggerfunction</a>";

And you insert it into the page at a suitable point. For example, imagine that you've found a place in front of which it could sit nicely:

 target.parentNode.insertBefore(triggerHTML, target);

And then later when you want to run the function, you execute

 var evt = document.createEvent("MouseEvents");
 evt.initMouseEvent("click", true, true, window, 
            0, 0, 0, 0, 0, 
            false, false, false, false, 
            0, null);
 var triggerGame= document.getElementById("triggerfunction");
 triggerGame.dispatchEvent(evt);


You should call the page scripts using unsafeWindow, e.g. instead of $ you would write unsafeWindow.$.

BUT make sure you understand the safety threats arising from using unsafeWindow - they are decribed at http://wiki.greasespot.net/UnsafeWindow .

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜