-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
45 lines (37 loc) · 997 Bytes
/
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
document.addEventListener("click", handleCtrlLeftClick, true);
document.addEventListener("auxclick", handleMiddleClick, true);
function handleCtrlLeftClick(event) {
if (event.button === 0 && (event.ctrlKey || event.metaKey)) {
handleOpenInTabClick(event);
}
}
function handleMiddleClick(event) {
if (event.button === 1) {
handleOpenInTabClick(event);
}
}
function handleOpenInTabClick(event) {
const anchor = getAnchor(event.target);
if (!anchor || !anchor.href) {
return;
}
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
chrome.runtime.sendMessage(
{
name: "open_link_in_tab",
openInForeground: !event.shiftKey,
url: anchor.href,
}
);
}
function getAnchor(element) {
if (!element) {
return undefined;
}
if (element.tagName === "A") {
return element;
}
return getAnchor(element.parentNode);
}