Skip to content

Instantly share code, notes, and snippets.

@gijigae
Created November 17, 2022 03:40
Show Gist options
  • Select an option

  • Save gijigae/b757c6f53f82c895ee333e1d03b4bdf4 to your computer and use it in GitHub Desktop.

Select an option

Save gijigae/b757c6f53f82c895ee333e1d03b4bdf4 to your computer and use it in GitHub Desktop.
Bookmarklet to copy YouTube transcript to Roam Research
javascript:void function(){(function(){const a=()=>document.querySelectorAll("ytd-transcript-renderer ytd-transcript-segment-renderer > div"),b=a=>{const b=Array.from(a),c=b.map(a=>{const b=Array.from(a.querySelectorAll("div")).slice(0,2),c=Array.from(a.querySelectorAll("yt-formatted-string")).slice(0,1);let d=b[0].textContent.trim();4===d.length%3Fd=`00:0${d}`:5===d.length%3Fd=`00:${d}`:7===d.length%26%26(d=`0${d}`);const e="{{[[video-timestamp]]: "+d+"}}",f=c[0].textContent.trim().replace(/(\r\n|\n|\r)/gm," ");return{timestamp:e,transcript:f}});let d="";c.forEach(a=>{d+=[`- ${a.timestamp}`,`${a.transcript}\n`].join(" ")}),navigator.clipboard.writeText(d)};let c=a();if(0!==c.length)return void b(c);const d=setInterval(()=>{try{document.querySelector("ytd-menu-service-item-renderer.style-scope:nth-child(2) > tp-yt-paper-item:nth-child(1) > yt-formatted-string:nth-child(2)").click()}catch(a){return void console.log("WARN: failed to click button opening a transcript")}clearInterval(d);const c=setInterval(()=>{const d=a();0===d.length||(b(d),clearInterval(c))},100)},100)})()}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment