summaryrefslogtreecommitdiffstats
path: root/assets/js/watch.js
blob: 0126c5863789b50cc81957aaf4aff5857827795f (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function toggle_parent(target) {
  body = target.parentNode.parentNode.children[1];
  if (body.style.display === null || body.style.display === "") {
    target.innerHTML = "[ + ]";
    body.style.display = "none";
  } else {
    target.innerHTML = "[ - ]";
    body.style.display = "";
  }
}

function toggle_comments(target) {
  body = target.parentNode.parentNode.parentNode.children[1];
  if (body.style.display === null || body.style.display === "") {
    target.innerHTML = "[ + ]";
    body.style.display = "none";
  } else {
    target.innerHTML = "[ - ]";
    body.style.display = "";
  }
}

function swap_comments(source) {
  if (source == "youtube") {
    get_youtube_comments();
  } else if (source == "reddit") {
    get_reddit_comments();
  }
}

String.prototype.supplant = function(o) {
  return this.replace(/{([^{}]*)}/g, function(a, b) {
    var r = o[b];
    return typeof r === "string" || typeof r === "number" ? r : a;
  });
};

function show_youtube_replies(target) {
  body = target.parentNode.parentNode.children[1];
  body.style.display = "";

  target.innerHTML = "Hide replies";
  target.setAttribute("onclick", "hide_youtube_replies(this)");
}

function hide_youtube_replies(target) {
  body = target.parentNode.parentNode.children[1];
  body.style.display = "none";

  target.innerHTML = "Show replies";
  target.setAttribute("onclick", "show_youtube_replies(this)");
}

function download_video(target) {
  var title = target.getAttribute("data-title");
  var children = document.getElementById("download_widget").children;
  var progress = document.getElementById("download-progress");
  var url = "";

  document.getElementById("progress-container").style.display = "";

  for (i = 0; i < children.length; i++) {
    if (children[i].selected) {
      url = children[i].getAttribute("data-url");
    }
  }

  var xhr = new XMLHttpRequest();
  xhr.open("GET", url);
  xhr.responseType = "arraybuffer";
  
  xhr.onprogress = function(event) {
    if (event.lengthComputable) {
      progress.style.width = "" + (event.loaded / event.total)*100 + "%";
    }
  };

  xhr.onload = function(event) {
    if (event.currentTarget.status != 200) {
      console.log("Downloading " + title + " failed.")
      document.getElementById("progress-container").style.display = "none";
      progress.style.width = "0%";

      return;
    }

    var data = new Blob([xhr.response], {'type' : 'video/mp4'});
    var videoFile = window.URL.createObjectURL(data);

    var link = document.createElement('a');
    link.href = videoFile;
    link.setAttribute('download', title);
    document.body.appendChild(link);

    window.requestAnimationFrame(function() {
      var event = new MouseEvent('click');
      link.dispatchEvent(event);
      document.body.removeChild(link);
    });

    document.getElementById("progress-container").style.display = "none";
    progress.style.width = "0%";
  };

  xhr.send(null);
}