Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FreshPenguin112
GitHub Repository: FreshPenguin112/bookmarklets
Path: blob/main/ytstamp.js
5051 views
1
var timestamps = document.createElement("div");
2
var time = 0;
3
4
function watchTime() {
5
var video = document.getElementsByTagName("video")[0];
6
time = Math.floor(video.currentTime);
7
requestAnimationFrame(watchTime);
8
}
9
10
function formatTime(seconds) {
11
var h = Math.floor(seconds / 3600);
12
var m = Math.floor(seconds / 60) - 60 * h;
13
var s = Math.floor(seconds) - 60 * m - 3600 * h;
14
return (h ? (h + ":" + String(m).padStart(2, 0)) : m) + ":" + String(s).padStart(2, 0);
15
}
16
17
function saveTimestamp(e) {
18
if (e.target.href && e.target.href.match(/[?&]t=\d/)) {
19
if (e.target.parentElement == timestamps) {
20
e.preventDefault();
21
var video = document.getElementsByTagName("video")[0];
22
video.currentTime = e.target.dataset.t;
23
}
24
var v = location.search.match(/v=([^&]+)/)[1];
25
var a = document.createElement("a");
26
a.href = "/watch?v=" + v + "&t=" + time;
27
a.dataset.t = time;
28
a.innerHTML = formatTime(time);
29
timestamps.appendChild(a);
30
timestamps.scrollTo(0, 0);
31
}
32
}
33
34
timestamps.style.background = "white";
35
timestamps.style.position = "fixed";
36
timestamps.style.maxHeight = "90%";
37
timestamps.style.bottom = "0";
38
timestamps.style.fontFamily = "Helvetica";
39
timestamps.style.fontSize = "16px";
40
timestamps.style.lineHeight = "20px";
41
timestamps.style.color = "pink";
42
timestamps.style.display = "flex";
43
timestamps.style.padding = "4px";
44
timestamps.style.flexDirection = "column-reverse";
45
timestamps.style.overflow = "scroll";
46
document.body.appendChild(timestamps);
47
48
watchTime();
49
50
window.addEventListener("click", saveTimestamp);
51
52