Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function searchPage(topics) {
- highlightAllWords(topics);
- }
- var highlightAllWords = function(topics) {
- Object.keys(topics.topics).forEach(function(topic) {
- highlightTopic(topic);
- })
- }
- function highlightTopic(topic) {
- let found = 0;
- if (topic == null || topic.length === 0) return;
- var topicRegex = new RegExp(topic, 'gi');
- var treeWalker = document.createTreeWalker(
- document.body,
- NodeFilter.SHOW_TEXT,
- {
- acceptNode: function(node) {
- var result = NodeFilter.FILTER_SKIP;
- if (topicRegex.test(node.nodeValue)) {
- found += 1
- console.log('found', found);
- if (found <= 6) {
- console.log('foound less than 6', found)
- result = NodeFilter.FILTER_ACCEPT
- return result;
- }
- };
- }
- }, false
- )
- var skipTagName = {
- "NOSCRIPT": true,
- "SCRIPT": true,
- "STYLE": true
- }
- var nodeList = [];
- while (treeWalker.nextNode()) {
- if (!skipTagName[treeWalker.currentNode.parentNode.tagName]) {
- nodeList.push(treeWalker.currentNode);
- }
- }
- nodeList.forEach(function (n) {
- var rangeList = [];
- // find sub-string ranges
- var startingIndex = 0;
- do {
- // console.log(word, startingIndex, n.parentNode, n.textContent);
- startingIndex = n.textContent.indexOf(topic, startingIndex + 1);
- if (startingIndex !== -1) {
- var topicRange = document.createRange();
- topicRange.setStart(n, startingIndex);
- topicRange.setEnd(n, startingIndex + topic.length);
- rangeList.push(topicRange);
- }
- } while (startingIndex !== -1);
- // highlight all ranges
- rangeList.forEach(function (r) {
- highlightRange(r);
- });
- });
- }
- // highlight the keyword by surround it by `i`
- var highlightRange = function (range) {
- const bgColorCode = '#000000';
- var anchor = document.createElement("A");
- var selectorName = anchor.className = "highlighted_text";
- anchor.classList.add("highlighted_text");
- // range.surroundContents(iNode) will throw exception if word across multi tag
- if (!ruleExistenceDict[bgColorCode]) {
- sheet.insertRule([".", selectorName, " { background: #", bgColorCode, " !important; }"].join(""), 0);
- ruleExistenceDict[bgColorCode] = true;
- console.log(sheet);
- }
- anchor.appendChild(range.extractContents());
- anchor.href = `https://google.com/?search=${
- range.extractContents()
- }`;
- range.insertNode(anchor);
- };
Add Comment
Please, Sign In to add comment