-
Notifications
You must be signed in to change notification settings - Fork 5
/
microformat_bitcoin_content_script.js
66 lines (55 loc) · 1.43 KB
/
microformat_bitcoin_content_script.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
if (window == top) {
var addresses = findBitcoinAddresses();
chrome.extension.sendMessage(addresses);
}
/* Search the page for any bitcoin related urls and returns them as array. */
function findBitcoinAddresses() {
var result = document.evaluate(
'//link[starts-with(@href, "bitcoin:") or @rel="bitcoin"] | //a[starts-with(@href, "bitcoin:") or @rel="bitcoin"]',
document, null, 0, null);
var bitcoin = {};
var other = {};
var item;
while (item = result.iterateNext()) {
var parsed = parseBitcoinURL(item.href);
if (!parsed) {
other[item.href] = true;
} else {
bitcoin[parsed.address] = parsed;
}
}
var count = 0;
var output = { bitcoin: [], other: [] }
for (var key in bitcoin) {
if (bitcoin.hasOwnProperty(key)) {
output.bitcoin.push(bitcoin[key]);
count += 1;
}
}
for (var key in other) {
if (other.hasOwnProperty(key)) {
output.other.push(key);
count += 1;
}
}
output.count = count;
return output;
}
/* Parse bitcoin URL query keys. */
function parseBitcoinURL(url) {
var r = /^bitcoin:([a-zA-Z0-9]{27,34})(?:\?(.*))?$/;
var match = r.exec(url);
if (!match) return null;
var parsed = { url: url }
if (match[2]) {
var queries = match[2].split('&');
for (var i = 0; i < queries.length; i++) {
var query = queries[i].split('=');
if (query.length == 2) {
parsed[query[0]] = decodeURIComponent(query[1].replace(/\+/g, '%20'));
}
}
}
parsed.address = match[1];
return parsed;
}