forked from EncryptedCurse/pixieDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
153 lines (126 loc) · 3.96 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let running = false;
let autoScrolling = true;
let status;
let albumZip;
// run receiver: extension is triggered by a message from background.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.run == 'true') {
if (running) {
alert('There is a download already in progress!');
} else {
running = true;
init();
}
}
}
);
function init() {
const compatCheck = (document.getElementById('meta_og_site_name').content.toUpperCase() == 'PIXIESET');
if (!compatCheck) {
alert("This doesn't look like a Pixieset website.");
running = false;
} else {
if (!document.getElementById('statusText')) {
// add status text to navbar
let nav = document.evaluate(
'//div[@id="nav-icon-buttons" and @class="pull-right"]',
document, null, XPathResult.ANY_TYPE, null
);
if (nav) {
nav = nav.iterateNext();
let div = document.createElement('div');
div.className = 'nav-buy-photos__container';
div.innerHTML = `
<a id="statusText" title="pixieDownloader" style="cursor: default; font-weight: bold; font-size: 15px" class="nav-buy-photos">Ready</a></div>
<span class="f-24 spacer-right-15 spacer-left-15 bl o-20"></span>
`;
nav.insertBefore(div, nav.firstChild);
status = document.getElementById('statusText');
}
}
if (autoScrolling) {
// scroll to the bottom to load all images in the current album
let lastScrollHeight = 0;
function autoScroll() {
let scrollHeight = document.documentElement.scrollHeight;
if (scrollHeight != lastScrollHeight) {
lastScrollHeight = scrollHeight;
document.documentElement.scrollTop = scrollHeight;
}
}
window.setInterval(autoScroll, 50);
}
// initialize JSZip object
albumZip = new JSZip();
// give it some time to (hopefully) finish completely scrolling/loading first
setTimeout(() => { zip() }, 2000);
}
}
// helper function to update navbar status text
function setStatus(text, color = null) {
status.style.color = color;
status.innerText = text;
}
function zip() {
setStatus('Preparing...', '#45b0e6');
// locate all <img> elements
const container = document.getElementById('gamma-container');
const imgElements = container.getElementsByTagName('img');
const re_imgUrl = /(.*images.pixieset.*-)(.*)(.jpg)/;
// JSON data structure to store { URL, filename, base64 encoding } of each image
let imgObjects = [];
for (let i = 0; i < imgElements.length; i++) {
const currEle = imgElements[i];
const currSrc = currEle.currentSrc;
// filter out irrelevant images (i.e. site assets)
if (currSrc.match(re_imgUrl)) {
// obtain largest size available ('xxlarge')
const newUrl = currSrc.replace(re_imgUrl, '$1xxlarge$3');
// obtain original filename
const origName = currEle.alt;
imgObjects.push({ url: newUrl, name: origName, base64: '' });
}
}
// set default ZIP filename — combination of window title and current album
let albumName = window.location.pathname.split('/');
albumName = albumName.pop() || albumName.pop();
let zipName = document.title + ' - ' + albumName;
setStatus('Downloading...', '#eca142');
chrome.runtime.sendMessage(
{ array: imgObjects },
array => {
// add all images to ZIP
array.forEach(obj => albumZip.file(
obj.name,
obj.base64,
{ base64: true }
));
// serve ZIP to user
download(albumZip, zipName, true);
}
);
}
function download(zip, name, ask) {
zip.generateAsync({ type:'blob' }).then(blob => {
if (ask) {
name = prompt('What should the name of the ZIP file be?', name);
if (name === null) {
setStatus('Cancelled', '#e71d1d');
running = false;
return;
};
}
// https://stackoverflow.com/a/9834261
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = name + '.zip';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
setStatus('Finished', '#37ae3d');
running = false;
});
}