forked from ProTipHQ/ProTip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
194 lines (158 loc) · 6 KB
/
content.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var port = chrome.runtime.connect();
chrome.runtime.sendMessage({action: 'isBlacklisted', url:document.URL});
chrome.runtime.sendMessage({action: 'isStarredUser', url:document.URL});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
isBlacklisted = request.response // set global
if (request.method == 'isBlacklisted' && request.response == false){
if(!checkMetatag()){ // if the metatag tip is found, don't scan the page.
scanPage();
}
} else if (request.method == 'isStarredUser' && request.response == true){
starredUser();
} else {
// else page is blacklisted and no need to scan anything.
}
});
function checkMetatag(){
//<meta name="microtip" content="1PvxNMqU29vRj8k5EVKsQEEfc84rS1Br3b" data-currency="btc">
if ( $('meta[name=microtip]').content ){
chrome.runtime.sendMessage({
bitcoinAddress: $('meta[name=microtip]').attr("content"),
title: document.title,
url: document.URL,
timestamp: Date.now()
});
return true
} else {
return false
}
}
function starredUser(){
var twitterUserContainer = document.getElementsByClassName('ProfileHeaderCard-name')[0];
var span = document.createElement("span");
span.style.backgroundColor = '#7FE56F'; //'#5ada46';
//Code for displaying <extensionDir>/images/myimage.png:
var imgURL = chrome.extension.getURL("./assets/images/star.png");
var img = document.createElement("img");
img.setAttribute("src", imgURL);
//var img.src = imgURL;
span.style.padding = '5px';
span.style.marginLeft = '6px';
span.style.position = 'relative';
span.style.fontSize = '10px';
span.style.top = '-3px';
span.style.borderRadius = '2px';
span.style.display = 'inline-flex';
span.innerText = 'ProTip Sponsor';
twitterUserContainer.appendChild(img);
twitterUserContainer.appendChild(span);
}
var numberOfHighlightedAddresses = 3,
// because I am too stupid to work out how to do this correctly.
accumulator = [];
var matchText = function(node, regex, callback, excludeElements) {
excludeElements || (excludeElements = [
'script',
'style',
'iframe',
'cavas'
] );
var child = node.firstChild;
do {
if (!child) { break }
switch (child.nodeType) {
case 1:
if (excludeElements.indexOf(child.tagName.toLowerCase()) > -1) {
continue;
}
matchText( child, regex, callback, excludeElements );
break;
case 3:
match = child.data.match(regex);
if (match !== null){
accumulator = accumulator.concat(match);
// Some pages a filled with bitcoin addresses
// These are not useful to us.
if(accumulator.length >= numberOfHighlightedAddresses){ return }
}
child.data.replace(regex, function (all) {
var args = [].slice.call(arguments),
offset = args[args.length - 2],
newTextNode = child.splitText(offset);
newTextNode.data = newTextNode.data.substr(all.length);
callback.apply(window, [child].concat(args));
child = newTextNode;
});
break;
}
} while (child = child.nextSibling);
return node;
}
function searchLinksForBitcoin(){
var accumulator = [];
bitcoinTagRegexp = /bitcoin:([13][1-9A-HJ-NP-Za-km-z]{26,33})\?&?amount=[0-9\.]+/g;
$.each(document.links, function(key, value) {
if (value.href.match(bitcoinTagRegexp)) {
accumulator.push(value.href)
}
});
return accumulator;
}
function stripBitcoinAddresssesFromBitcoinLinks(accumulator){
if (accumulator.length) { accumulator = [] }
$.each(searchLinksForBitcoin(), function (value) {
match = value.match(/[13][a-km-zA-HJ-NP-Z0-9]{26,33}/)
if (match.length > 0) {
accumulator.push(match);
}
} );
return accumulator;
}
window.addEventListener("message", function (event) {
// We only accept messages from ourselves
if (event.source != window) {
return;
}
var highlight = document.getElementById(event.data.bitcoinAddress);
highlight.style.backgroundColor = '';
chrome.runtime.sendMessage({
action: event.data.action,
bitcoinAddress: event.data.bitcoinAddress,
url: document.URL
} );
}, false);
function scanPage(){
if(document.URL.match(/http/)){ // only send http or https urls no chrome:// type addresses.
matchText(document.documentElement, new RegExp("(^|\\s)[13][a-km-zA-HJ-NP-Z0-9]{26,33}($|\\s)", "g"), function (node, match, offset) {
checkbox = document.createElement("input");
checkbox.type = 'checkbox';
checkbox.checked = 'checked';
checkbox.addEventListener("click",
function () {
window.postMessage({ action: "revokeBitcoinAddress", bitcoinAddress: match }, "*");
}, false);
var span = document.createElement("span");
span.style.backgroundColor = '#7FE56F'; //'#5ada46';
span.style.padding = '2px 0 2px 0';
span.style.borderRadius = '2px';
span.style.display = 'inline-flex';
span.textContent = match;
span.id = match;
// Checkbox after the BTC address.
//node.parentNode.insertBefore(span, node.nextSibling).appendChild(checkbox);
// Checkbox after the BTC address.
node.parentNode.insertBefore(span, node.nextSibling);
node.nextSibling.insertBefore(checkbox, node.nextSibling.firstChild);
});
if (accumulator.length > 0) {
for(i in accumulator){ accumulator[i] = accumulator[i].trim() } // clean the addresses a little...
chrome.runtime.sendMessage({
bitcoinAddresses: accumulator.slice(0,numberOfHighlightedAddresses -1),
bitcoinAddress: accumulator[0],
title: document.title,
url: document.URL,
timestamp: Date.now()
});
}
}
}