aboutsummaryrefslogtreecommitdiff
path: root/watch.js
blob: be0c7ef1ab5566539f319f5e6cf8529c83cbcb44 (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
/* This is a bookmarklet. Copy it to a bookmark in your web browser and
   add javascript: in front of it. */

var watch = function () {
  if (window.watchclicked == undefined)
    window.watchclicked = 0;
  if (window.watchlasttime == undefined)
    window.watchlasttime = 0;
  var vid = function (href) {
    return 'watch:' + href.replace(/(https?:\/?\/?www\.youtube\.com\/watch.*)&list=.*$/,'$1');
  };
  var reset = function () {
    window.watchclicked = 0;
    Array.from(document.getElementsByTagName('a')).forEach(function (el) {
      el.onclick = undefined;
      el.style.cursor = el.getAttribute('data-watch-cursor-orig');
    });
  };
  var open = function (ev) {
    reset();
    location.href = vid(ev.target.href);
    ev.preventDefault();
  };
  var d = new Date();
  if (d.getTime() - window.watchlasttime <= 500) { /* double click */
    location.href = vid(location.href);
    window.watchlasttime = d.getTime();
    return;
  }
  window.watchlasttime = d.getTime();
  if (window.watchclicked == 0) {
    window.watchclicked = 1;
    Array.from(document.getElementsByTagName('a')).forEach(function (el) {
      el.onclick = open;
      el.setAttribute('data-watch-cursor-orig', el.style.cursor);
      el.style.setProperty('cursor', 'alias', 'important');
    });
  } else {
    reset();
  }
};
watch();