Culling captions from YouTube

A few months back I posted a script to get captions from a Youtube video. That script worked best if the captions were added by a user; basically, if there was a captioner.

If you want to grab Youtube auto-generated captions, here is a better script:

var captionCollector = {
    captions : '',

    collect : function(){
      try {
        var currentCaption = document.getElementsByClassName("captions-text")[0].innerText.replace(/\n/,'');
        console.log(currentCaption);
      } catch (e) {

      }

    if(currentCaption) {
      if(captionCollector.captions.lastIndexOf(currentCaption) == -1) {
        captionCollector.captions += currentCaption;
      }
    }

      setTimeout(captionCollector.collect, 500);
    }
}

captionCollector.collect();

Then calle captionCollector.captions to print the captions to the console.

Published by

Estelle Weyl

My name is Estelle Weyl. I an open web evangelist and community engineer. I'm a consulting web developer, writing technical books with O'Reilly, running frontend workshops, and speaking about web development, performance, and other fun stuff all over the world. If you have any recommendations on topics for me to hit, please let me know via @estellevw.