-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
131 lines (107 loc) · 3.75 KB
/
page.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var CAPTURE_DELAY = 500;
var onMessage = (data, sender, callback) => {
if (data.msg === 'scrollPage') {
getPositions(callback, true);
return true;
}
else if (data.msg === 'visibleOnly') {
getPositions(callback, false);
return true;
}
else
console.error('Unknown message received: ' + data.msg);
}
if (!window.hasScreenCapturePage) {
window.hasScreenCapturePage = true;
chrome.runtime.onMessage.addListener(onMessage);
}
var max = (nums) => Math.max.apply(Math, nums.filter((x) => { return x; }));
var getPositions = (callback, fullPage) => {
const body = document.body,
originalBodyOverflowYStyle = body ? body.style.overflowY : '',
originalX = window.scrollX,
originalY = window.scrollY,
originalOverflowStyle = document.documentElement.style.overflow;
if (body)
body.style.overflowY = 'visible';
let widths = [
document.documentElement.clientWidth,
body ? body.scrollWidth : 0,
document.documentElement.scrollWidth,
body ? body.offsetWidth : 0,
document.documentElement.offsetWidth
],
heights = [
document.documentElement.clientHeight,
body ? body.scrollHeight : 0,
document.documentElement.scrollHeight,
body ? body.offsetHeight : 0,
document.documentElement.offsetHeight
],
fullWidth = max(widths),
fullHeight = max(heights),
windowWidth = window.innerWidth,
windowHeight = window.innerHeight,
arrangements = [],
scrollPad = 200,
yDelta = windowHeight - (windowHeight > scrollPad ? scrollPad : 0),
xDelta = windowWidth,
yPos = fullHeight - windowHeight,
xPos,
numArrangements;
if (fullWidth <= xDelta + 1)
fullWidth = xDelta;
document.documentElement.style.overflow = 'hidden';
if (fullPage) {
while (yPos > -yDelta) {
xPos = 0;
while (xPos < fullWidth) {
arrangements.push([xPos, yPos]);
xPos += xDelta;
}
yPos -= yDelta;
}
}
else
arrangements.push([originalX, originalY]);
const arText = [];
arrangements.forEach((x) => { arText.push('[' + x.join(',') + ']'); });
numArrangements = arrangements.length;
const cleanUp = () => {
document.documentElement.style.overflow = originalOverflowStyle;
if (body)
body.style.overflowY = originalBodyOverflowYStyle;
window.scrollTo(originalX, originalY);
};
(function processArrangements() {
if (!arrangements.length) {
cleanUp();
if (callback)
callback();
return;
}
let next = arrangements.shift();
if (fullPage)
window.scrollTo(next[0], next[1]);
const data = {
msg: 'capture',
x: fullPage ? window.scrollX : 0,
y: fullPage ? window.scrollY : 0,
complete: (numArrangements - arrangements.length) / numArrangements,
windowWidth: windowWidth,
totalWidth: fullPage ? fullWidth : window.innerWidth,
totalHeight: fullPage ? fullHeight : window.innerHeight,
devicePixelRatio: window.devicePixelRatio
};
window.setTimeout(() => {
const cleanUpTimeout = window.setTimeout(cleanUp, 1250);
chrome.runtime.sendMessage(data, (captured) => {
window.clearTimeout(cleanUpTimeout);
if (captured)
processArrangements();
else
cleanUp();
});
}, fullPage ? CAPTURE_DELAY : 0);
})();
};