|
// Manga Plus by Shueisha |
|
// https://mangaplus.shueisha.co.jp |
|
// Note: You will have to scroll down all the way first to make sure everything is loaded |
|
|
|
async function main() { |
|
function loadScript(src) { |
|
return new Promise((resolve, reject) => { |
|
// Check if the script is already loaded |
|
const existingScript = document.querySelector(`script[src="${src}"]`); |
|
if (existingScript) { |
|
// Script already loaded, resolve immediately |
|
resolve(); |
|
return; |
|
} |
|
|
|
// Create a script element and set the src |
|
const script = document.createElement('script'); |
|
script.src = src; |
|
script.async = true; // Ensure it loads asynchronously |
|
|
|
// Resolve when the script is successfully loaded |
|
script.onload = () => resolve(); |
|
|
|
// Reject the promise if there's an error loading the script |
|
script.onerror = () => reject(new Error(`Failed to load script: ${src}`)); |
|
|
|
// Append the script to the document head to start loading |
|
document.head.appendChild(script); |
|
}); |
|
} |
|
|
|
await loadScript('https://cdn.jsdelivr.net/npm/[email protected]/dist/jszip.min.js'); |
|
const JSZip = window.JSZip; |
|
|
|
function canvasToBlob(canvas, type = 'image/jpeg', quality = 1) { |
|
return new Promise((resolve, reject) => { |
|
canvas.toBlob((blob) => { |
|
blob ? resolve(blob) : reject(new Error('Canvas toBlob() failed')); |
|
}, type, quality); |
|
}); |
|
} |
|
|
|
async function imageToBlob(image, type = 'image/jpeg', quality = 1) { |
|
const canvas = document.createElement('canvas'); |
|
canvas.width = image.naturalWidth; |
|
canvas.height = image.naturalHeight; |
|
|
|
const ctx = canvas.getContext('2d'); |
|
ctx.drawImage(image, 0, 0); |
|
|
|
return canvasToBlob(canvas, type, quality); |
|
} |
|
|
|
async function createZipOfJPGs(jpgBlobs, name = "file") { |
|
const zip = new JSZip(); |
|
|
|
// Loop through the blobs and add them to the ZIP archive |
|
jpgBlobs.forEach((blob, index) => { |
|
// Get the file extension based on the blob's type |
|
const fileType = blob.type.split("/")[1]; // Extracting the type (e.g., "jpeg" from "image/jpeg") |
|
const fileExtension = fileType === "jpeg" ? "jpg" : fileType; // Default to jpg for jpeg |
|
const fileName = `page${index + 1}.${fileExtension}`; // Naming the files with detected extension |
|
zip.file(fileName, blob); |
|
}); |
|
|
|
// Generate the ZIP file as a blob |
|
const zipBlob = await zip.generateAsync({ type: "blob" }); |
|
|
|
// Create a download link to save the ZIP file |
|
const downloadLink = document.createElement("a"); |
|
downloadLink.href = URL.createObjectURL(zipBlob); |
|
downloadLink.download = `${name}.zip`; |
|
downloadLink.click(); |
|
} |
|
|
|
|
|
const title = document.querySelector(":has(a > h1) > a").innerText; |
|
const chapter = document.querySelector(":has(a > h1) > div > p").innerText; |
|
const imageElements = [...document.querySelectorAll(".zao-image")] |
|
const imageBlobs = await Promise.all(imageElements.map(imageToBlob)) |
|
await createZipOfJPGs(imageBlobs, `${title} - ${chapter}`) |
|
} |
|
|
|
main(); |