Highlight Words Jquery Library

Insructions

目前为 2017-06-06 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/30368/199091/Highlight%20Words%20Jquery%20Library.js

  1. // ==UserScript==
  2. // @name Highlight Words Jquery Library
  3. // @namespace https://greasyfork.org/en/users/710-tjololo
  4. // @version 0.1
  5. // @description Insructions
  6. // @author Tjololo
  7. // @require http://code.jquery.com/jquery-latest.min.js
  8. // @grant GM_Log
  9. // ==/UserScript==
  10.  
  11. /*
  12.  
  13. highlight v3 !! Modified by Jon Raasch (http://jonraasch.com) to fix IE6 bug !!
  14.  
  15. Highlights arbitrary terms.
  16.  
  17. <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
  18.  
  19. MIT license.
  20.  
  21. Johann Burkard
  22. <http://johannburkard.de>
  23. <mailto:jb@eaio.com>
  24.  
  25. */
  26.  
  27. highlight = function(pat) {
  28. function innerHighlight(node, pat) {
  29. var skip = 0;
  30. if (node.nodeType == 3) {
  31. var pos = node.data.toUpperCase().indexOf(pat);
  32. if (pos >= 0) {
  33. var spannode = document.createElement('span');
  34. spannode.className = 'highlight';
  35. var middlebit = node.splitText(pos);
  36. var endbit = middlebit.splitText(pat.length);
  37. var middleclone = middlebit.cloneNode(true);
  38. spannode.appendChild(middleclone);
  39. middlebit.parentNode.replaceChild(spannode, middlebit);
  40. skip = 1;
  41. }
  42. }
  43. else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
  44. for (var i = 0; i < node.childNodes.length; ++i) {
  45. i += innerHighlight(node.childNodes[i], pat);
  46. }
  47. }
  48. return skip;
  49. }
  50. return this.each(function() {
  51. innerHighlight(this, pat.toUpperCase());
  52. });
  53. };
  54.  
  55. removeHighlight = function() {
  56. function newNormalize(node) {
  57. for (var i = 0, children = node.childNodes, nodeCount = children.length; i < nodeCount; i++) {
  58. var child = children[i];
  59. if (child.nodeType == 1) {
  60. newNormalize(child);
  61. continue;
  62. }
  63. if (child.nodeType != 3) { continue; }
  64. var next = child.nextSibling;
  65. if (next == null || next.nodeType != 3) { continue; }
  66. var combined_text = child.nodeValue + next.nodeValue;
  67. new_node = node.ownerDocument.createTextNode(combined_text);
  68. node.insertBefore(new_node, child);
  69. node.removeChild(child);
  70. node.removeChild(next);
  71. i--;
  72. nodeCount--;
  73. }
  74. }
  75.  
  76. return this.find("span.highlight").each(function() {
  77. var thisParent = this.parentNode;
  78. thisParent.replaceChild(this.firstChild, this);
  79. newNormalize(thisParent);
  80. }).end();
  81. };