[Greasemonkey] add prefetch attributes

Nikolas Coukouma lists at atrus.org
Mon May 16 13:55:57 EDT 2005


Leo Zelevinsky wrote:

>Thanks!
>
>The script is quite different than what I was envisioning though -
>what it seems to do is preload an image, after determining the image
>path.
>
>What I am thinking of doing is not handling any of the prefetching
>itself - let Firefox do it, but just to alter the settings on the link
>to add the prefetch attribute.
>
>I guess if that's hard to do for some reason I could try to use 
>GM_xmlhttpRequest instead.
>
>And the pages I'm preloading aren't images anyway, though certainly
>the idea of preloading the image is very useful too.
>
>Thanks again though - any help on altering an element in the way I
>want appreciated (I'm currently reading the 'Diving into greasemonkey'
>book - so maybe that will answer my questions as well.
>  
>
You can add " prefetch" to rel, but I doubt Firefox will act on it. The
page is already half-loaded by the time your script gets to it. Using
GM_xmlhttpRequest is the best way to go. Dive into Greasemonkey and the
Greasemonkey homepage have documentation for it. Basically, all you need
to do is:
GM_xmlhttpRequest({ url: "http://something", method: "get" });
Normally you'd set handlers for when the data arrives, but that doesn't
matter since you just want the request to be made.

As for modifying elements, you can usually do:
var elm = document.getElementById( "something" );
elm.attr = "value";

for example,
elm.rel += " prefetch";

The DOM way of doing this is using hasAttribute, getAttribute, setAttribute:
elm.hasAttribute( "rel" ); // does it have a rel attribute?
var str = elm.hasAttribute( "rel" ); // get the current value of rel
elm.setAttribute( "rel", str + " prefetch" ); // set the value of rel

There's a lot more. You can read the W3C's DOM spec, or check out XUL
Planet's reference
http://xulplanet.com/references/objref/

-Nikolas Coukouma

>On 5/16/05, Julien Couvreur <julien.couvreur at gmail.com> wrote:
>  
>
>>Sure, there is a script that does that already for the AppleGeeks site:
>>http://www.warwick.ac.uk/~esudbi/greasemonkeyscripts/applegeeks_preload.user.js
>>
>>Besides looking for links that say "next", you could also look for rel="next"
>>
>>Cheers,
>>Julien
>>
>>On 5/16/05, Leo Zelevinsky <leo.zelevinsky at gmail.com> wrote:
>>    
>>
>>>Hi all!
>>>
>>>I would like to take advantage of the prefetch feature of firefox on
>>>non-prefetch-aware webpages. Can I use Greasemonkey for this? I did
>>>some searches with no luck.
>>>
>>>It seems like it should be relatively straightforward so I'm wondering
>>>why I can't find anything. For instance, I'd want to add to a link
>>>that says "Next" the prefetch attribute automatically.
>>>
>>>Any pointers would be much appreciated. Thanks!
>>>



More information about the Greasemonkey mailing list