-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.js
130 lines (113 loc) · 4.04 KB
/
highlighter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const regexs = {
BTC: /\bbc1[02-9ac-hj-np-z]{11,71}\b|\b[13][a-zA-HJ-NP-Z0-9]{23,39}\b/,
ETH: /\b0x[a-fA-F0-9]{40}\b/
};
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
};
const performRegex = (highlight) => {
// param highlight will wrap the match in a span if wanted
let matches = {
BTC: [],
ETH: [],
DOGE: [] // left for testing
};
let texts = getTextNodes(document.documentElement);
// texts = [].concat.apply([],texts);
for (let i = 0; i < texts.length; i++){
for (let j = 0; j < Object.keys(regexs).length; j++) {
ticker = Object.keys(regexs)[j];
re = new RegExp(regexs[ticker], 'gm');
match = texts[i].nodeValue.match(re);
if ((match) && (match.indexOf(",") == -1)) {
matches[ticker].push(... match);
// make mark on texts[i]
if (highlight) {wrapTextNode(texts[i], ticker);}
} else if ((match) && (match.indexOf(",") > -1)) {
matches[ticker].push(... match.split(","));
// make mark on texts[i]
if (highlight) {wrapTextNode(texts[i], ticker);}
} else {}
}
}
// Below is a to:do, figure out how to represent addresses found in commented HTML
// Also figure out for addresses within html non-text (e.g. as an id or class name)
// let comments = getAllComments(document.documentElement);
// for (let i = 0; i < comments.length; i++) {
// match = comments[i].nodeValue.match(re);
// if ((match) && (match.indexOf(",") == -1)) {
// matches.push(...match.map(x => "<!-- " +x));
// } else if ((match) && (match.indexOf(",") > -1)) {
// matches.push(... match.map(x => "<!-- " +x).split(","));
// }
// }
// const whole_html_string_1 = document.documentElement.innerHTML;
// const matching_btc_1 = whole_html_string_1.match(re);
// if (matching_btc_1) {
// const uniques_2 = matching_btc_1.filter(onlyUnique);
// console.log(`${uniques_2.length} uniques from innerHTML: ${uniques_2}`);
// }
if (matches) {
let tickers_to_delete = [];
for (let i = 0; i < Object.keys(matches).length; i++){
ticker = Object.keys(matches)[i];
if (matches[ticker].length == 0){
tickers_to_delete.push(ticker);
} else {
let uniques = matches[ticker].filter(onlyUnique);
matches[ticker] = uniques;
}
}
for (let i = 0; i < tickers_to_delete.length; i ++){
delete matches[tickers_to_delete[i]];
}
return matches;
}
};
function getAllComments(rootElem) {
var comments = [];
var iterator = document.createNodeIterator(rootElem, NodeFilter.SHOW_COMMENT);
var curNode;
while (curNode = iterator.nextNode()) {
comments.push(curNode);
}
return comments;
};
function getTextNodes(rootElem){
var texts = [];
var iterator = document.createNodeIterator(rootElem, NodeFilter.SHOW_TEXT);
var curNode;
while (curNode = iterator.nextNode()) {
texts.push(curNode);
}
return texts;
};
function wrapTextNode(textNode, ticker) {
// Take a text node and wrap it in a span, creating the highlight
var spanNode = document.createElement('span');
spanNode.setAttribute('class', 'crypto-highlighter-mark-'+ticker.toLowerCase());
var newTextNode = document.createTextNode(textNode.textContent);
spanNode.appendChild(newTextNode);
spanNode.addEventListener("click", copyAddressToClipboard);
textNode.parentNode.replaceChild(spanNode, textNode);
};
const copyAddressToClipboard = e => {
// works for span, needs some sort of visual confirmation
const addressText = e.target.innerText;
navigator.clipboard.writeText(addressText);
// console.log(`Copied the text: ${addressText}`);
};
// below are the chrome listeners for messages from background.js
chrome.runtime.onMessage.addListener((obj, sender, response) => {
const {type, value} = obj;
if (type === 'NEW'){
matches = performRegex(true);
response(matches);
} else if (type === 'SWITCHED'){
matches = performRegex(false);
response(matches);
} else if (type === 'Addresses'){
matches = performRegex(false);
response(matches);
}
});