JAVASCRIPT   52

doHighlight

Guest on 3rd May 2022 02:50:44 AM

  1. function doHighlight(bodyText, searchTerm)
  2. {
  3. if ((!highlightStartTag) || (!highlightEndTag)) {
  4. highlightStartTag = "<font style='color:black; background-color:yellow;'>";
  5. highlightEndTag = "</font>";
  6. }
  7. var newText = "";
  8. var i = -1;
  9. var lcSearchTerm = searchTerm.toLowerCase();
  10. var lcBodyText = bodyText.toLowerCase();
  11.  
  12. while (bodyText.length > 0) {
  13. i = lcBodyText.indexOf(lcSearchTerm, i+1);
  14. if (i < 0) {
  15. newText += bodyText;
  16. bodyText = "";
  17. } else {
  18. if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
  19. if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
  20. newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
  21. bodyText = bodyText.substr(i + searchTerm.length);
  22. lcBodyText = bodyText.toLowerCase();
  23. i = -1;
  24. }
  25. }
  26. }
  27. }
  28. return newText;
  29. }
  30. function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
  31. {
  32. if (treatAsPhrase) {
  33. searchArray = [searchText];
  34. } else {
  35. searchArray = searchText.split(" ");
  36. }
  37. if (!document.body || typeof(document.body.innerHTML) == "undefined") {
  38. if (warnOnFailure) {
  39. alert("Palavra indisponível."");
  40. }
  41. return false;
  42. }
  43. var bodyText = document.body.innerHTML;
  44. for (var i = 0; i < searchArray.length; i++) {
  45. bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  46. }
  47. document.body.innerHTML = bodyText;
  48. return true;
  49. }
  50. function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
  51. {
  52. if (!defaultText) {
  53. defaultText = ";
  54. }
  55. if ((!textColor) || (!bgColor)) {
  56. highlightStartTag = "";
  57. highlightEndTag = "";
  58. } else {
  59. highlightStartTag = ""<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";
  60. highlightEndTag = "</font>";
  61. }
  62. if (treatAsPhrase) {
  63. promptText = "Digite os termos que deseja destacar:";
  64. return false;
  65. } else {
  66. promptText = "Digite os termos que deseja destacar:";
  67. }
  68. searchText = prompt(promptText, defaultText);
  69.  
  70. if (!searchText) {
  71. alert("Nenhum termo foi solicitado.");
  72. return false;
  73. }
  74. return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
  75. }
  76. function highlightGoogleSearchTerms(referrer)
  77. {
  78. if (!referrer) {
  79. return false;
  80. }
  81. var queryPrefix = "q=";
  82. var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  83. if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
  84. return false;
  85. }
  86. var endPos = referrer.indexOf("&", startPos);
  87. if (endPos < 0) {
  88. endPos = referrer.length;
  89. }
  90. var queryString = referrer.substring(startPos + queryPrefix.length, endPos);
  91. queryString = queryString.replace(%20/gi,, " ");
  92. queryString = queryString.replace(\+/gi,, " ");
  93. queryString = queryString.replace(%22/gi,, ");
  94. queryString = queryString.replace(/\\/gi, "");
  95. return highlightSearchTerms(queryString, false);
  96. }
  97. function testHighlightGoogleSearchTerms()
  98. {
  99. var referrerString = ""http//www.google.com/search?q=javascript%20highlight&start=0";
  100.  
  101. referrerString = prompt("Test the following referrer string:", referrerString);
  102. return highlightGoogleSearchTerms(referrerString);
  103.  

Raw Paste


Login or Register to edit or fork this paste. It's free.