-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
84 lines (71 loc) · 1.71 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
"use strict";
function onClick(event) {
if (!event.isTrusted || event.defaultPrevented || event.button != 0) {
return;
}
if (event.ctrlKey || event.shiftKey ||
event.metaKey || event.altKey) {
return;
}
let node = event.target;
if (node instanceof HTMLBodyElement ||
node instanceof HTMLHtmlElement) {
return;
}
let link = null;
do {
if (node instanceof HTMLAnchorElement ||
node instanceof HTMLAreaElement ||
node instanceof SVGAElement) {
link = node;
}
else {
node = node.parentElement;
if (!node || node instanceof HTMLBodyElement) {
break;
}
}
}
while (link == null);
if (!link || !link.hasAttribute("download")) {
return;
}
let url = link.href;
if (url) {
// Handle SVG links:
if (typeof url == "object" && url.animVal) {
url = url.animVal;
}
}
if (!url) {
let href = link.getAttribute("href") ||
link.getAttributeNS("http://www.w3.org/1999/xlink", "href");
if (href && /\S/.test(href)) {
url = new URL(href, link.baseURI).href;
}
}
if (!url || !/^https?:/i.test(url)) {
return;
}
let origin = link.origin;
if (!origin) {
origin = new URL(url).origin;
}
if (origin != window.location.origin) {
return;
}
event.stopPropagation();
event.preventDefault();
browser.runtime.sendMessage({
url,
"filename": link.download.trim()
}).then(() => {
window.location.href = url;
}).catch(error => {
console.error(error);
link.click();
});
}
if (window.origin != "resource://pdf.js") {
window.addEventListener("click", onClick, true);
}