-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject.js
67 lines (60 loc) · 2.03 KB
/
inject.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
var conf = new function () {
this.deattach_clear = true;
this.animation = true;
this.sensitivity = 50;
this.log = true;
this.init = function () {
chrome.storage.sync.get({
animation: true,
sensitivity: 50,
}, function (items) {
for (key in items) {
conf.set(key, items[key]);
}
if (items.animation) conf.inject_css();
});
window.addEventListener("wheel", poll_wheel);
}
this.set = function (name, val) {
conf[name] = val;
}
this.inject_css = function () {
window.addEventListener('DOMContentLoaded', (event) => {
const sheet = new CSSStyleSheet();
sheet.replaceSync('body.swipeHistory { transition: opacity 0.2s; opacity: 0.2; overflow: hidden;' +
'backface-visibility: hidden; pointer-events: none; }');
document.adoptedStyleSheets = [sheet];
});
}
};
function deattach() {
// Remove wheel event listener but reattach it in case the user navigates back using browser history
// Timeout is neccessary when a user tries to navigate forward but forward history is missing
if (conf.deattach_clear) {
window.removeEventListener("wheel", poll_wheel);
try {
document.body.classList.add("swipeHistory");
} catch (err) {
log(err.message);
}
setTimeout(function () {
window.addEventListener("wheel", poll_wheel);
document.body.classList.remove("swipeHistory");
conf.deattach_clear = true;
}, 1250);
}
conf.deattach_clear = false;
}
function poll_wheel(e) {
if (e.deltaX > conf.sensitivity) {
deattach();
window.history.forward();
} else if (e.deltaX < -Math.abs(conf.sensitivity)) {
deattach();
window.history.back();
}
}
function log(message) {
if (conf.log) console.log("SwipeHistory:", message);
}
conf.init();