-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
206 lines (169 loc) · 5.04 KB
/
Copy pathcontent.js
File metadata and controls
206 lines (169 loc) · 5.04 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const utils = window.KickTimeSaver.utils;
class KickVideoTimeSaver {
constructor() {
this.initializeVariables();
this.init();
}
async init() {
this.waitForVideo();
this.observeUrlChanges();
}
isVOD() {
const url = window.location.href;
return url.match(/kick\.com\/[^\/]+\/videos\/([^\/\?]+)/)
}
waitForVideo() {
const checkForVideo = () => {
const video = document.querySelector('video') ||
document.querySelector('video[src]') ||
document.querySelector('.video-player video');
if (video && !this.isInitialized && this.isVOD()) {
this.setupVideoHandlers(video);
} else if (!video && this.isInitialized) {
this.cleanup();
}
};
checkForVideo();
setInterval(checkForVideo, 2000);
const observer = new MutationObserver(checkForVideo);
observer.observe(document.body, {
childList: true,
subtree: true
});
}
async setupVideoHandlers(video) {
this.video = video;
this.streamInfo.id = this.getStreamId();
this.isInitialized = true;
console.log('Kick Video Time Saver: Video detected', this.streamInfo.id);
video.addEventListener('loadedmetadata', () => {
this.updateName();
this.loadSavedTime();
});
this.startPeriodicSave();
video.addEventListener('pause', () => this.saveCurrentTime());
video.addEventListener('seeked', () => this.saveCurrentTime());
window.addEventListener('beforeunload', () => this.saveCurrentTime());
}
getStreamName() {
const metaDescription = document.querySelector('meta[name="description"]');
if (metaDescription && metaDescription.content) {
const description = metaDescription.content.trim();
if (description && description !== '') {
return description;
}
}
const title = document.title;
if (title && title !== 'Kick') {
return title.replace(' - Kick', '').trim();
}
return 'No title';
}
getStreamId() {
const url = window.location.href;
const videoMatch = url.match(/kick\.com\/[^\/]+\/videos\/([^\/\?]+)/);
if (videoMatch) {
return videoMatch[1];
}
return 'unknown';
}
async loadSavedTime() {
try {
const key = this.streamInfo.id;
const savedData = await utils.getSavedDataObj(key);
if (savedData && savedData.timestamp && this.video && !utils.extractTimecode()) {
// Wait a bit for video to be fully loaded
setTimeout(() => {
if (this.video && this.video.duration > savedData.timestamp) {
this.video.currentTime = savedData.timestamp;
console.log(`Kick Video Time Saver: Restored time to ${savedData.timestamp}s`);
this.isLoaded = true;
}
}, 1000);
} else {
this.isLoaded = true;
}
} catch (error) {
console.error('Error loading saved time:', error);
this.isLoaded = true;
}
}
getStreamerName(url) {
const re = /^https?:\/\/(?:www\.)?kick\.com\/([^/]+)\/videos/;
const match = url.match(re);
return match ? match[1] : null;
}
async saveCurrentTime() {
if (!this.video || !this.streamInfo.id || !this.isLoaded) return;
const currentTime = this.video.currentTime;
const duration = this.video.duration;
try {
const key = this.streamInfo.id;
const data = {
timestamp: currentTime,
duration,
savedAt: Date.now(),
streamName: this.streamInfo.title,
streamerName: this.getStreamerName(window.location.href),
url: window.location.href
};
await utils.setSavedDataObj(key, data);
this.lastSavedTime = currentTime;
} catch (error) {
console.error('Error saving time:', error);
}
}
updateName() {
const newName = this.getStreamName();
if (newName !== this.streamInfo.title) {
this.streamInfo.title = newName;
}
}
startPeriodicSave() {
this.stopPeriodicSave();
this.saveInterval = setInterval(() => {
if (this.video && !this.video.paused) {
this.saveCurrentTime();
}
}, this.saveFrequency);
}
stopPeriodicSave() {
if (this.saveInterval) {
clearInterval(this.saveInterval);
this.saveInterval = null;
}
}
initializeVariables() {
this.video = null;
this.streamInfo = {
id: null,
title: 'No title',
channelName: 'No name'
}
this.saveInterval = null;
this.isInitialized = false;
this.isLoaded = false;
this.lastSavedTime = 0;
this.saveFrequency = 5000;
}
cleanup() {
this.stopPeriodicSave();
this.initializeVariables();
}
observeUrlChanges() {
let currentUrl = window.location.href;
const observer = new MutationObserver(() => {
if (window.location.href !== currentUrl) {
currentUrl = window.location.href;
// URL changed, reinitialize
this.cleanup();
setTimeout(() => this.waitForVideo(), 1000);
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
}
const kickVideoTimeSaver = new KickVideoTimeSaver();