-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
77 lines (65 loc) · 1.89 KB
/
main.js
File metadata and controls
77 lines (65 loc) · 1.89 KB
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
/*
Code from here: https://minaminaworld.tistory.com/89
*/
function bodyShot() {
html2canvas(document.body)
.then(
function (canvas) {
var fname = 'screenshot-' + window.location.hostname + '-' + document.title + '.png';
saveAs(canvas.toDataURL(), fname);
}).catch(function (err) {
console.log(err);
});
}
function saveAs(uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
window.open(uri);
}
}
function loadImage() {
var images = document.getElementsByTagName('img');
var urls = new Array;
for (var i = 0; i < images.length; i++) {
var url = images[i].getAttribute('src');
if (url == null) {
continue;
}
urls[i] = toAbsolute(url);
}
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentDocument == null) {
continue;
}
images = iframes[i].contentDocument.getElementsByTagName('img');
for (var j = 0; j < images.length; j++) {
var url = images[j].getAttribute('src');
if (url == null) {
continue;
}
urls.push(toAbsolute(url));
}
}
return urls;
}
// https://stackoverflow.com/a/14781678
function toAbsolute(url) {
var link = document.createElement("a");
link.href = url;
return link.href;
}
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.text && (msg.text == "full_screen")) {
bodyShot();
}
else if (msg.text && (msg.text == "get_image")) {
sendResponse(loadImage());
}
});