-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathytscroller.js
More file actions
91 lines (87 loc) · 3.09 KB
/
ytscroller.js
File metadata and controls
91 lines (87 loc) · 3.09 KB
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
/* keep video on top right when scrolling */
function videoAnchor() {
var miniplayer = document.getElementsByClassName("ytdMiniplayerComponentVisible")[0];
if (miniplayer) {
return;
}
var player = document.getElementById("movie_player");
var video = document.getElementsByTagName("video")[0];
var control = document.getElementsByClassName("ytp-chrome-bottom")[0];
var mastRect = document.getElementById("masthead-container").getBoundingClientRect();
var theaterRect = document.querySelector("#player-container.ytd-watch-flexy").getBoundingClientRect();
player.removeAttribute("style"); /* reset for theaterRect height */
var playerRect = player.getBoundingClientRect();
var videoRect = video.getBoundingClientRect();
if (theaterRect.bottom < mastRect.height) {
var comment = (
document.getElementById("primary-inner") ||
document.getElementById("primary")
);
var commentRect = comment.getBoundingClientRect();
var minRatio = 320 / videoRect.width;
var widthRatio = (window.innerWidth - commentRect.right) / videoRect.width;
var heightRatio = (window.innerHeight - mastRect.height) / videoRect.height;
player.style.position = "fixed";
player.style.right = "0";
player.style.top = mastRect.height + "px";
player.style.width = videoRect.width + "px";
player.style.height = videoRect.height + "px";
player.style.transformOrigin = "right top";
player.style.transform = "scale(" + Math.max(minRatio, Math.min(widthRatio, heightRatio)) + ")";
player.style.zIndex = "1500";
control.style.left = "12px";
control.style.width = (videoRect.width - 24) + "px";
video.style.left = 0;
}
else {
control.style.left = "12px";
control.style.width = (playerRect.width - 24) + "px";
video.style.left = (playerRect.width - videoRect.width) / 2 + "px";
}
}
window.addEventListener("scroll", videoAnchor);
window.addEventListener("resize", videoAnchor);
/* keep scroll position on timestamp click */
/* and add a scroll position return button in case it doesn't work */
(function () {
var x, y;
var returner = document.createElement("div");
returner.textContent = "▼";
returner.style.background = "red";
returner.style.color = "white";
returner.style.fontSize = "24px";
returner.style.position = "absolute";
returner.style.left = "50%";
returner.style.top = "0";
returner.style.transform = "translateX(-50%)";
returner.style.display = "none";
returner.style.borderRadius = "0 0 8px 8px";
returner.style.zIndex = "8000";
returner.style.cursor = "pointer";
document.body.appendChild(returner);
function returnToStamp() {
returner.style.display = "none";
window.scrollTo(x, y);
}
function scrollAnchor(e) {
var a = e.target.closest("a");
var t0;
function scrollToXY(t1) {
window.scrollTo(x, y);
if (typeof t0 == "undefined") {
t0 = t1;
}
if (t1 - t0 < 1) {
requestAnimationFrame(scrollToXY);
}
}
if (a && /[?&]t=\d+s/.test(a.href)) {
x = window.scrollX;
y = window.scrollY;
returner.style.display = "block";
requestAnimationFrame(scrollToXY);
}
}
window.addEventListener("mouseup", scrollAnchor);
returner.addEventListener("click", returnToStamp);
})();