Posted By

wizard04 on 07/25/08


Tagged

javascript selection define bookmarklet dictionary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

c0rsana


Ninjawords Bookmarklet


 / Published in: JavaScript
 

Select some text, click the bookmarklet, and the definition(s) will open up in Ninjawords.

[Ninjawords](javascript:(function(){function%20t(f){try{if(f.getSelection)return%20f.getSelection().toString();if(f.document.selection)return%20f.document.selection.createRange().text}catch(e){}return%20''}function%20getQ(f){var%20q=t(f).replace(/^[^a-z0-9-]+|[^a-z0-9-]+$/ig,'').replace(/[^a-z0-9-]+/ig,%20',');var%20s='';for(var%20i=0;i<f.length;i++){s=getQ(f[i]);if(s)q+=(q?',':'')+s}return%20q}if(window.location.hostname!='ninjawords.com')window.open('http://ninjawords.com/'+getQ(window.top));else{document.getElementById('q').value%20=%20getQ(window.top);Ninjawords.textboxQuery()}})() "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 getQ(f){var q=t(f).replace(/^[^a-z0-9-]+|[^a-z0-9-]+$/ig,'').replace(/[^a-z0-9-]+/ig, ',');var s='';for(var i=0;i<f.length;i++){s=getQ(f[i]);if(s)q+=(q?',':'')+s}return q}if(window.location.hostname!='ninjawords.com')window.open('http://ninjawords.com/'+getQ(window.top));else{document.getElementById('q').value = getQ(window.top);Ninjawords.textboxQuery()}})()
  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 getQ(f) //get the selected text in frame f and its children
  14. {
  15. //get rid of non-alphanumeric characters and separate words with commas
  16. var q = t(f).replace(/^[^a-z0-9-]+|[^a-z0-9-]+$/ig,'').replace(/[^a-z0-9-]+/ig, ',');
  17. var s = '';
  18. for(var i=0; i<f.length; i++) //for each child frame
  19. {
  20. s = getQ(f[i]);
  21. if(s) q += (q?',':'')+s;
  22. }
  23. return q;
  24. }
  25. if(window.location.hostname != 'ninjawords.com') //if not already at ninjawords.com
  26. window.open('http://ninjawords.com/'+getQ(window.top));
  27. else
  28. {
  29. document.getElementById('q').value = getQ(window.top);
  30. Ninjawords.textboxQuery();
  31. }
  32. })()

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: wizard04 on November 5, 2009

Updated so that it stays on the Ninjawords page if you're already there, instead of opening a new one.

You need to login to post a comment.