-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoneToLink.js
63 lines (50 loc) · 2.04 KB
/
phoneToLink.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
var proto = undefined; // global held protocol
async function makeLinks() {
// get protocol to use
let setting = await browser.storage.sync.get("protocol");
proto = setting.protocol;
if (!proto) {
// Found no specific setting for phoneToLink AddOn. Using default.
proto = "callto:";
}
// replace numbers
let body = document.body.innerHTML;
// cleanup weird linebreaks that hinder number detection
if (body.search("content=\"text/html") != -1 || body.search("class=\"moz-text-html") != -1) {
// html bodies
// console.log("replace", body);
body = body.replace(/[\r|\n]{1}\s{0,40}/g, " ");
body = body.replace(/\s{2,40}/g, " ");
} else {
// plain text bodies
// console.log("no replace", body);
}
// HINT: Copy this to https://regex101.com for validation and testing
const r = /(\<a .*?a\>|\<style.*?style\>|\<pre.*?pre\>|{.*?}|style=".*?"|[\s>\(]{1}(00|0|\+|+|+){1}\s?(\d{2,})(?:[ -\/\\\(\)]){0,2}(\d*)(?:[ -\/\\\(\)]){0,2}(\d*)(?:[ -\/\\\(\)]){0,2}(\d*)(?:[ -\/\\\(\)]){0,2}(\d*)(?:[ -\/\\\(\)]){0,2}(\d*)(?:[ -\/\\\(\)]){0,2}(\d*))/igs;
body = body.replace(r, telReplace);
document.body.innerHTML = body;
}
function telReplace(m, f1, f2, f3, f4, f5, f6, f7, f8) {
// console.log(m, f1, f2, f3, f4, f5, f6, f7, f8);
if (f1.substr(0, 1) === "<" ||
f1.substr(0, 1) === "{" ||
f1.substr(0, 5) === "style" ||
f1.substr(0, 6) === "<style" ||
f1.substr(0, 4) === "<pre") {
// keep found links, curlys, pre and style tags like they are
return m;
}
m = m.trim();
let number = (f2+f3+f4+f5+f6+f7+f8).trim();
if (m.substr(0,1) == ">") {
m = m.substr(1); // cut pre >
return '><a href="'+proto+number+'">'+m+'</a>';
}
if (m.slice(-1) == "&") {
m = m.slice(0, -1); // cut pre >
return '<a href="'+proto+number+'">'+m+'</a>&';
}
console.log("use", m, number);
return ' <a href="'+proto+number+'">'+m+'</a>';
}
makeLinks()