Reputation: 9621
From earlier SO post i found that for Greasemonkey script to be compatible with Chrome we have to create script element for jQuery http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script
If my current userscript which works fine in firefox is as follows. How should I modify it to include the above method and still call the function on onload? Where do I place the current code in the solution suggested in earlier posts?
// ==UserScript==
// @name Google+
// @version 1.1
//
//
// @include http://plus.google.com/*
// @include https://plus.google.com/*
//
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
$(function()
{
$(window).bind('load', function()
{
console.log("Hi Condition is very bad!")
$('<div id="vdiv"></div>').prependTo('#contentPane');
$('<img>',
{
src: 'http://icons.iconarchive.com/icons/aha-soft/security/16/key-icon.png',
alt: 'Key',
title:'Key',
click: function(){
alert($(this).attr('title'));
var vtext = jQuery('div.f-ba-mg iframe').contents().find('body').text().trim();
alert(vtext);
}
})
.css({
cursor: 'pointer',
border: '1px solid black',
backgroundColor: 'white'
})
.appendTo('#vdiv');
});
});
Upvotes: 1
Views: 590
Reputation: 3212
Ehm, you can be pretty sure the script executes after the ondomready-event, that's greasemonkey's normal execution time (chrome offers an earlier execution time, but that's just a special option). So there's no need to run it at onload. So you can actually put it anywhere, as long as you load jQuery beforehand.
The solution by Erik would work by making 1 init-function (what would normally run at .ready()), and use addJquery(init-function);
Btw, my method for including jQuery is adding it into the script itself, since otherwise it has to be loaded for every page (which is a slowdown), so I have a minified version of jQuery in the script and some other solutions, see http://userscripts.org/scripts/review/52341 (btw, I came in as a maintainer, the code is far from perfect).
Solutions there:
1.) there are some comment lines on top of jQuery, followed by an adapted and modified version of jQuery 1.4.4 (since there are slight differences between the greasemonkey environment and the normal document scope).
2.) Just above those lines, I'm using localStorage to recreate some of greasemonkey's functions on Chrome.
3.) unsafeWindow in greasemonkey is about equal to window in chrome, but only for functions the browser itself offers (others are blocked by chrome, since they are unsafe). Not using a lot, only unsafeWindow.console.log() and some less important thingies afaik.
But for your case, erik's method works too:
$(function(){
$(window).bind('load', function(){
...
});
});
to
addJquery(function() {
...
});
Upvotes: 1