Skip to content

Instantly share code, notes, and snippets.

@anadius
Created October 7, 2021 18:39
Show Gist options
  • Save anadius/0e799b2c1bad0f0d6b3adbabafb0bcf6 to your computer and use it in GitHub Desktop.
Save anadius/0e799b2c1bad0f0d6b3adbabafb0bcf6 to your computer and use it in GitHub Desktop.

Select the code below and add drag it to bookmarks bar. And if you want to read the full code see the file below.

javascript:(function()%7Bconst%20addJS%20%3D%20url%20%3D%3E%20new%20Promise((resolve%2C%20reject)%20%3D%3E%20%7B%0A%20%20const%20js%20%3D%20document.createElement('script')%3B%0A%20%20js.onload%20%3D%20resolve%3B%0A%20%20js.onerror%20%3D%20reject%3B%0A%20%20js.src%20%3D%20url%3B%0A%20%20document.body.appendChild(js)%3B%0A%7D)%3B%0A%0A(async%20()%20%3D%3E%20%7B%0A%20%20await%20Promise.all(%5B%0A%20%20%20%20addJS('https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Fjszip%403.7.1%2Fdist%2Fjszip.min.js')%2C%0A%20%20%20%20addJS('https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Ffile-saver-es%402.0.5%2Fdist%2FFileSaver.min.js')%0A%20%20%5D)%3B%0A%20%20const%20data%20%3D%20JSON.parse(document.querySelector('%23reader-init').innerHTML)%3B%0A%20%20const%20response%20%3D%20await%20fetch(data.manifest)%3B%0A%20%20const%20manifest%20%3D%20await%20response.json()%3B%0A%0A%20%20const%20zip%20%3D%20JSZip()%3B%0A%20%20const%20folder%20%3D%20%60%24%7Bdata.comic.name%7D%2F%24%7Bmanifest.metadata.title%7D%20-%20%24%7Bmanifest.metadata.subtitle%7D%2F%60%3B%0A%20%20for(let%20i%20%3D%200%3B%20i%20%3C%20manifest.readingOrder.length%3B%20%2B%2Bi)%20%7B%0A%20%20%20%20console.log(i)%3B%20document.title%20%3D%20i%3B%0A%20%20%20%20const%20page%20%3D%20manifest.readingOrder%5Bi%5D%3B%0A%20%20%20%20const%20extension%20%3D%20page.type.substr(page.type.indexOf('%2F')%20%2B%201)%3B%0A%20%20%20%20const%20name%20%3D%20(i).toString().padStart(3%2C%20'0')%3B%0A%20%20%20%20const%20filename%20%3D%20%60%24%7Bfolder%7D%24%7Bname%7D.%24%7Bextension%7D%60%3B%0A%20%20%20%20const%20resp%20%3D%20await%20fetch(page.href)%3B%0A%20%20%20%20zip.file(filename%2C%20await%20resp.blob())%3B%0A%20%20%7D%0A%20%20saveAs(await%20zip.generateAsync(%7Btype%3A%20'blob'%7D)%2C%20%60%24%7Bfolder%7D.zip%60.replace('%2F'%2C%20'%20-%20').replace('%2F'%2C%20''))%3B%0A%7D)()%3B%7D)()%3B
const addJS = url => new Promise((resolve, reject) => {
const js = document.createElement('script');
js.onload = resolve;
js.onerror = reject;
js.src = url;
document.body.appendChild(js);
});
(async () => {
await Promise.all([
addJS('https://cdn.jsdelivr.net/npm/[email protected]/dist/jszip.min.js'),
addJS('https://cdn.jsdelivr.net/npm/[email protected]/dist/FileSaver.min.js')
]);
const data = JSON.parse(document.querySelector('#reader-init').innerHTML);
const response = await fetch(data.manifest);
const manifest = await response.json();
const zip = JSZip();
const folder = `${data.comic.name}/${manifest.metadata.title} - ${manifest.metadata.subtitle}/`;
for(let i = 0; i < manifest.readingOrder.length; ++i) {
console.log(i); document.title = i;
const page = manifest.readingOrder[i];
const extension = page.type.substr(page.type.indexOf('/') + 1);
const name = (i).toString().padStart(3, '0');
const filename = `${folder}${name}.${extension}`;
const resp = await fetch(page.href);
zip.file(filename, await resp.blob());
}
saveAs(await zip.generateAsync({type: 'blob'}), `${folder}.zip`.replace('/', ' - ').replace('/', ''));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment