Skip to content

Instantly share code, notes, and snippets.

@kejun
Created October 29, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kejun/a825285adb69de0a77b578cc265f3a20 to your computer and use it in GitHub Desktop.
Save kejun/a825285adb69de0a77b578cc265f3a20 to your computer and use it in GitHub Desktop.
记录youtube字幕
;(() => {
const mergeText = (str, text) => {
if (str === '') {
return text;
}
if (!text || text === ' ' || str === text) {
return str;
}
str = str.replace(/\s+/g, ' ')
.replace(/\t|\r|\n/g, '');
const words = str.split(' ').filter(w => w.trim() !== '').map(w => w.trim());
var i = words.length;
var regex = s => new RegExp(`${ s.replace(/' '/g, '\s+') }$`, 'i');
while(!regex(words.slice(0, i).join(' ').trim()).test(text.trim()) && i > 0) {
i--;
}
return `${ text } ${ words.slice(i === words.length ? i + 1 : i).join(' ').trim() }`.trim();
}
let scripts = '';
let timer;
const record = () => {
const targetNode = document.querySelector('.captions-text');
if (targetNode) {
scripts = mergeText(targetNode.innerText, scripts);
console.clear();
console.log(scripts);
}
timer = setTimeout(() => record(), 100);
}
window.__stopRecord = () => clearTimeout(timer);
record();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment