Advertisement
Guest User

hilite.js

a guest
Jun 27th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Highlight Words
  3. // @author      bpm
  4. // @version     1.1
  5. // @description     Highlights works from an array
  6. // @include     https://*
  7. // @include     http://*
  8. // ==/UserScript==
  9. // 1.1 Updated by Frenzie, added easy to configure wordlist
  10. (function () {
  11.     var wordlist=[
  12.         /*@Wordlist (quote properly and use commas between values)@string@*/
  13.  
  14. "and",
  15. "und",
  16. "or",
  17. "oder",
  18.  
  19. /*@*/
  20.     ];
  21.     // other config
  22.     var highlightBgColor = /*@Highlight background color@string@*/'blue'/*@*/;
  23.     var highlightColor = /*@Highlight font color@string@*/'yellow'/*@*/;
  24.  
  25.     function hilite(text){
  26.         var count=0,dv;
  27.         dv=document.defaultView;
  28.         function searchWithinNode(node,te,len){
  29.             var pos,skip,spannode,middlebit,endbit,middleclone;
  30.             skip=0;
  31.             if(node.nodeType==3){
  32.                 pos=node.data.toUpperCase().indexOf(te);
  33.                 if(pos>=0){
  34.                     spannode=document.createElement('SPAN');
  35.                     spannode.style.backgroundColor=highlightBgColor;
  36.                     spannode.style.color=highlightColor;
  37.                     spannode.style.fontWeight='bold';
  38.                     middlebit=node.splitText(pos);
  39.                     endbit=middlebit.splitText(len);
  40.                     middleclone=middlebit.cloneNode(true);
  41.                     spannode.appendChild(middleclone);
  42.                     middlebit.parentNode.replaceChild(spannode,middlebit);
  43.                     ++count;
  44.                     skip=1;
  45.                 }
  46.             }
  47.             else if(node.nodeType==1&& node.childNodes && node.tagName.toUpperCase()!='SCRIPT' && node.tagName.toUpperCase!='STYLE'){
  48.                 for (var child=0;child<node.childNodes.length;++child){
  49.                     child=child+searchWithinNode(node.childNodes[child],te,len);
  50.                 }
  51.             }
  52.         return skip;
  53.         }
  54.         searchWithinNode(document.body,text.toUpperCase(),text.length);
  55.     }
  56.  
  57.     document.addEventListener('DOMContentLoaded',function() {
  58.         for (i=0; i<wordlist.length; i++) {
  59.             hilite(wordlist[i]);
  60.         }
  61.     }, false);
  62. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement