Page 1 of 1

Greasemonkey-scripts and jQuery

PostPosted: Sun Dec 19, 2010 8:00 am
by sherkaner
I enjoy the jQuery-library when I write javascript, so I've added that to BOB. But it slowly became harder to do. So I guess I should share that knowledge.

At first, I just loaded it by adding a <script>-tag to the page with jQuery, and wait till it came available. In the end, this doesn't work because it could take .1 or .2 seconds extra to load. It wasn't really noticable at the start, and it was 1 time per page, but still it's better to fetch it directly.

Then I found out about @Require, which loads the file along with the script when installing, and so it doesn't cost any extra time. So I was satisfied with the result (except that 1 person complained about fetching things from google, the script is hosted there etc.).

After that, 2 problems came up: jQuery 1.4 came out, which did not work fully in greasemonkey, and I tried to make the script work in Chrome, which does not support @Require.
The solution for the latter was to include the jQuery-code in the script itself. It's ugly, but happily it's not too large after minification (script is still smaller now than when I started), and it doesn't complicate anything after that.
The other was to adapt the jQuery code in a specific place.. I chose not to get stuck with an old version, so I get the code, make the modifications and minify it through google closure. With jQuery 1.4.4, there seem to be 3 places the code has to be adjusted now, and I'm wondering if the speedup that came with 1.4 and 1.4.2 is worth making the changes.. In time, I hope a better solution will show up.

Re: Greasemonkey-scripts and jQuery

PostPosted: Mon Dec 20, 2010 4:57 am
by Dako
I think you can include LABjs code into the script and load jQuery that way. After it has loaded - just call your BOB init function.

Here is the example code
Code: Select all
$LAB
   .script("jquery-1.4.4.min.js")
   .wait(function(){
      BOB.init();
   });


That will do the trick - I am most sure.

Re: Greasemonkey-scripts and jQuery

PostPosted: Mon Dec 20, 2010 10:40 am
by sherkaner
Yeah, I could do that. But I think the solution is pretty equal to my first variant. The downside (you have to wait till the thing is downloaded) is still true.
And the other disadvantage: If it adds it to the page (which is likely), Chrome won't have access to it.

Re: Greasemonkey-scripts and jQuery

PostPosted: Mon Dec 20, 2010 11:16 am
by Dako
It is better to add jQuery externally because it may be already cached by client and will result in a small speed up. Loading it inline with the BOB script just slows every bob-load.

LBjs should work at Chrome afaik but I never tried it there.

Re: Greasemonkey-scripts and jQuery

PostPosted: Mon Dec 20, 2010 3:27 pm
by sherkaner
Dako wrote:It is better to add jQuery externally because it may be already cached by client and will result in a small speed up. Loading it inline with the BOB script just slows every bob-load.

LBjs should work at Chrome afaik but I never tried it there.

I did try require.js once (work-related), but I still think it adds things to the page (http://requirejs.org/docs/why.html , just checked labjs and it does about the same).

And the case I have here is different. Because the script is already at the client, it takes no time to fetch it, execution time is all that matters. Hitting the cache would result in a small speedup compared to fetching it from a server. But still it should be slower than actually having it in the script already.

LABjs should work in chrome, but not in chrome extensions I think. Extensions normally allow you to add extra files anyway, so it will be hard to get information on that. Time to try something out I guess.

Re: Greasemonkey-scripts and jQuery

PostPosted: Tue Jul 05, 2011 8:35 pm
by ricaluanna
What is greasemonkey?

Re: Greasemonkey-scripts and jQuery

PostPosted: Wed Jul 06, 2011 3:33 pm
by sherkaner
Firefox-addon

'Customize the way a web page displays or behaves, by using small bits of JavaScript.'
Well, the scripts here aren't that small any more..