Last active
July 6, 2020 14:12
-
-
Save djosix/fba87de76507c9b4f19064579bfb410d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
if (location.pathname.startsWith('/play')) { | |
(() => { | |
function downloadVideo(print = console.log) { | |
let url = | |
document | |
.querySelector('video') | |
.firstElementChild | |
.getAttribute('src'); | |
let dir = url.slice(0, url.lastIndexOf('/') + 1); | |
let finished = 0; | |
let total; | |
function getList(link) { | |
return new Promise(resolve => { | |
let xhr = new XMLHttpRequest(); | |
xhr.withCredentials = true; | |
xhr.onreadystatechange = () => { | |
if (xhr.readyState == 4) { | |
resolve( | |
xhr | |
.responseText | |
.trim() | |
.split('\n') | |
.filter(url => url[0] !== '#') | |
); | |
} | |
}; | |
xhr.open('GET', link, true); | |
xhr.send(); | |
}); | |
} | |
function getBlob(link) { | |
return new Promise(resolve => { | |
let xhr = new XMLHttpRequest(); | |
xhr.responseType = 'blob'; | |
xhr.withCredentials = true; | |
xhr.onreadystatechange = () => { | |
if (xhr.readyState == 4) { | |
print(`${Math.round(10000 * ++finished / total) / 100}%`); | |
resolve(xhr.response); | |
} | |
}; | |
xhr.open('GET', link, true); | |
xhr.send(); | |
}); | |
} | |
return new Promise(resolve => { | |
getList(url) | |
.then(list => { | |
total = list.length; | |
Promise | |
.all(list.map(file => getBlob(dir + file))) | |
.then(blobs => { | |
let blob = new Blob(blobs, { type: 'text/vnd.trolltech.linguist' }); | |
let a = document.createElement('a'); | |
a.href = URL.createObjectURL(blob); | |
a.download = `${document.title}.mpeg`; | |
a.click(); | |
resolve(); | |
}); | |
}); | |
}); | |
} | |
function addDownloadButton() { | |
let button = document.createElement('div'); | |
button.classList.add('video-action-btn', 'download'); | |
let icon = document.createElement('i'); | |
icon.classList.add('glyphicon', 'glyphicon-save', 'downloadIcon'); | |
button.appendChild(icon); | |
let text = document.createElement('span'); | |
text.classList.add('text'); | |
text.innerText = '幹你娘載爆'; | |
button.appendChild(text); | |
document.querySelector('.video-action').appendChild(button); | |
let downloading = false; | |
button.onclick = () => { | |
if (downloading) { | |
alert('已在下載中'); | |
return; | |
} | |
downloading = true; | |
downloadVideo((message) => { | |
text.innerText = message; | |
}).then(() => { | |
text.innerText = '幹你娘載爆'; | |
downloading = false; | |
}); | |
}; | |
} | |
// downloadVideo(); | |
addDownloadButton(); | |
})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment