/
YouTube: Remember current time stamp.user.js
92 lines (82 loc) · 2.96 KB
/
YouTube: Remember current time stamp.user.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
// ==UserScript==
// @name YouTube: Remember current time stamp
// @description Puts time stamp in URL every time the video is paused, except if video is within its last 5 seconds.
// @author Sebastian Simon
// @include https://youtube.com/*
// @include https://youtube.com/
// @include https://www.youtube.com/*
// @include https://www.youtube.com/
// @grant none
// @run-at document-start
// @version 1.0.0
// @homepageURL https://github.com/SebastianSimon/youtube-save-time-stamp
// ==/UserScript==
/* eslint no-unused-vars: [ "error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" } ] */
/* eslint no-trailing-spaces: [ "error", { "skipBlankLines": true } ] */
addEventListener("DOMContentLoaded", function(){ "use strict";
addEventListener("pause", () => {
if(location.pathname.includes("/watch")){
const url = new URL(location),
[
currentTime,
totalTime
] = [
".ytp-time-current",
".ytp-time-duration"
].map((selector) => {
const timeStamp = document.querySelector(selector)?.textContent;
if(timeStamp){
const time = timeStamp.split(":").map(Number);
if(time.length === 4){
time[1] += 24 * time[0];
time.shift();
}
const {
timeParam,
totalSeconds
} = time.reduceRight((aggregator, measure) => {
const unit = aggregator.timeUnits.shift(),
scalar = aggregator.timeScalars.shift();
if(measure){
aggregator.timeParam = `${measure}${unit}${aggregator.timeParam}`;
aggregator.totalSeconds += scalar * measure;
}
return aggregator;
}, {
timeParam: "",
totalSeconds: 0,
timeUnits: [
"s",
"m",
"h"
],
timeScalars: [
1,
60,
60 * 60
]
});
return {
timeParam,
totalSeconds
};
}
return null;
});
if(currentTime){
if(currentTime.timeParam && currentTime.totalSeconds <= totalTime.totalSeconds - 5){
if(currentTime.timeParam !== url.searchParams.get("t")){
url.searchParams.set("t", currentTime.timeParam);
history.replaceState(history.state, "", url);
}
}
else{
url.searchParams.delete("t");
history.replaceState(history.state, "", url);
}
}
}
}, {
capture: true
});
});