-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
97 lines (78 loc) · 2.28 KB
/
core.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
var MOUSE_VISITED_CLASSNAME = "crx_mouse_visited";
var SHOW_INFO = "show-info";
var links = document.getElementsByTagName("a");
var utminfos = document.createElement("div");
utminfos.className = "utminfo";
var loupe = chrome.extension.getURL("/images/loupe.svg");
var title = document.createElement("h1");
title.innerHTML =
'<img src="' +
loupe +
'" class="svg"/><span class="big">Utm</span><br />Explorer';
var container = document.createElement("div");
container.className = "container";
utminfos.append(title, container);
document.getElementsByTagName("body")[0].appendChild(utminfos);
chrome.runtime.sendMessage("active");
var disabled = true;
chrome.runtime.onMessage.addListener(function(request) {
if (request == "disabled") {
disabled = true;
} else if (request == "enabled") {
disabled = false;
}
return true;
});
for (var i = 0; i < links.length; i++) {
let link = links[i];
link.addEventListener("mouseover", function() {
if (!disabled) {
showUtm(this.href);
utminfos.classList.add(SHOW_INFO);
this.classList.add(MOUSE_VISITED_CLASSNAME);
}
});
link.addEventListener("mouseout", function() {
utminfos.classList.remove(SHOW_INFO);
this.classList.remove(MOUSE_VISITED_CLASSNAME);
container.innerHtml = "";
});
}
function showUtm(url) {
var utm = extractUtm(url);
if (utm) {
constructPop(utm);
} else {
constructPop({ Aucun: "tag UTM" });
}
}
function extractUtm(url) {
var regexp = /(?!&)utm_[^=]*=[^&]*/g;
var matches = url.match(regexp);
if (matches) {
var values = matches.reduce(function(obj, param) {
var keyVal = param.split("=");
obj[keyVal[0]] = keyVal[1];
return obj;
}, {});
}
return values;
}
function constructPop(utms) {
while (container.firstChild) {
container.removeChild(container.firstChild);
}
Object.keys(utms).map(function(objectKey, index) {
let value = utms[objectKey];
let line = document.createElement("div");
line.className = "line";
let leftutm = document.createElement("span");
leftutm.className = "utm";
let rightutm = document.createElement("span");
rightutm.className = "value";
leftutm.append(objectKey);
rightutm.append(value);
line.append(leftutm, rightutm);
container.appendChild(line);
});
}