forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
161 lines (132 loc) · 6.72 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const TIMER_DEFAULT_STYLE =
"position: fixed; z-index: 2147483647; " +
"top: 0px; left: 0px; margin: 2px; padding: 4px 4px 2px 4px; " +
"background-color: #fff; color: #000; " +
"border: solid 1px #000; border-radius: 4px; " +
"font: normal 12px \"Lucida Console\", Monaco, monospace; " +
"user-select: none; -moz-user-select: none;";
const TIMER_SIZES = ["10px", "12px", "14px", "16px"];
const TIMER_LOCATIONS = [
["0px", "", "0px", ""],
["0px", "", "", "0px"],
["", "0px", "", "0px"],
["", "0px", "0px", ""]
];
const ALERT_CONTAINER_STYLE =
"position: fixed; z-index: 2147483647; display: none; " +
"top: 0px; left: 0px; width: 100%; height: 100%; " +
"margin: auto; padding: 0px; " +
"background-color: transparent;";
const ALERT_BOX_STYLE =
"margin: auto; padding: 12px; " +
"line-height: normal; text-align: center; " +
"background-color: #fff; color: #000; " +
"border: solid 1px #000; border-radius: 4px; " +
"font: normal 16px Helvetica, Arial, sans-serif; " +
"box-shadow: 2px 2px 2px #888; " +
"user-select: none; -moz-user-select: none;";
const ALERT_ICON_STYLE = "padding: 0px;"
const ALERT_TEXT_STYLE = "padding: 8px 0px 0px 0px;"
const ALERT_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAHLUlEQVRYhbWXa1CTZxbHI7XMrJ1pO86O0w8FEgIoAbGYtSqgExUDISawjq9Ct1BQbkIiJFxCQoA3CeSCkZtAIEBA1novoiCt2BYEWi+IKCmlIsK2asdp3a5bUERa/vshmGKXHQh1/zPv1/f8zv88zznnoVD+gAiCeIUgiFf+yD9sFkmSduv3EEu9y5XxXmXKj9apJTFsNnsZQRB/IknS7qUGm87QnsfjLdksT6OvKlfGrzQoGsPO1o4fGejB5e9HUNbbhcDDJWPMwqzWd/MyopmxsXSCIN7g8XhLCIKwn3Zo0byD0g3kMq8KZcI75WSeVzlZ712R2739lPFR+sWzODl4E533hmG48QUCjpWDUaFEyNFylF5pw4U7X8NkvgJxWyO2HS37N7NMcX11UVbDqoKsYu8ieQorMvKteTnkmS9ni9sb0X53CG3fDaL1H7dg7LsEcVsjWMdL4Vmng2etFh5VuXAvV2DFwWwsL5DBbb8Ea0tI7DxUAum54zBebsMJczdahwbwwcdHsDV010F/f/835gRYXSAPLOvtwo6ztZZgs3weNRowjLlwLyOxolgOtwNSuOnS4aJOAV2RBFp2ImjSeFDTYrBBnwVpZzMCw3YeDQgIWDonwKqi7IDS3s45ANRwr1DBvSwHK4oy4abPgKs2DS4qEeikEDR5IqgZcaCm7oFffiaknc1g7yKOzQuAmS9nl/Z2gjhbNztArRYexjy4Gyz2ryjMxGajDvyaA/DSSUDPFoKWuRdUSSycUnbD11YAT3321oO9nSCaZgfwMGnAMKrgXkZifZUGn98ZwHM9eTaBpBO10/ZHw1EUBZ982QIArndgZ9Oh2QGq1WBUKrHSoIT5wT0AwKVvh/DJwE0AwHc//WixPyUajsmRWK+V2gbgVZy9peR/AHjUauFRnQeGQYmSy58DADpHbmG5Ng1MfSampqbw0+PRafv3wHHfBwsAKJRvLu65iF2zOWDSYNdp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hJ9hstgPFlq14pkiSXLwhPWEH75hhouPubWyu0llWr1wR6OQ+OMsTQZVONx9RFLxIET4dHoB/le5XDrHjMw6H402S5OIFBX8ugiDsfYSxXFa1fvTjO/1IaKyHa64YztlCOE+vXtT0GER9aMDp233wLc6ZDNi5/QKXy2XO99rNKSaT+eqWsDCvtbqMvqyuFjQN3IBvMQmaLB6swhy03OpD0mcN8JHvGw0KDq4IDAxczmQyX30pwZ+LJEk7Npu9zC8tTrXBqHn89/5uXLjdj/IbXdhYqpr0j3yvLygoKJLFYr31/3wvLuLxeEs4RDDDJ0d0bk2BfGxTfOQQn8/P4nK57jwebwlloQfOFpEkaRcSEvIml8v15HK5zhwO5/WX/jCdj2x+eM6i/wA9co387UiW6AAAAABJRU5ErkJggg==";
var gTimer;
var gAlert;
// Update timer
//
function updateTimer(text, size, location) {
if (!text) {
if (gTimer) {
// Hide timer
gTimer.hidden = true;
}
} else {
if (!gTimer) {
// Create timer
gTimer = document.createElement("div");
gTimer.setAttribute("style", TIMER_DEFAULT_STYLE);
}
if (!document.body.contains(gTimer)) {
// Insert timer at end of document body
document.body.appendChild(gTimer);
}
// Set text
gTimer.innerText = text;
// Set size
if (size >= 0 && size < TIMER_SIZES.length) {
gTimer.style.fontSize = TIMER_SIZES[size];
}
// Set location
if (location >= 0 && location < TIMER_LOCATIONS.length) {
gTimer.style.top = TIMER_LOCATIONS[location][0];
gTimer.style.bottom = TIMER_LOCATIONS[location][1];
gTimer.style.left = TIMER_LOCATIONS[location][2];
gTimer.style.right = TIMER_LOCATIONS[location][3];
}
// Show timer
gTimer.hidden = false;
}
}
// Show alert message
//
function showAlert(text) {
let alertBox, alertIcon, alertText;
if (!gAlert) {
// Create container
gAlert = document.createElement("div");
gAlert.setAttribute("style", ALERT_CONTAINER_STYLE);
document.body.appendChild(gAlert);
// Create message box
alertBox = document.createElement("div");
alertBox.setAttribute("style", ALERT_BOX_STYLE);
alertBox.addEventListener("click", hideAlert);
alertIcon = document.createElement("img");
alertIcon.setAttribute("style", ALERT_ICON_STYLE);
alertIcon.setAttribute("src", ALERT_ICON);
alertBox.appendChild(alertIcon);
alertText = document.createElement("div");
alertText.setAttribute("style", ALERT_TEXT_STYLE);
alertBox.appendChild(alertText);
gAlert.appendChild(alertBox);
}
// Set text
alertText.innerText = text;
// Show timer
gAlert.style.display = "flex";
}
// Hide alert message
//
function hideAlert() {
if (gAlert) {
gAlert.style.display = "none";
}
}
// Check page for keyword(s)
//
function checkKeyword(keywordRE) {
// Create regular expression (case insensitive)
let regexp = new RegExp(keywordRE, "i");
// Get all text nodes in document
let textNodes = document.evaluate(
"//text()", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
//console.log("Checking " + textNodes.snapshotLength + " text node(s) for keyword(s)...");
for (let i = 0; i < textNodes.snapshotLength; i++) {
if (regexp.test(textNodes.snapshotItem(i).data)) {
return true; // keyword(s) found
}
}
return false; // keyword(s) not found
}
/*** EVENT HANDLERS BEGIN HERE ***/
function handleMessage(message, sender, sendResponse) {
if (message.type == "timer") {
updateTimer(message.text, message.size, message.location);
} else if (message.type == "alert") {
showAlert(message.text);
} else if (message.type == "keyword") {
let keyword = checkKeyword(message.keywordRE);
sendResponse(keyword);
}
}
browser.runtime.onMessage.addListener(handleMessage);