Posted By

wizard04 on 07/31/08


Tagged

javascript selection bookmarklet


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SpinZ
wirenaught


Open Selected Link Bookmarklet


 / Published in: JavaScript
 

When there's a URL on a page that's not a link, just highlight it and click this bookmarklet. The URL will open in a new window.

If there are multiple selections (e.g., if you've selected something in each of two frames on the page), they will each be opened in a new window.

[Open Link](javascript:(function(){function T(f){try{if(f.getSelection)return f.getSelection().toString();if(f.document.selection)return f.document.selection.createRange().text}catch(e){}return ""}function A(f){var a=[];var t=T(f);if(t)a.push(t.replace(/^\s+|\s+$/g,""));for(var i=0;i<f.length;i++){a=a.concat(A(f[i]))}return a}var a=A(window.top);for(var i=0;i<a.length;i++){if(!/^https?:\/\//.test(a[i])) a[i]="http://"+a[i];window.open(a[i])}})() "Drag this into your bookmarks!")

  1. javascript:(function(){function T(f){try{if(f.getSelection)return f.getSelection().toString();if(f.document.selection)return f.document.selection.createRange().text}catch(e){}return ''}function A(f){var a=[];var t=T(f);if(t)a.push(t.replace(/^\s+|\s+$/g,''));for(var i=0;i<f.length;i++){a=a.concat(A(f[i]))}return a}var a=A(window.top);for(var i=0;i<a.length;i++){if(!/^https?:\/\//.test(a[i])) a[i]="http://"+a[i];window.open(a[i])}})();
  2.  
  3. //Here it is expanded so you can read it:
  4. javascript:(function(){
  5. function T(f) //get the selected text in frame f
  6. {
  7. try{
  8. if(f.getSelection) return f.getSelection().toString();
  9. if(f.document.selection) return f.document.selection.createRange().text;
  10. }catch(e){} //if the frame is in another domain
  11. return '';
  12. }
  13. function A(f) //get the selected text in frame f and its children
  14. {
  15. var a = [];
  16. var t = T(f);
  17. if(t)
  18. {
  19. a.push(t.replace(/^\s+|\s+$/g, ''));
  20. }
  21. for(var i=0; i<f.length; i++) //for each child frame
  22. {
  23. a = a.concat(A(f[i]));
  24. }
  25. return a;
  26. }
  27. var a = A(window.top);
  28. for(var i=0; i<a.length; i++)
  29. {
  30. if(!/^https?:\/\//.test(a[i])) a[i] = "http://"+a[i];
  31. window.open(a[i]);
  32. }
  33. })();

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on August 12, 2008

Fixed a bug. Needed to add a try...catch statement in case a frame does not belong to the current domain.

Posted By: the_coder on January 16, 2010

Line 23 needs to be: a=a.concat(A(f[i]));

Thanks for this. I will be using it to improve several of my bookmarklets.

Posted By: wizard04 on January 19, 2010

Thanks for catching that; I fixed it.

You need to login to post a comment.