Ninjawords Bookmarklet


/ Published in: JavaScript
Save to your folder(s)

Select some text, click the bookmarklet, and the definition(s) will open up in [Ninjawords](http://www.ninjawords.com).

[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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.