-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent-script.js
190 lines (162 loc) · 5.57 KB
/
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
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
var tabId = null // our current tabId
var bRefreshing = null // true if a reload timer is active on this page
function toBackground(event, param1 = undefined, param2 = undefined) {
if (tabId !== null) { // don't send anything unless we know who we are
browser.runtime.sendMessage({
event: event,
tabId: tabId,
arg1: param1,
arg2: param2,
})
}
}
function doPostRequest(url, postData) {
// Create form element
let form = document.createElement("form");
form.action = "";
form.method = "post";
form.target = "_top";
form.style.display = "none";
// Pass each value-pair to the form as hidden fields
if (postData) {
Object.keys(postData).forEach(function (key, index) {
let node = document.createElement("input");
node.type = "hidden";
node.name = key;
node.value = postData[key];
form.appendChild(node);
});
}
// Send form, but first it needs to be attached to the main document
document.body.appendChild(form);
form.submit();
}
browser.runtime.onMessage.addListener((message) => {
if (message.event == "set-tab-id") {
tabId = message.tabId
} else if (message.event == "reload-post") {
let loc = window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search + window.location.hash;
doPostRequest(loc, message.postData)
} else if (message.event == "reload-get") {
if (message.bypassCache) {
// TODO: implement force reload
}
window.location = message.url;
} else if (message.event == "timer-enabled") {
setTimerBadge(true, false);
} else if (message.event == "timer-disabled") {
setTimerBadge(false, false);
} else if (message.event == "scroll") {
window.scrollTo(message.scrollX, message.scrollY);
}})
window.addEventListener("mousedown", evt => {
if (evt.button === 2) {
toBackground("contextMenu")
} else {
toBackground("activity")
}
}, true)
window.addEventListener("keydown", evt => {
if (evt.shiftKey && evt.key === "F10" || evt.key === "ContextMenu") {
toBackground("contextMenu")
} else {
// Determine if this was a keypress we want to ignore
const ignoredKeys = [
"Alt",
"Control",
"Fn",
"FnLock",
"Hyper",
"Meta",
"ScrollLock",
"Super",
"OS"
];
if (ignoredKeys.includes(evt.key)) {
return;
}
// Determine if the user was typing into a text field
let bTextInput = false;
const textTypes = [
"text",
"textarea",
"search",
"email",
"password"
];
try {
bTextInput = textTypes.includes(evt.target.type);
} catch (ex) { }
// Let the background script know about the user activity
if (bTextInput) {
toBackground("typing-activity")
} else {
toBackground("activity")
}
}
}, true)
window.addEventListener("mousemove", evt => {
toBackground("activity")
}, true)
window.addEventListener("scroll", evt => {
toBackground("activity")
}, true)
// ---------------------------------
// Scroll events
// ---------------------------------
var last_known_scrollX_position = 0;
var last_known_scrollY_position = 0;
var scrollReportingBlocked = false
window.addEventListener("scroll", evt => {
last_known_scrollX_position = window.scrollX;
last_known_scrollY_position = window.scrollY;
if (!scrollReportingBlocked) {
window.requestAnimationFrame(function () {
toBackground(
"scroll",
last_known_scrollX_position,
last_known_scrollY_position
);
scrollReportingBlocked = false;
});
scrollReportingBlocked = true;
}
});
// ---------------------------------
// Timer badge handling
// ---------------------------------
function setTimerBadge(bEnable, bThrottle) {
const TIMER_BADGE = "↻ ";
bRefreshing = bEnable;
let noBadgeTitle = document.title.replace(TIMER_BADGE, '');
let newTitle = null;
if (bEnable) {
newTitle = TIMER_BADGE + noBadgeTitle;
} else {
newTitle = noBadgeTitle;
}
if (document.title != newTitle) {
// Note to other addon authors:
// If an addon other than ReloadMatic also tries to alter the title,
// the client can get into an infinite loop and beside eating up
// a lot of CPU, this can also make the browser unresponsive.
// To prevent this, please set the title using a small delay
// whenever it is changing not as a direct result of a user action.
// The loop and possible graphical glitches will still be there,
// but at least the CPU-use will be kept within limits.
if (!bThrottle) {
document.title = newTitle;
} else {
setTimeout(function(){ document.title = newTitle; }, 100);
}
}
}
document.addEventListener("DOMContentLoaded", function(event) {
// the observer instance watches for changes in the <title element>
var observer = new MutationObserver(function(mutations) {
setTimerBadge(bRefreshing, true);
});
var obsConfig = { subtree: true, characterData: true, childList: true };
var obsTarget = document.querySelector("head > title");
observer.observe(obsTarget, obsConfig);
});