-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.js
65 lines (50 loc) · 1.27 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
var isHovered = false;
var global;
const imgs = document.getElementsByTagName('img');
const map = fn => x => Array.prototype.map.call(x, fn);
map(img => {
img.addEventListener('mouseover', (e) => {
var a = e.target.closest("a");
if (a && a.getAttribute('href')) {
global = e.target.closest("a").getAttribute('href');
console.log(global)
} else {
global = e.target.src;
console.log(global)
}
hoveredBox();
});
img.addEventListener('mouseleave', (e) => {
isHovered = false;
});
})(imgs)
document.addEventListener('keypress', keyDown);
function hoveredBox() {
isHovered = true;
}
function keyDown(event) {
if (!isHovered) return;
var key = event.keyCode;
if (key === 115) {
saveFile(global);
}
}
// Download a file form a url.
function saveFile(url) {
// Get file name from url.
filename=url;
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
// alert('asdf');
a.download = filename; // Set the file name.
a.style.display = 'none';
document.body.appendChild(a);
a.click();
delete a;
};
xhr.open('GET', url);
xhr.send();
}