Last active
March 31, 2018 12:05
-
-
Save Antti/175a7ffc1f0746180c0553537b08e246 to your computer and use it in GitHub Desktop.
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
(function() { | |
'use strict'; | |
function download(filename, data) { | |
var pom = document.createElement('a'); | |
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data)); | |
pom.setAttribute('download', filename); | |
if (document.createEvent) { | |
var event = document.createEvent('MouseEvents'); | |
event.initEvent('click', true, true); | |
pom.dispatchEvent(event); | |
} | |
else { | |
pom.click(); | |
} | |
} | |
function DownloadSub() { | |
let pagesNumber = document.querySelectorAll('.chic-pages ul li').length; | |
let url = `${document.location.protocol}//${document.location.host}${document.location.pathname}`; | |
let subs = Array.from({length: pagesNumber}, (x,i) => i + 1).map((pageNumber) => { | |
return fetch(`${url}?Orig_page=${pageNumber}`).then((response) => { | |
return response.text(); | |
}).then((text) => { | |
let parser = new DOMParser(); | |
let doc = parser.parseFromString(text, 'text/html'); | |
let subText = Array.from(doc.querySelectorAll(".t div:nth-child(1) .text")).map(e => e.innerText); | |
let timingsStart = Array.from(doc.querySelectorAll("#Tr span.t1")).map(e => e.innerText); | |
let timingsEnd = Array.from(doc.querySelectorAll("#Tr span.t2")).map(e => e.innerText); | |
let normalizeTime = (time) => { | |
var res = time.replace(".", ","); | |
if (res.match(/:/g).length == 1) { | |
return "00:"+res; | |
} else { | |
return res; | |
} | |
}; | |
return subText.map((t, i) => { | |
return { | |
text: t.replace("\n\n","\n"), | |
start: normalizeTime(timingsStart[i]), | |
end: normalizeTime(timingsEnd[i]) | |
}; | |
}); | |
}); | |
}); | |
Promise.all(subs).then((allPromises) => { | |
let allSubs = allPromises.reduce((acc, val) => acc.concat(val), []); | |
let text = allSubs.map((s, i) => `${i+1}\n${s.start} --> ${s.end}\n${s.text}`).join("\n\n"); | |
download("subs.srt", text); | |
}); | |
} | |
let downloadButton = document.createElement("div"); | |
downloadButton.innerHTML = "Download sub"; | |
downloadButton.classList.add("group", "btn", "btn-small"); | |
downloadButton.addEventListener("click", DownloadSub); | |
let element = document.querySelector("#tb-main div div.tb-progress"); | |
element.parentNode.insertBefore(downloadButton, element.nextSibling); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment