Video Game Music batch downloader

batch download for downloads.khinsider.com originalsoundtracks

目前为 2018-01-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Video Game Music batch downloader
  3. // @name:zh-TW Video Game Music 批量下載器
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1
  6. // @description batch download for downloads.khinsider.com originalsoundtracks
  7. // @description:zh-TW 批量下載 downloads.khinsider.com 的原聲帶
  8. // @author maple3142
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js
  10. // @match https://downloads.khinsider.com/game-soundtracks/album/*
  11. // @grant GM_xmlhttpRequest
  12. // @connect 66.90.93.122
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict'
  17. function downloadblob(url){
  18. return new Promise((resolve,reject)=>{
  19. GM_xmlhttpRequest({
  20. method: 'GET',
  21. url,
  22. responseType: 'blob',
  23. onload: res=>resolve(res.response)
  24. })
  25. })
  26. }
  27. function download(link,name){
  28. GM_xmlhttpRequest({
  29. method: 'GET',
  30. url: link,
  31. responseType: 'blob',
  32. onload(res){
  33. const url=URL.createObjectURL(res.response)
  34. const a=document.createElement('a')
  35. a.download=name
  36. a.href=url
  37. document.body.appendChild(a)
  38. a.click()
  39. a.remove()
  40. }
  41. })
  42. }
  43. $('a:contains("click to download")').on('click',e=>{
  44. e.preventDefault()
  45. $('.albumMassDownload').append(`
  46. <div>
  47. <span>Download progress:</span>
  48. <progress min="0" max="100" id="dp" value="0"></progress>
  49. </div>
  50. `)
  51.  
  52. const title=$('h2')[0].textContent
  53. const urls=$('tr>td.clickable-row:not([align])').toArray().map(el=>$(el).find('a').attr('href'))
  54. const requests=urls.map(e=>fetch(e).then(r=>r.text()))
  55. Promise.all(requests).then(ar=>ar.map(ht=>{
  56. const url=$(ht).find('a:contains("Click here to download as MP3")').attr('href')
  57. return {
  58. blob: downloadblob(url),
  59. name: decodeURIComponent(url.split('/').pop())
  60. }
  61. }).reduce((zip,file)=>{
  62. zip.file(file.name,file.blob)
  63. return zip
  64. },new JSZip()).generateAsync({type: 'blob'},meta=>{
  65. $('#dp').attr('value',parseInt(meta.percent))
  66. }).then(blob=>{
  67. const url=URL.createObjectURL(blob)
  68. const a=document.createElement('a')
  69. a.download=title+'.zip'
  70. a.href=url
  71. document.body.appendChild(a)
  72. a.click()
  73. a.remove()
  74. URL.revokeObjectURL(url)
  75. }))
  76. })
  77. })()