-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathenhancedYouTube.js
340 lines (322 loc) · 16 KB
/
enhancedYouTube.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// ==UserScript==
// @name Responsive YouTube Player & Timestamp Control & Player Controller for Roamresearch
// @author Connected Cognition Crumbs <c3founder@gmail.com>
// @require -
// @version 0.5
// @description Add timestamp controls to YouTube videos embedded in Roam and makes the player responsive.
// Parameters:
// Shortcuts:
// grabTitleKey: if in a DIRECT child block of the YT video,
// grabs the title and paste it to the beginning of the current block.
// grabTimeKey: if in ANY child blocks of the YT video,
// grabs the current time of the player and paste it to the beginning.
// normalSpeedKey: set the playback rate to 1
// speedUpKey: increase the rate by .25
// speedDownKey: decrease the rate by .25
// muteKey: mut the player
// volUpKey: increase volume by 10/100
// volDownKey: decrease volume by 10/100
// playPauseKey: play/pause the most recent player or the first one
// backwardKey: go backward 10 sec
// forwardKey: go forward 10 sec
// Player Size: Video height and width when the right sidebar is closed.
// @match https://*.roamresearch.com
/* mousetrap v1.6.5 craig.is/killing/mice */
(function (q, u, c) {
function v(a, b, g) { a.addEventListener ? a.addEventListener(b, g, !1) : a.attachEvent("on" + b, g) } function z(a) { if ("keypress" == a.type) { var b = String.fromCharCode(a.which); a.shiftKey || (b = b.toLowerCase()); return b } return n[a.which] ? n[a.which] : r[a.which] ? r[a.which] : String.fromCharCode(a.which).toLowerCase() } function F(a) { var b = []; a.shiftKey && b.push("shift"); a.altKey && b.push("alt"); a.ctrlKey && b.push("ctrl"); a.metaKey && b.push("meta"); return b } function w(a) {
return "shift" == a || "ctrl" == a || "alt" == a ||
"meta" == a
} function A(a, b) { var g, d = []; var e = a; "+" === e ? e = ["+"] : (e = e.replace(/\+{2}/g, "+plus"), e = e.split("+")); for (g = 0; g < e.length; ++g) { var m = e[g]; B[m] && (m = B[m]); b && "keypress" != b && C[m] && (m = C[m], d.push("shift")); w(m) && d.push(m) } e = m; g = b; if (!g) { if (!p) { p = {}; for (var c in n) 95 < c && 112 > c || n.hasOwnProperty(c) && (p[n[c]] = c) } g = p[e] ? "keydown" : "keypress" } "keypress" == g && d.length && (g = "keydown"); return { key: m, modifiers: d, action: g } } function D(a, b) { return null === a || a === u ? !1 : a === b ? !0 : D(a.parentNode, b) } function d(a) {
function b(a) {
a =
a || {}; var b = !1, l; for (l in p) a[l] ? b = !0 : p[l] = 0; b || (x = !1)
} function g(a, b, t, f, g, d) { var l, E = [], h = t.type; if (!k._callbacks[a]) return []; "keyup" == h && w(a) && (b = [a]); for (l = 0; l < k._callbacks[a].length; ++l) { var c = k._callbacks[a][l]; if ((f || !c.seq || p[c.seq] == c.level) && h == c.action) { var e; (e = "keypress" == h && !t.metaKey && !t.ctrlKey) || (e = c.modifiers, e = b.sort().join(",") === e.sort().join(",")); e && (e = f && c.seq == f && c.level == d, (!f && c.combo == g || e) && k._callbacks[a].splice(l, 1), E.push(c)) } } return E } function c(a, b, c, f) {
k.stopCallback(b,
b.target || b.srcElement, c, f) || !1 !== a(b, c) || (b.preventDefault ? b.preventDefault() : b.returnValue = !1, b.stopPropagation ? b.stopPropagation() : b.cancelBubble = !0)
} function e(a) { "number" !== typeof a.which && (a.which = a.keyCode); var b = z(a); b && ("keyup" == a.type && y === b ? y = !1 : k.handleKey(b, F(a), a)) } function m(a, g, t, f) {
function h(c) { return function () { x = c; ++p[a]; clearTimeout(q); q = setTimeout(b, 1E3) } } function l(g) { c(t, g, a); "keyup" !== f && (y = z(g)); setTimeout(b, 10) } for (var d = p[a] = 0; d < g.length; ++d) {
var e = d + 1 === g.length ? l : h(f ||
A(g[d + 1]).action); n(g[d], e, f, a, d)
}
} function n(a, b, c, f, d) { k._directMap[a + ":" + c] = b; a = a.replace(/\s+/g, " "); var e = a.split(" "); 1 < e.length ? m(a, e, b, c) : (c = A(a, c), k._callbacks[c.key] = k._callbacks[c.key] || [], g(c.key, c.modifiers, { type: c.action }, f, a, d), k._callbacks[c.key][f ? "unshift" : "push"]({ callback: b, modifiers: c.modifiers, action: c.action, seq: f, level: d, combo: a })) } var k = this; a = a || u; if (!(k instanceof d)) return new d(a); k.target = a; k._callbacks = {}; k._directMap = {}; var p = {}, q, y = !1, r = !1, x = !1; k._handleKey = function (a,
d, e) { var f = g(a, d, e), h; d = {}; var k = 0, l = !1; for (h = 0; h < f.length; ++h)f[h].seq && (k = Math.max(k, f[h].level)); for (h = 0; h < f.length; ++h)f[h].seq ? f[h].level == k && (l = !0, d[f[h].seq] = 1, c(f[h].callback, e, f[h].combo, f[h].seq)) : l || c(f[h].callback, e, f[h].combo); f = "keypress" == e.type && r; e.type != x || w(a) || f || b(d); r = l && "keydown" == e.type }; k._bindMultiple = function (a, b, c) { for (var d = 0; d < a.length; ++d)n(a[d], b, c) }; v(a, "keypress", e); v(a, "keydown", e); v(a, "keyup", e)
} if (q) {
var n = {
8: "backspace", 9: "tab", 13: "enter", 16: "shift", 17: "ctrl",
18: "alt", 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", 37: "left", 38: "up", 39: "right", 40: "down", 45: "ins", 46: "del", 91: "meta", 93: "meta", 224: "meta"
}, r = { 106: "*", 107: "+", 109: "-", 110: ".", 111: "/", 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", 221: "]", 222: "'" }, C = { "~": "`", "!": "1", "@": "2", "#": "3", $: "4", "%": "5", "^": "6", "&": "7", "*": "8", "(": "9", ")": "0", _: "-", "+": "=", ":": ";", '"': "'", "<": ",", ">": ".", "?": "/", "|": "\\" }, B = {
option: "alt", command: "meta", "return": "enter",
escape: "esc", plus: "+", mod: /Mac|iPod|iPhone|iPad/.test(navigator.platform) ? "meta" : "ctrl"
}, p; for (c = 1; 20 > c; ++c)n[111 + c] = "f" + c; for (c = 0; 9 >= c; ++c)n[c + 96] = c.toString(); d.prototype.bind = function (a, b, c) { a = a instanceof Array ? a : [a]; this._bindMultiple.call(this, a, b, c); return this }; d.prototype.unbind = function (a, b) { return this.bind.call(this, a, function () { }, b) }; d.prototype.trigger = function (a, b) { if (this._directMap[a + ":" + b]) this._directMap[a + ":" + b]({}, a); return this }; d.prototype.reset = function () {
this._callbacks = {};
this._directMap = {}; return this
}; d.prototype.stopCallback = function (a, b) { if (-1 < (" " + b.className + " ").indexOf(" mousetrap ") || D(b, this.target)) return !1; if ("composedPath" in a && "function" === typeof a.composedPath) { var c = a.composedPath()[0]; c !== a.target && (b = c) } return "INPUT" == b.tagName || "SELECT" == b.tagName || "TEXTAREA" == b.tagName || b.isContentEditable }; d.prototype.handleKey = function () { return this._handleKey.apply(this, arguments) }; d.addKeycodes = function (a) { for (var b in a) a.hasOwnProperty(b) && (n[b] = a[b]); p = null };
d.init = function () { var a = d(u), b; for (b in a) "_" !== b.charAt(0) && (d[b] = function (b) { return function () { return a[b].apply(a, arguments) } }(b)) }; d.init(); q.Mousetrap = d; "undefined" !== typeof module && module.exports && (module.exports = d); "function" === typeof define && define.amd && define(function () { return d })
}
})("undefined" !== typeof window ? window : null, "undefined" !== typeof window ? document : null);
Mousetrap.prototype.stopCallback = function () { return false }
const ytParams = window.ytParams;
const activateYtVideos = () => {
if (typeof (YT) == 'undefined') return;
Array.from(document.getElementsByTagName('IFRAME'))
.filter(iframe => iframe.src.includes('youtube.com'))
.forEach(ytEl => {
if (ytEl.closest('.rm-zoom-item') !== null) {
return; //ignore breadcrumbs and page log
}
const block = ytEl.closest('.roam-block-container');
let frameId;
if (!ytEl.classList.contains('yt-activated')) {
var ytId = extractVideoID(ytEl.src);
frameId = "yt-" + ytEl.closest('.roam-block').id;
var ytWrapper = document.createElement('div');
ytWrapper.id = frameId;
ytEl.parentNode.insertBefore(ytWrapper, ytEl);
ytEl.remove()
let newYTEl = new window.YT.Player(frameId, {videoId: ytId})
let iframe = document.getElementById(frameId)
iframe.classList.add('rm-iframe', 'rm-video-player', 'yt-activated')
iframe.closest('div').classList.add('rm-iframe__container', 'rm-video-player__container', 'hoverparent')
players.set(frameId, newYTEl);
} else {
frameId = ytEl.id
}
addTimestampControls(block, players.get(frameId));
});
};
const addTimestampControls = (block, player) => {
if (block.children.length < 2) return null;
const childBlocks = Array.from(block.children[1].querySelectorAll('.rm-block__input'));
childBlocks.forEach(child => {
const timestamp = getTimestamp(child);
const buttonIfPresent = getControlButton(child);
const timestampChanged = buttonIfPresent !== null && timestamp != buttonIfPresent.dataset.timestamp;
if (buttonIfPresent !== null && (timestamp === null || timestampChanged)) {
buttonIfPresent.remove();
}
if (timestamp !== null && (buttonIfPresent === null || timestampChanged)) {
addControlButton(child, timestamp, () => player.seekTo(timestamp, true));
}
});
};
const getControlButton = (block) => block.parentElement.querySelector('.timestamp-control');
const addControlButton = (block, timestamp, fn) => {
const button = document.createElement('button');
button.innerText = '►';
button.classList.add('timestamp-control');
button.dataset.timestamp = timestamp;
button.style.borderRadius = '50%';
button.addEventListener('click', fn);
block.parentElement.insertBefore(button, block);
};
const getTimestamp = (block) => {
var myspan = block.querySelector('span')
if (myspan === null) return null;
const blockText = myspan.textContent;
const matches = blockText.match(/^((?:\d+:)?\d+:\d\d)\D/); // start w/ m:ss or h:mm:ss
if (!matches || matches.length < 2) return null;
const timeParts = matches[1].split(':').map(part => parseInt(part));
if (timeParts.length == 3) return timeParts[0] * 3600 + timeParts[1] * 60 + timeParts[2];
else if (timeParts.length == 2) return timeParts[0] * 60 + timeParts[1];
else return null;
};
const extractVideoID = (url) => {
var regExp = /^(https?:\/\/)?((www\.)?(youtube(-nocookie)?|youtube.googleapis)\.com.*(v\/|v=|vi=|vi\/|e\/|embed\/\/?|user\/.*\/u\/\d+\/)|youtu\.be\/)([_0-9a-z-]+)/i;
var match = url.match(regExp);
if (match && match[7].length == 11) {
return match[7];
} else {
return null;
}
};
var ytReady = setInterval(() => {
if (typeof (YT) == 'undefined' || typeof (YT.Player) == 'undefined') {
const tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
clearInterval(ytReady);
}
}, 1000);
//Fill out the current block with the given text
function fillTheBlock(givenTxt) {
var setValue = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set;
let newTextArea = document.querySelector("textarea.rm-block-input");
setValue.call(newTextArea, givenTxt);
var e = new Event('input', { bubbles: true });
newTextArea.dispatchEvent(e);
}
//Getting the playing player
function whatIsPlaying() {
for (let player of players.values()) {
if (player.getPlayerState() == 1) {
return player;
}
}
return null;
}
//Getting the first uncued player
function whatIsPresent() {
for (let [playerId, player] of players) {
if (document.getElementById(playerId) === null) {
continue;
}
return player;
}
return null;
}
//Getting the target player
//1)playing or 2)most recent one or 3) the first one
function targetPlayer() {
var playing = whatIsPlaying();
if (playing !== null)
return playing;
if (paused !== null)
return paused;
//If nothing is playing return the fist player (if exists) that is not cued.
if (players.size > 0)
return whatIsPresent();
return null;
}
//Setting all shortcuts
//Title
Mousetrap.bind(ytParams.grabTitleKey, async function (e) {
e.preventDefault()
if (e.srcElement.localName == "textarea") {
var container = e.srcElement.closest('.roam-block-container');
var parContainer = container.parentElement.closest('.roam-block-container');
var myIframe = parContainer.querySelector("iframe");
if (myIframe === null) return false;
var oldTxt = document.querySelector("textarea.rm-block-input").value;
var newValue = players.get(myIframe.id).getVideoData().title + " " + oldTxt;
fillTheBlock(newValue);
}
return false;
}, 'keydown');
//TimeStamp
Mousetrap.bind(ytParams.grabTimeKey, async function (e) {
e.preventDefault()
var playing = targetPlayer();
if (playing !== null) {
var timeStr = new Date(playing.getCurrentTime() * 1000).toISOString().substr(11, 8)
var oldTxt = document.querySelector("textarea.rm-block-input").value;
fillTheBlock(timeStr + " " + oldTxt);
return false;
}
return false;
}, 'keydown');
//Play-Pause
Mousetrap.bind(ytParams.playPauseKey, async function (e) {
e.preventDefault();
var playing = whatIsPlaying();
//If something is playing => pause it
if (playing !== null) {
playing.pauseVideo();
paused = playing;
return false;
}
//If there is an active paused video => play it
if (paused !== null) {
paused.playVideo();
paused = null;
return false;
}
//If nothing is playing or paused => play the first video
if (players.size > 0) {
playing = whatIsPresent();
if (playing !== null) {
playing.playVideo();
return false;
}
}
return false;
}, 'keydown');
//Mute
Mousetrap.bind(ytParams.muteKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
if (playing.isMuted()) {
playing.unMute();
} else {
playing.mute();
}
return false;
}
return false;
}, 'keydown');
//Volume Up
Mousetrap.bind(ytParams.volUpKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
playing.setVolume(Math.min(playing.getVolume() + 10, 100))
return false;
}
return false;
}, 'keydown');
//Volume Down
Mousetrap.bind(ytParams.volDownKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
playing.setVolume(Math.max(playing.getVolume() - 10, 0))
return false;
}
return false;
}, 'keydown');
//Speed Up
Mousetrap.bind(ytParams.speedUpKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
playing.setPlaybackRate(Math.min(playing.getPlaybackRate() + 0.25, 2))
return false;
}
return false;
}, 'keydown');
//Speed Down
Mousetrap.bind(ytParams.speedDownKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
playing.setPlaybackRate(Math.max(playing.getPlaybackRate() - 0.25, 0))
return false;
}
return false;
}, 'keydown');
//Normal Speed
Mousetrap.bind(ytParams.normalSpeedKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
playing.setPlaybackRate(1, 0)
return false;
}
return false;
}, 'keydown');
//Move Forward
Mousetrap.bind(ytParams.forwardKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
var duration = playing.getDuration();
playing.seekTo(Math.min(playing.getCurrentTime() + 10, duration), true)
return false;
}
return false;
}, 'keydown');
//Move Backward
Mousetrap.bind(ytParams.backwardKey, async function (e) {
e.preventDefault();
var playing = targetPlayer();
if (playing !== null) {
var duration = playing.getDuration();
playing.seekTo(Math.max(playing.getCurrentTime() - 10, 0), true)
return false;
}
return false;
}, 'keydown');
var paused = null;
const players = new Map();
setInterval(activateYtVideos, 1000);