aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/static/js/common.js
blob: 2997f612e096272b7c693e502588175944ee2a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Q = document.querySelector.bind(document);
QA = document.querySelectorAll.bind(document);
function text(msg) { return document.createTextNode(msg); }
function clearNode(node) { while (node.firstChild) node.removeChild(node.firstChild); }
function toTimestamp(seconds) {
  var seconds = Math.floor(seconds);

  var minutes = Math.floor(seconds/60);
  var seconds = seconds % 60;

  var hours = Math.floor(minutes/60);
  var minutes = minutes % 60;

  if (hours) {
    return `0${hours}:`.slice(-3) + `0${minutes}:`.slice(-3) + `0${seconds}`.slice(-2);
  }
  return `0${minutes}:`.slice(-3) + `0${seconds}`.slice(-2);
}


var cur_track_idx = 0;
function getActiveTranscriptTrackIdx() {
    let textTracks = Q("video").textTracks;
    if (!textTracks.length) return;
    for (let i=0; i < textTracks.length; i++) {
        if (textTracks[i].mode == "showing") {
            cur_track_idx = i;
            return cur_track_idx;
        }
    }
    return cur_track_idx;
}
function getActiveTranscriptTrack() { return Q("video").textTracks[getActiveTranscriptTrackIdx()]; }

function getDefaultTranscriptTrackIdx() {
  let textTracks = Q("video").textTracks;
  return textTracks.length - 1;
}

function doXhr(url, callback=null) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.onload = (e) => {
      let ok = xhr.status >= 200 && xhr.status < 300;
      if (ok) callback(e.currentTarget.response);
      else alert(`${xhr.responseURL} status code: ${xhr.status}`);
    }
    xhr.send();
    return xhr;
}


window.addEventListener('DOMContentLoaded', function() {
    cur_track_idx = getDefaultTranscriptTrackIdx();
});