Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using an extension for Firefox or Google Chrome, how can I modify all links on a page such that

<a href="www.google.com">Google</a>

becomes

<a href="http://www.google.com">http://www.google.com</a>

?

EDIT

Tampermonkey is Greasemonkey alternative for Google Chrome.

share|improve this question
    
It needs to be an extension? –  soandos May 21 '12 at 20:34
    
Does not matter. You can suggest different approach. –  Kerberos May 21 '12 at 20:39
    
Why do you close the opening tag right away <a … /> </a>? Where did you get that HTML syntax from? –  slhck May 21 '12 at 22:09
    
@slhck I've cleaned up the question. :) –  iglvzx May 22 '12 at 2:27
    
@slhck thank you very much for cleaned up the question. –  Kerberos May 22 '12 at 7:59
add comment

2 Answers

up vote 7 down vote accepted

I wrote you a simple Greasemonkey userscript to replace
<a href="URL">TEXT</a> with <a href="ABSOLUTE_URL">ABSOLUTE_URL</a>:

// ==UserScript==
// @name        Replace Link Text with URL
// @namespace   http://igalvez.net
// @description Replaces <a href="URL">TEXT</a> with <a href="ABSOLUTE_URL">ABSOLUTE_URL</a>
// @version     1.0
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

$("a").each(function() {
    var url = this.href;
    $(this).attr('href', url);
    $(this).text(url);       
});

Note: This will break most page layouts, as URL's can be quite long.

example

share|improve this answer
1  
Crazy. Awesome. Crazy awesome! –  nhinkle May 22 '12 at 2:27
    
@nhinkle As Spiderman once said, "With 1337 skillz comes great responsibility." –  iglvzx May 22 '12 at 2:29
1  
As Abraham Lincoln once said, "The Internet is an amazing thing." –  nhinkle May 22 '12 at 3:30
    
This might be better as a bookmarklet - click to reveal URLs. That should keep page layouts intact until needed. –  Bob May 22 '12 at 6:51
    
You can quickly enable/disable the script with a click, if the Greasemonkey icon/menu is on a toolbar. –  iglvzx May 22 '12 at 7:07
show 3 more comments

And here's a bookmarklet version based off iglvzx's answer. The advantage of a bookmarklet is the page will look normal until the button (link) is pressed. As iglvzx noted, URLs tend to be long and break page layouts.Of course his Greasemonkey version is possible to enable permanently on a per page basis. The main difference is Greasemonkey is designed to be on most of the time, while a bookmarklet is off most of the time. It's browser agnostic, as long as jQuery and JavaScript bookmarklets work.

javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){$("a").each(function(){var%20a=this.href;$(this).attr("href",a);$(this).text(a)});});

Made using this tool.

You can add as a bookmarklet directly with the link available at the following link:

http://benalman.com/code/test/jquery-run-code-bookmarklet/?name=Run+jQuery+Code&ver=1.3.2&code=%24%28%22a%22%29.each%28function%28%29{var+a%3Dthis.href%3B%24%28this%29.attr%28%22href%22%2Ca%29%3B%24%28this%29.text%28a%29}%29%3B

Just copy and paste into the address bar. Adding as an actual link seems to break something. As both Markdown and HTML, too!

share|improve this answer
    
Nice. Good idea. :) –  iglvzx May 22 '12 at 7:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.