Advertisement
Guest User

Code

a guest
Aug 7th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
  2. {
  3.   // the highlightStartTag and highlightEndTag parameters are optional
  4.   if ((!highlightStartTag) || (!highlightEndTag)) {
  5.     highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
  6.     highlightEndTag = "</font>";
  7.   }
  8.  
  9.   // find all occurences of the search term in the given text,
  10.   // and add some "highlight" tags to them (we're not using a
  11.   // regular expression search, because we want to filter out
  12.   // matches that occur within HTML tags and script blocks, so
  13.   // we have to do a little extra validation)
  14.   var newText = "";
  15.   var i = -1;
  16.   var lcSearchTerm = searchTerm.toLowerCase();
  17.   var lcBodyText = bodyText.toLowerCase();
  18.    
  19.   while (bodyText.length > 0) {
  20.     i = lcBodyText.indexOf(lcSearchTerm, i+1);
  21.     if (i < 0) {
  22.       newText += bodyText;
  23.       bodyText = "";
  24.     } else {
  25.       // skip anything inside an HTML tag
  26.       if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
  27.         // skip anything inside a <script> block
  28.         if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
  29.           newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
  30.           bodyText = bodyText.substr(i + searchTerm.length);
  31.           lcBodyText = bodyText.toLowerCase();
  32.           i = -1;
  33.         }
  34.       }
  35.     }
  36.   }
  37.   return newText;
  38. }
  39.  
  40.  
  41.  
  42. /*
  43.  * This is sort of a wrapper function to the doHighlight function.
  44.  * It takes the searchText that you pass, optionally splits it into
  45.  * separate words, and transforms the text on the current web page.
  46.  * Only the "searchText" parameter is required; all other parameters
  47.  * are optional and can be omitted.
  48. // */
  49. function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
  50. {
  51.   alert("I WORK 1 ");
  52.   // if the treatAsPhrase parameter is true, then we should search for
  53.   // the entire phrase that was entered; otherwise, we will split the
  54.   // search string so that each word is searched for and highlighted
  55.   // individually
  56.   if (treatAsPhrase) {
  57.     searchArray = [searchText];
  58.   } else {
  59.     searchArray = searchText.split(" ");
  60.   }
  61.  
  62.   if (!document.body || typeof(document.body.innerHTML) == "undefined") {
  63.     if (warnOnFailure) {
  64.       alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
  65.     }
  66.     return false;
  67.   }
  68.  
  69.   var bodyText = document.body.innerHTML;
  70.   alert(html1);
  71.   alert(titl);
  72.   for (var i = 0; i < searchArray.length; i++) {
  73.     bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  74.   }
  75.   alert("I WORK 1.1 ");
  76.   document.body.innerHTML = bodyText;
  77.   return true;
  78. }
  79.  
  80. function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
  81. {
  82.   // This function prompts the user for any words that should
  83.   // be highlighted on this web page
  84.   if (!defaultText) {
  85.     defaultText = "";
  86.   }
  87.  
  88.   // we can optionally use our own highlight tag values
  89.   /*if ((!textColor) || (!bgColor)) {
  90.     highlightStartTag = "";
  91.     highlightEndTag = "";
  92.   } else { */
  93.     highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
  94.     highlightEndTag = "</font>";
  95.  // }
  96.  
  97.   if (treatAsPhrase) {
  98.     promptText = "Please enter the phrase you'd like to search for:";
  99.   } else {
  100.     promptText = "Please enter the words you'd like to search for, separated by spaces:";
  101.   }
  102.  
  103.   searchText = prompt(promptText, defaultText);
  104.  
  105.   if (!searchText)  {
  106.     alert("No search terms were entered. Exiting function.");
  107.     return false;
  108.   }
  109.  
  110.   return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
  111. }
  112.  
  113.  
  114.  
  115.  
  116. document.addEventListener('DOMContentLoaded', function() {
  117.     var btn_search = document.getElementById('btn_search');
  118.     // onClick's logic below:
  119.     btn_search.addEventListener('click', function() {
  120.         highlightSearchTerms("web",true,true,"<font style='color:blue; background-color:yellow;'>","</font>");
  121.     });
  122. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement