forked from greatsuspender/thegreatsuspender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
52 lines (42 loc) · 1.75 KB
/
content_script.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
/*global chrome, document, window, console, html2canvas */
(function () {
"use strict";
chrome.extension.onMessage.addListener(
function (request, sender, sendResponse) {
//console.log('received request');
var elementCount = document.getElementsByTagName("*").length,
processing = true;
//safety check here. don't try to use html2canvas if the page has more than 5000 elements
if (elementCount < 5000) {
//allow max of 3 seconds to finish generating image (used to catch unexpected html2canvas failures)
window.setTimeout(function () {
if (processing) {
processing = false;
console.error('failed to render');
sendResponse({});
}
}, 3000);
try {
html2canvas([document.body], {
height: Math.min(document.body.offsetHeight, window.innerHeight) - 125,
width: document.body.clientWidth - 6,
proxy: false,
onrendered: function (canvas) {
if (processing) {
processing = false;
sendResponse({previewUrl: canvas.toDataURL()});
}
}
});
} catch (ex) {
console.error('failed to render');
sendResponse({});
}
} else {
console.error('too many page elements');
sendResponse({});
}
return true;
}
);
}());