SearchWebView.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // We're using a global variable to store the number of occurrences
  2. var MyApp_SearchResultCount = 0;
  3. var scrollIndex = 0;
  4. var keywordNum;
  5. var clickTime = 0;
  6. var keywordBox = new Array();
  7. // helper function, recursively searches in elements and their child nodes
  8. function MyApp_HighlightAllOccurencesOfStringForElement(element,keyword) {
  9. if (element) {
  10. if (element.nodeType == 3) { // Text node
  11. while (true) {
  12. var value = element.nodeValue; // Search for keyword in text node
  13. var idx = value.toLowerCase().indexOf(keyword);
  14. if (idx < 0) break; // not found, abort
  15. var span = document.createElement("span");
  16. var text = document.createTextNode(value.substr(idx,keyword.length));
  17. span.appendChild(text);
  18. span.setAttribute("class","MyAppHighlight");
  19. span.style.backgroundColor="yellow";
  20. span.style.color="black";
  21. text = document.createTextNode(value.substr(idx+keyword.length));
  22. element.deleteData(idx, value.length - idx);
  23. var next = element.nextSibling;
  24. element.parentNode.insertBefore(span, next);
  25. element.parentNode.insertBefore(text, next);
  26. element = text;
  27. if(span.offsetTop == 0){
  28. return;
  29. }
  30. keywordBox.push(span);
  31. MyApp_SearchResultCount++; // update the counter
  32. }
  33. }
  34. else if (element.nodeType == 1) { // Element node
  35. if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
  36. for (var i=element.childNodes.length-1; i>=0; i--) {
  37. MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. function getPosition(e) {
  44. var t = e.offsetTop;
  45. var l = e.offsetLeft;
  46. var w = e.offsetWidth;
  47. var h = e.offsetHeight-1;
  48. while(e=e.offsetParent) {
  49. t+=e.offsetTop;
  50. l+=e.offsetLeft;
  51. }
  52. scrollTo(l,t-60);
  53. }
  54. // the main entry point to start the search
  55. function MyApp_HighlightAllOccurencesOfString(keyword, index) {
  56. MyApp_RemoveAllHighlights();
  57. clickTime = parseInt(index);
  58. keywordNum = 0;
  59. keywordBox = new Array();
  60. MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
  61. var arrLength = keywordBox.length;
  62. getPosition(keywordBox[arrLength-index]);
  63. return arrLength;
  64. }
  65. // helper function, recursively removes the highlights in elements and their childs
  66. function MyApp_RemoveAllHighlightsForElement(element) {
  67. if (element) {
  68. if (element.nodeType == 1) {
  69. if (element.getAttribute("class") == "MyAppHighlight") {
  70. var text = element.removeChild(element.firstChild);
  71. element.parentNode.insertBefore(text,element);
  72. element.parentNode.removeChild(element);
  73. return true;
  74. } else {
  75. var normalize = false;
  76. for (var i=element.childNodes.length-1; i>=0; i--) {
  77. if (MyApp_RemoveAllHighlightsForElement(element.childNodes[i])) {
  78. normalize = true;
  79. }
  80. }
  81. if (normalize) {
  82. element.normalize();
  83. }
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. // the main entry point to remove the highlights
  90. function MyApp_RemoveAllHighlights() {
  91. MyApp_SearchResultCount = 0;
  92. MyApp_RemoveAllHighlightsForElement(document.body);
  93. }