Video Game Music 批量下載器

批量下載 downloads.khinsider.com 的原聲帶

目前為 2018-01-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Video Game Music batch downloader
// @name:zh-TW   Video Game Music 批量下載器
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  batch download for downloads.khinsider.com originalsoundtracks
// @description:zh-TW 批量下載 downloads.khinsider.com 的原聲帶
// @author       maple3142
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js
// @match        https://downloads.khinsider.com/game-soundtracks/album/*
// @grant        GM_xmlhttpRequest
// @connect      66.90.93.122
// ==/UserScript==

(function() {
	'use strict'
	function downloadblob(url){
		return new Promise((resolve,reject)=>{
			GM_xmlhttpRequest({
				method: 'GET',
				url,
				responseType: 'blob',
				onload: res=>resolve(res.response)
			})
		})
	}
	function download(link,name){
		GM_xmlhttpRequest({
			method: 'GET',
			url: link,
			responseType: 'blob',
			onload(res){
				const url=URL.createObjectURL(res.response)
				const a=document.createElement('a')
				a.download=name
				a.href=url
				document.body.appendChild(a)
				a.click()
				a.remove()
			}
		})
	}
	$('a:contains("click to download")').on('click',e=>{
		e.preventDefault()
		$('.albumMassDownload').append(`
<div>
<span>Download progress:</span>
<progress min="0" max="100" id="dp" value="0"></progress>
</div>
`)

		const title=$('h2')[0].textContent
		const urls=$('tr>td.clickable-row:not([align])').toArray().map(el=>$(el).find('a').attr('href'))
		const requests=urls.map(e=>fetch(e).then(r=>r.text()))
		Promise.all(requests).then(ar=>ar.map(ht=>{
			const url=$(ht).find('a:contains("Click here to download as MP3")').attr('href')
			return {
				blob: downloadblob(url),
				name: decodeURIComponent(url.split('/').pop())
			}
		}).reduce((zip,file)=>{
			zip.file(file.name,file.blob)
			return zip
		},new JSZip()).generateAsync({type: 'blob'},meta=>{
			$('#dp').attr('value',parseInt(meta.percent))
		}).then(blob=>{
			const url=URL.createObjectURL(blob)
			const a=document.createElement('a')
			a.download=title+'.zip'
			a.href=url
			document.body.appendChild(a)
			a.click()
			a.remove()
			URL.revokeObjectURL(url)
		}))
	})
})()