-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
94 lines (80 loc) · 1.78 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
(function () {
"use strict";
let minOffset = 0;
let direction = 1;
function syncSettings(items) {
let v = items["minOffset"];
if (v)
minOffset = v;
v = items["direction"];
if (v)
direction = v;
}
chrome.storage.onChanged.addListener(syncSettings);
chrome.storage.sync.get(["minOffset", "direction"], syncSettings);
/* GTK+ shows context-menu on mouse press. It could be easily disabled here but
that'd be too intrusive and so only Shift modifier will work. */
chrome.runtime.sendMessage({type: "isgtk"}, gtk => {
if (gtk)
initGtk();
else
initWindows();
});
let offset = 0;
function handleWheelScroll(ev)
{
offset += ev.deltaY + ev.deltaX; // got X on Linux, Y on Windows
let d;
if (offset > minOffset)
d = -1;
else if (offset < -minOffset)
d = 1;
else
return;
chrome.runtime.sendMessage({type: "scroll", dir: d * direction});
offset = 0;
}
function initGtk()
{
window.addEventListener("wheel", ev => {
if (!ev.shiftKey)
return;
handleWheelScroll(ev);
ev.preventDefault();
});
}
function initWindows()
{
let mouseDown = false;
let preventMenu = false;
window.addEventListener("wheel", ev => {
if ((ev.buttons & 2) == 0 && !ev.shiftKey)
return;
handleWheelScroll(ev);
ev.preventDefault();
preventMenu = true;
});
/* It'd be nice to prevent context-menu with ev.stopPropagation() in
"mouseup" handler but I couldn't get it to work. */
window.addEventListener("mousedown", ev => {
if (ev.button != 2)
return;
mouseDown = true;
preventMenu = false;
});
window.addEventListener("mouseup", ev => {
if (ev.button != 2)
return;
if (!mouseDown)
preventMenu = true;
else
mouseDown = false;
});
window.addEventListener('contextmenu', ev => {
if (preventMenu) {
ev.preventDefault();
preventMenu = false;
}
});
}
})();