|
1 | | -var DATA_TEXTCONTENT = 'data-dd-original-textcontent'; |
| 1 | +(function() { |
| 2 | + const DATA_TEXTCONTENT = 'data-dd-original-textcontent'; |
2 | 3 |
|
3 | | -// From: |
4 | | -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions |
5 | | -var escapeRegExp = function(string) { |
6 | | - return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string |
7 | | -} |
| 4 | + // From: |
| 5 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions |
| 6 | + const escapeRegExp = function(string) { |
| 7 | + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string |
| 8 | + } |
8 | 9 |
|
9 | | -var mutateDOM = function(mutations) { |
10 | | - return new Promise(function(resolve, _reject) { |
11 | | - requestAnimationFrame(function() { |
12 | | - mutations.forEach(function(mut) { |
13 | | - mut(); |
| 10 | + const mutateDOM = function(mutations) { |
| 11 | + return new Promise(function(resolve, _reject) { |
| 12 | + requestAnimationFrame(function() { |
| 13 | + mutations.forEach(function(mut) { |
| 14 | + mut(); |
| 15 | + }); |
| 16 | + resolve(true); |
14 | 17 | }); |
15 | | - resolve(true); |
16 | 18 | }); |
17 | | - }); |
18 | | -}; |
| 19 | + }; |
19 | 20 |
|
20 | | -var hasSearchTerm = function(string, term) { |
21 | | - return new RegExp(escapeRegExp(term), 'i').test(string); |
22 | | -}; |
| 21 | + const hasSearchTerm = function(string, term) { |
| 22 | + return new RegExp(escapeRegExp(term), 'i').test(string); |
| 23 | + }; |
23 | 24 |
|
24 | | -var highlightTermInNode = function(node, term) { |
25 | | - return function() { |
26 | | - let content = node.textContent; |
27 | | - if (node.hasAttribute(DATA_TEXTCONTENT)) { |
28 | | - content = node.getAttribute(DATA_TEXTCONTENT) |
29 | | - } else { |
30 | | - // Make backup of original textContent. |
31 | | - node.setAttribute(DATA_TEXTCONTENT, content); |
32 | | - } |
33 | | - const newContent = content.replace( |
34 | | - new RegExp(escapeRegExp(term), 'gi'), |
35 | | - function(match) { |
36 | | - return `<mark>${match}</mark>`; |
| 25 | + const highlightTermInNode = function(node, term) { |
| 26 | + return function() { |
| 27 | + let content = node.textContent; |
| 28 | + if (node.hasAttribute(DATA_TEXTCONTENT)) { |
| 29 | + content = node.getAttribute(DATA_TEXTCONTENT) |
| 30 | + } else { |
| 31 | + // Make backup of original textContent. |
| 32 | + node.setAttribute(DATA_TEXTCONTENT, content); |
37 | 33 | } |
38 | | - ); |
39 | | - node.innerHTML = newContent; |
| 34 | + const newContent = content.replace( |
| 35 | + new RegExp(escapeRegExp(term), 'gi'), |
| 36 | + (match) => `<mark>${match}</mark>` |
| 37 | + ); |
| 38 | + node.innerHTML = newContent; |
| 39 | + }; |
40 | 40 | }; |
41 | | -}; |
42 | 41 |
|
43 | | -var restoreNodeTextContent = function(node) { |
44 | | - return function() { |
45 | | - if (node.hasAttribute(DATA_TEXTCONTENT)) { |
46 | | - node.textContent = node.getAttribute(DATA_TEXTCONTENT); |
47 | | - node.removeAttribute(DATA_TEXTCONTENT); |
| 42 | + const restoreNodeTextContent = function(node) { |
| 43 | + return function() { |
| 44 | + if (node.hasAttribute(DATA_TEXTCONTENT)) { |
| 45 | + node.textContent = node.getAttribute(DATA_TEXTCONTENT); |
| 46 | + node.removeAttribute(DATA_TEXTCONTENT); |
| 47 | + } |
| 48 | + }; |
| 49 | + }; |
| 50 | + |
| 51 | + const reset = function() { |
| 52 | + var mainNode = document.querySelector('main[role="main"]'); |
| 53 | + var treeWalker = document.createTreeWalker(mainNode, NodeFilter.SHOW_ELEMENT, { |
| 54 | + acceptNode: function(node) { |
| 55 | + if (node.hasAttribute(DATA_TEXTCONTENT)) { |
| 56 | + return NodeFilter.FILTER_ACCEPT; |
| 57 | + } |
| 58 | + return NodeFilter.FILTER_SKIP; |
| 59 | + }, |
| 60 | + }); |
| 61 | + var domMutations = []; |
| 62 | + while (treeWalker.nextNode()) { |
| 63 | + domMutations.push(restoreNodeTextContent(treeWalker.currentNode)); |
48 | 64 | } |
| 65 | + return mutateDOM(domMutations); |
49 | 66 | }; |
50 | | -}; |
51 | 67 |
|
52 | | -var reset = function() { |
53 | | - var mainNode = document.querySelector('main[role="main"]'); |
54 | | - var treeWalker = document.createTreeWalker(mainNode, NodeFilter.SHOW_ELEMENT, { |
55 | | - acceptNode: function(node) { |
56 | | - if (node.hasAttribute(DATA_TEXTCONTENT)) { |
57 | | - return NodeFilter.FILTER_ACCEPT; |
58 | | - } |
59 | | - return NodeFilter.FILTER_SKIP; |
60 | | - }, |
61 | | - }); |
62 | | - var domMutations = []; |
63 | | - while (treeWalker.nextNode()) { |
64 | | - domMutations.push(restoreNodeTextContent(treeWalker.currentNode)); |
65 | | - } |
66 | | - return mutateDOM(domMutations); |
67 | | -}; |
| 68 | + const search = function(term) { |
| 69 | + var mainNode = document.querySelector('main[role="main"]'); |
| 70 | + var treeWalker = document.createTreeWalker(mainNode, NodeFilter.SHOW_TEXT, { |
| 71 | + acceptNode: function(node) { |
| 72 | + var parent = node.parentNode; |
| 73 | + // TODO switching docs doesn't invalidate the nodes. |
| 74 | + // MutationObserver to track? |
| 75 | + if (parent.tagName === 'MARK') { |
| 76 | + return NodeFilter.FILTER_REJECT; |
| 77 | + } |
| 78 | + let content = node.textContent; |
| 79 | + if (parent.hasAttribute(DATA_TEXTCONTENT)) { |
| 80 | + content = parent.getAttribute(DATA_TEXTCONTENT); |
| 81 | + } |
| 82 | + if (hasSearchTerm(content, term)) { |
| 83 | + return NodeFilter.FILTER_ACCEPT; |
| 84 | + } |
| 85 | + return NodeFilter.FILTER_SKIP; |
| 86 | + }, |
| 87 | + }); |
| 88 | + var domMutations = []; |
| 89 | + while (treeWalker.nextNode()) { |
| 90 | + domMutations.push(highlightTermInNode(treeWalker.currentNode.parentNode, term)); |
| 91 | + } |
| 92 | + return mutateDOM(domMutations); |
| 93 | + }; |
68 | 94 |
|
69 | | -var search = function(term) { |
70 | | - var mainNode = document.querySelector('main[role="main"]'); |
71 | | - var treeWalker = document.createTreeWalker(mainNode, NodeFilter.SHOW_TEXT, { |
72 | | - acceptNode: function(node) { |
73 | | - var parent = node.parentNode; |
74 | | - // TODO switching docs doesn't invalidate the nodes. |
75 | | - // MutationObserver to track? |
76 | | - if (parent.tagName === 'MARK') { |
77 | | - return NodeFilter.FILTER_REJECT; |
78 | | - } |
79 | | - let content = node.textContent; |
80 | | - if (parent.hasAttribute(DATA_TEXTCONTENT)) { |
81 | | - content = parent.getAttribute(DATA_TEXTCONTENT); |
82 | | - } |
83 | | - if (hasSearchTerm(content, term)) { |
84 | | - return NodeFilter.FILTER_ACCEPT; |
85 | | - } |
86 | | - return NodeFilter.FILTER_SKIP; |
87 | | - }, |
88 | | - }); |
89 | | - var domMutations = []; |
90 | | - while (treeWalker.nextNode()) { |
91 | | - domMutations.push(highlightTermInNode(treeWalker.currentNode.parentNode, term)); |
92 | | - } |
93 | | - return mutateDOM(domMutations); |
94 | | -}; |
| 95 | + window.search = function(term) { |
| 96 | + if (term === '' || typeof term !== 'string') { |
| 97 | + return; |
| 98 | + } |
| 99 | + return reset().then(() => search(term)); |
| 100 | + }; |
| 101 | + |
| 102 | + window.resetSearch = function() { |
| 103 | + return reset(); |
| 104 | + }; |
| 105 | +})(); |
95 | 106 |
|
0 commit comments