Created
September 6, 2021 19:08
-
-
Save blood72/daa515f4aba97fd088a72833371d0226 to your computer and use it in GitHub Desktop.
khinsider batch downloader (mainly written by maple3142)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name khinsider Batch Downloader | |
// @description batch download for downloads.khinsider.com | |
// @version 1.0.0 | |
// @match https://downloads.khinsider.com/game-soundtracks/album/* | |
// @require https://cdn.jsdelivr.net/npm/web-streams-polyfill@3.1.1/dist/ponyfill.min.js | |
// @require https://cdn.jsdelivr.net/npm/streamsaver@2.0.5/StreamSaver.js | |
// @require https://cdn.jsdelivr.net/npm/streamsaver@2.0.5/examples/zip-stream.js | |
// @connect 23.237.126.42 | |
// @connect vgmsite.com | |
// @grant GM_xmlhttpRequest | |
// @inspired-by maple3142 (https://blog.maple3142.net/) - khinsider batch downloader | |
// ==/UserScript== | |
function download(url) { | |
return new Promise((resolve, reject) => { | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url, | |
responseType: 'blob', | |
onload: res => resolve(res.response), | |
}); | |
}); | |
} | |
let started = false; | |
$('a:contains("click to download")').on('click', async e => { | |
e.preventDefault(); | |
$(e.target).text("Don't close this tab until download complete."); | |
if (started) { | |
return; | |
} | |
started = true; | |
const title = $('h2')[0].textContent; | |
const files = $('tr > td.clickable-row:not([align])') | |
.toArray() | |
.map(el => $(el).find('a').attr('href')) | |
.map(url => { | |
const obj = {}; | |
obj.pageUrl = url; | |
obj.blobPromise = fetch(url) | |
.then(r => r.text()) | |
.then(html => { | |
obj.url = $(html).find('a:contains("Click here to download as FLAC")').attr('href') | |
|| $(html).find('a:contains("Click here to download as MP3")').attr('href'); | |
obj.name = decodeURIComponent(obj.url.split('/').pop()); | |
return download(obj.url); | |
}); | |
return obj; | |
}); | |
const ws = streamSaver.createWriteStream(title + '.zip'); | |
new ZIP({ | |
async pull(ctrl) { | |
const f = files.shift(); | |
const stream = new Response(await f.blobPromise).body; | |
ctrl.enqueue({ | |
name: f.name, | |
stream: () => stream, | |
}); | |
if (files.length === 0) { | |
ctrl.close(); | |
} | |
} | |
}) | |
.pipeTo(ws) | |
.then(console.log, console.error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An error may occur in StreamSaver.js.
This is related to cookies.
This script uses third-party cookies to use, so please check if they are blocked.