Created
October 29, 2018 08:51
-
-
Save kejun/a825285adb69de0a77b578cc265f3a20 to your computer and use it in GitHub Desktop.
记录youtube字幕
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
;(() => { | |
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