While I am new to both Greasemonkey and jQuery (both are dripping with
amazing potential), it is true that
onclick='AlertBox()'
will not work because this is being evaluated after Greasemonkey has
disappeared out of sight. Karl's remedy should have worked since that
is referencing the actual function itself. That continues to exist
even after the name has ceased (assuming something is referencing
it).
The disappearance is a result of some very important security
precautions, as I understand it. So it may be dangerous to try to get
around them. If not done right, the Greasemonkey paradigm can lead to
any website that you visit actually compromising your entire file
system.
I have listed below a fully functional Greasemonkey script though it
needs a host to run to load jQuery (say XAMPP on localhost). I could
not get 6061 working which would be a nice standalone solution. The
solution below I found awhile ago from
http://joanpiedra.com/jquery/greasemonkey/though I could only obtain it via the cache of Google. I also added
Karl's code in it at the end and it worked fine.
//
//
--------------------------------------------------------------------
//
// ==UserScript==
// @name jquery lib
// @namespace
http://jostylr.com/// @description Starting
// @include *
// @exclude
// ==/UserScript==
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = '
http://localhost/path/to/jquery.js'; GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
// Check if jQuery's loaded
function GM_wait() {
if(typeof unsafeWindow.jQuery == 'undefined')
{ window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();
// All your GM code must be inside this function
function letsJQuery() {
//make sure there is no conflict between jQuery and other libraries
$.noConflict()
//notify that jQuery is running...
$('<div>jQuery is running!</div>')
.css({padding: '10px', background: '#ffc', position:
'absolute',top: '0', width: '100%'})
.prependTo('body')
.fadeIn('fast')
.animate({opacity: 1.0}, 300)
.fadeOut('fast', function() {
$(this).remove();
});
$('<input type="button" name="popupButton" value="popUp">').prependTo
('body').click(function() {
alert("Popup from userscript function!");
});
}