-
Notifications
You must be signed in to change notification settings - Fork 7
/
contentscript.js
executable file
·40 lines (33 loc) · 1.04 KB
/
contentscript.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
/*
Navigate to previous page when the Backspace key is pressed.
Avoid this behavior when the focused element is an input field, textarea
or has contenteditable attribute set to true.
Not using `addEventListener` because the browser does not trigger the handler
when the Backspace key is pressed.
*/
document.onkeydown = function(e) {
if (e.key !== 'Backspace') {
return;
}
// Overcomes issues with custom elements wrapping INPUT
const target = event.composedPath()[0];
// Do not handle user input on input and textarea elements.
// In some scenarios tagName can be lowercase. Ensure we catch that.
if (["input", "textarea"].indexOf(target.tagName.toLowerCase()) > -1) {
return;
}
// Do not handle elements with contenteditable = true
if (target.isContentEditable) {
return;
}
// Do not handle alongside modifiers except Shift key
if (e.ctrlKey || e.altKey || e.metaKey) {
return;
}
e.preventDefault();
if (e.shiftKey) {
window.history.forward();
} else {
window.history.back();
}
};