-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeRangeLooper.js
More file actions
189 lines (141 loc) · 5.32 KB
/
Copy pathTimeRangeLooper.js
File metadata and controls
189 lines (141 loc) · 5.32 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
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
// ==UserScript==
// @name TimeRangeLooper
// @version 0.0.0
// @description Lets you loop media through an start time to an end time.
// @author Ervum
// @match *://*/*
// ==/UserScript==
(function() {
'use strict';
let ApplyButtonClickListener;
let SavedTimeRange;
let StartInput;
let EndInput;
let ApplyButton;
let VideoURL;
let Videos;
let Start;
let End;
setTimeout(() => {
function SetCurrentTime(Input) {
const PlayingVideo = Array.from(Videos).find((Video) => !(Video.paused));
if (PlayingVideo) {
Input.value = PlayingVideo.currentTime;
} else {
Input.value = 0;
}
ApplyTimeRange();
}
Videos = Array.from(document.getElementsByTagName('video'));
if ((Videos.length) === 0) {
return;
}
VideoURL = (window.location.href);
SavedTimeRange = JSON.parse(localStorage.getItem(VideoURL));
StartInput = CreateInput('Start', SavedTimeRange ? (SavedTimeRange.Start) : '');
EndInput = CreateInput('End', SavedTimeRange ? (SavedTimeRange.End) : '');
ApplyButton = CreateButton(SavedTimeRange ? 'Re-apply' : 'Apply', ApplyTimeRange);
const SetStartButton = CreateButton('Set current time as start', () => {
SetCurrentTime(StartInput);
});
const SetEndButton = CreateButton('Set current time as end', () => {
SetCurrentTime(EndInput);
});
if (IsDarkMode()) {
StyleControlsForDarkMode(StartInput, EndInput, ApplyButton, SetStartButton, SetEndButton);
}
const ControlsContainer = CreateControlsContainer([SetStartButton, StartInput, ApplyButton, EndInput, SetEndButton]);
document.body.appendChild(ControlsContainer);
ApplyTimeRange();
function StyleControlsForDarkMode(...Elements) {
for (const Element of Elements) {
Element.style.backgroundColor = 'black';
Element.style.color = 'white';
}
}
function CreateControlsContainer(Children) {
const Container = document.createElement('div');
Container.style.position = 'absolute';
Container.style.top = '95%';
Container.style.left = '50%';
Container.style.transform = 'translate(-50%, 0%)';
Container.style.display = 'flex';
Container.style.alignItems = 'center';
Container.style.zIndex = '9999';
Container.style.position = 'fixed';
Children.forEach((Child) => Container.appendChild(Child));
return Container;
}
}, 500);
function IsDarkMode() {
return (window.matchMedia) && (window.matchMedia('(prefers-color-scheme: dark)').matches);
}
function CreateButton(Text, OnClickFunction) {
const Button = document.createElement('button');
Button.innerText = Text;
Button.style.cursor = 'pointer';
Button.style.width = '8vw';
Button.style.marginRight = '1vw';
Button.style.marginLeft = '1vw';
Button.style.borderRadius = '1vh';
Button.style.fontWeight = 'bold';
Button.style.textAlign = 'center';
Button.style.border = '0.1vh solid #fff';
Button.classList.add('click-animation');
Button.onclick = OnClickFunction;
return Button;
}
function CreateInput(PlaceHolder, Value) {
const Input = document.createElement('input');
Input.type = 'text';
Input.placeholder = PlaceHolder;
Input.style.width = '6vw';
Input.style.marginRight = '1vw';
Input.style.marginLeft = '1vw';
Input.style.borderRadius = '1vh';
Input.style.textAlign = 'center';
Input.style.border = '0.1vh solid #fff';
Input.style.margin = '0.5vh';
Input.classList.add('click-animation');
Input.value = Value;
return Input;
}
function ApplyTimeRange() {
Start = parseFloat(StartInput.value);
End = parseFloat(EndInput.value);
if (!isNaN(Start) && !isNaN(End)) {
Videos.forEach((Video) => {
localStorage.setItem(VideoURL, JSON.stringify({ Start: Start, End: End }));
Video.setAttribute('loop', '');
ApplyButtonClickListener = function() {
if ((Video.currentTime) < Start || (Video.currentTime) > End) {
Video.currentTime = Start;
}
};
if (SavedTimeRange) {
ApplyButton.innerText = 'Re-applied';
} else {
ApplyButton.innerText = 'Applied';
}
Video.addEventListener('timeupdate', ApplyButtonClickListener);
Video.currentTime = Start;
setTimeout(() => {
ApplyButton.innerText = 'Re-apply';
}, 500);
});
}
}
})();
const Style = document.createElement('style');
Style.innerHTML = `
.click-animation {
transition: all 0.2s;
}
.click-animation:hover {
transform: scale(1.1);
}
.click-animation:active {
transform: scale(0.9);
}
`;
document.head.appendChild(Style);