Modify bookmarklet to add URL?

D

David Winter

Hello guys,

can someone help a humble user with modifying a bookmarklet?

Jesse Ruderman has this bookmarklet at
https://www.squarefree.com/bookmarklets/pagelinks.html#hrefs_as_link_text:

javascript:
(function()
{var i,c,x,h; for(i=0;x=document.links;++i)
{ h=x.href; x.title+=" " + x.innerHTML;
while(c=x.firstChild)x.removeChild(c);
x.appendChild(document.createTextNode(h));
} })()

It replaces all links on a page with their full URLs:
"Click here." becomes "Click http://www.foo.com."

Very useful when you copy a web page to a non-hypertext-aware target
format such as plain text.

Now how do I modify this so it *adds* the URL in brackets right after
the link instead of *replacing* it?
("Click here (http://www.foo.com).")

I thought that removing the "x.removeChild(c);" should do it, but that
only makes the browser crash.

I could not code my way out of a paper bag, so I'd appreciate some
help here. :)

Thank you.
 
T

Thomas 'PointedEars' Lahn

David said:
can someone help a humble user with modifying a bookmarklet?

Jesse Ruderman has this bookmarklet at
https://www.squarefree.com/bookmarklets/pagelinks.html#hrefs_as_link_text:
[...]
javascript:
(function()
{var i,c,x,h; for(i=0;x=document.links;++i)
{ h=x.href; x.title+=" " + x.innerHTML;
while(c=x.firstChild)x.removeChild(c);
x.appendChild(document.createTextNode(h));
} })()


Pretty-printed, that is:

javascript:
(function()
{
var i, c, x, h;
for (i = 0; x = document.links; ++i)
{
h = x.href;
x.title += " " + x.innerHTML;

while ((c = x.firstChild))
x.removeChild(c);

x.appendChild(document.createTextNode(h));
}
})()

It could be rewritten as follows:

javascript:
(function()
{
for (var i = 0, x; x = document.links; ++i)
{
x.title += " " + x.innerHTML;

var c;
while ((c = x.firstChild))
x.removeChild(c);

x.appendChild(document.createTextNode(x.href));
}
})()

As bookmarklet (thanks to Firebug' "Larger Command Line" Copy button, watch
for newline):

javascript:(function(){for(var i=0,x;x=document.links;++i){
x.title+=" "+ x.innerHTML;var c;while((c = x.firstChild))x.removeChild(c);
x.appendChild(document.createTextNode(x.href));}})()
Now how do I modify this so it *adds* the URL in brackets right after
the link instead of *replacing* it?
("Click here (http://www.foo.com).")

javascript:
(
function()
{
for (var i = 0, x; x = document.links; ++i)
{
x.parentNode.insertBefore(
document.createTextNode(" (" + x.href + ")"),
x.nextSibling);
}
}
)()

As bookmarklet:

javascript:(function(){for(var i=0,x;x=document.links;++i)
x.parentNode.insertBefore(document.createTextNode(" ("+x.href+")"),
x.nextSibling);})()


Informing Jesse so this can be of general benefit.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top