Skip to content

Instantly share code, notes, and snippets.

@syuchan1005
Created March 15, 2018 11:01
Show Gist options
  • Save syuchan1005/10c8a7a5ebb75ca1f685f3bfc832e2de to your computer and use it in GitHub Desktop.
Save syuchan1005/10c8a7a5ebb75ca1f685f3bfc832e2de to your computer and use it in GitHub Desktop.
Youtube subtitle view (run addLyrics() function)
[
'https://unpkg.com/codemirror@5.35.0/lib/codemirror.js',
'https://code.jquery.com/jquery-3.3.1.min.js',
].forEach(url => {
const script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
});
[
'https://unpkg.com/codemirror@5.35.0/lib/codemirror.css',
].forEach(url => {
const link = document.createElement('link');
link.href = url;
link.rel = 'stylesheet';
document.head.appendChild(link);
});
function YTLyrics(vId, lang) {
lang = lang || 'en';
if (!vId) {
const query = location.search.substr(1).split('&')
.reduce((obj, v) => {
const pair = v.split('=');
obj[pair[0]] = pair[1];
return obj;
}, {});
vId = query.v;
}
const url = `https://video.google.com/timedtext?lang=${lang}&name=&v=${vId}`;
return fetch(url)
.then(response => response.text())
.then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
.then(data => data.getElementsByTagName('text'))
.then((collection) => {
let lyrics = '';
for (let i = 0; i < collection.length; i += 1) {
lyrics += `${collection.item(i).innerHTML}\n`;
}
return lyrics;
});
}
function addLyrics() {
if (!document.getElementById('lyrics-view')) {
jQuery('#player-ads').before('<div id="lyrics-view" style="font-size: 1.7rem"><div>');
}
YTLyrics()
.then(str => {
CodeMirror(document.getElementById('lyrics-view'), { lineNUmbers: true, value: str });
}).catch(err => {
console.error(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment