[Greasemonkey] help with user script

Christopher Lam christopher.lck at gmail.com
Mon Aug 29 14:51:06 EDT 2005


I usually use javascript to get them links instead of XPath; I'm more used 
to them.
Plus I use getAttribute and setAttribute to retrieve the a.href and set the 
img.src.

How's that for an attempt?

// ==UserScript==
// @name Add icon to all links
// @namespace http://mywebsite.com/myscripts
// @description A template for creating new user scripts from
// @include *
// ==/UserScript==

(function() {
var links = document.getElementsByTagName('a');

for (var i=0; i < links.length; i++) {
a = links[i];
if (a.textContent=='Fx 1.0.6') {
imgsrc = a.getAttribute('href');
img = document.createElement('img');
img.setAttribute('src','http://website/img.php?url='+imgsrc);
a.parentNode.insertBefore(img, a.nextSibling);
}
}
})()

On 29/08/05, John Plsek <gm at plsek.id.au> wrote:
> 
> Tom Sonsuwan wrote:
> 
> >Thanks John,
> >
> >Great start.
> >
> >Is there any way to make the html of the links look like this:
> >normal: <a href=someurl>Some anchor</a>
> >new: <a href=someurl>Some anchor</a> - <img
> >src=image.php?url=someurl>
> >
> >
> >
> haven't tested this one
> 
> // ==UserScript==
> // @name anchorImage
> // @description Test
> // @include *
> // ==/UserScript==
> (function() {
> var baseImage="http://somewhere.com/image.php?url=";
> var xpath="//a[contains(text(), 'page')]";
> links =
> document.evaluate(xpath,document,null,
> XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
> for (var i = 0; i < links.snapshotLength; i++) {
> var node = links.snapshotItem(i);
> var parent = node.parentNode;
> var before = node.nextSibling;
> parent.insertBefore(document.createTextNode(" - "), before);
> var img=document.createElement("IMG");
> img.src=baseImage+escape(node.href); // to be sure????
> parent.insertBefore(img, before);
> }
> })();
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mozdev.org/pipermail/greasemonkey/attachments/20050829/6cf2ee1a/attachment.htm


More information about the Greasemonkey mailing list