Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling Greasemonkey functions from web page [duplicate]

Tags:

greasemonkey

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?

like image 897
well son Avatar asked Dec 22 '22 03:12

well son


1 Answers

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())
}
like image 95
ewwink Avatar answered Apr 01 '23 21:04

ewwink