-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-duration-worker.js
More file actions
212 lines (177 loc) · 6.03 KB
/
content-duration-worker.js
File metadata and controls
212 lines (177 loc) · 6.03 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
207
208
209
210
211
212
(() => {
if (window.__jumpKeyDurationWorkerInstalled) {
return;
}
window.__jumpKeyDurationWorkerInstalled = true;
const WORKER_TITLE = 'JumpKey Queue Sync';
const WORKER_SUBTITLE = 'Syncing library records in the background';
function applyWorkerPresentation() {
try {
document.title = WORKER_TITLE;
let metaDesc = document.querySelector('meta[name="description"]');
if (!metaDesc) {
metaDesc = document.createElement('meta');
metaDesc.setAttribute('name', 'description');
document.head.appendChild(metaDesc);
}
metaDesc.setAttribute('content', WORKER_SUBTITLE);
let favicon = document.querySelector('link[rel="icon"]');
if (!favicon) {
favicon = document.createElement('link');
favicon.setAttribute('rel', 'icon');
document.head.appendChild(favicon);
}
const svg = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'><rect width='64' height='64' rx='14' fill='%231f2937'/><path d='M18 22h28v6H18zm0 10h20v6H18zm0 10h28v6H18z' fill='%23f3f4f6'/></svg>";
favicon.setAttribute('href', `data:image/svg+xml,${encodeURIComponent(svg)}`);
} catch (err) {
console.warn('[JumpKey Worker] Failed to apply worker presentation:', err);
}
}
applyWorkerPresentation();
let ytApiPromise = null;
let isProcessing = false;
function loadYouTubeIframeAPI() {
if (window.YT && window.YT.Player) {
return Promise.resolve();
}
if (ytApiPromise) {
return ytApiPromise;
}
ytApiPromise = new Promise((resolve, reject) => {
const existingScript = document.querySelector('script[src="https://www.youtube.com/iframe_api"]');
if (!existingScript) {
const script = document.createElement('script');
script.src = 'https://www.youtube.com/iframe_api';
script.async = true;
script.onerror = () => reject(new Error('Failed to load YouTube IFrame API'));
document.head.appendChild(script);
}
const timeout = setTimeout(() => {
reject(new Error('Timeout waiting YouTube IFrame API'));
}, 10000);
const previousReady = window.onYouTubeIframeAPIReady;
window.onYouTubeIframeAPIReady = () => {
clearTimeout(timeout);
if (typeof previousReady === 'function') {
try {
previousReady();
} catch (_) {}
}
resolve();
};
if (window.YT && window.YT.Player) {
clearTimeout(timeout);
resolve();
}
});
return ytApiPromise;
}
async function getYouTubeDurationLight(videoId) {
if (!videoId) return null;
await loadYouTubeIframeAPI();
return new Promise((resolve) => {
const container = document.createElement('div');
container.style.cssText = 'position:fixed; left:-10000px; top:-10000px; width:1px; height:1px; overflow:hidden; opacity:0; pointer-events:none;';
document.body.appendChild(container);
let finished = false;
let player = null;
const finish = (duration) => {
if (finished) return;
finished = true;
try {
if (player && typeof player.destroy === 'function') {
player.destroy();
}
} catch (_) {}
try {
if (container && container.parentNode) {
container.parentNode.removeChild(container);
}
} catch (_) {}
resolve(Number.isFinite(Number(duration)) && Number(duration) > 0 ? Number(duration) : null);
};
player = new YT.Player(container, {
width: 1,
height: 1,
videoId,
playerVars: {
autoplay: 0,
controls: 0,
disablekb: 1,
fs: 0,
modestbranding: 1,
playsinline: 1,
rel: 0
},
events: {
onReady: async (event) => {
const direct = Number(event?.target?.getDuration?.() || 0);
if (direct > 0) {
finish(direct);
return;
}
setTimeout(() => {
const delayed = Number(event?.target?.getDuration?.() || 0);
finish(delayed > 0 ? delayed : null);
}, 700);
},
onError: () => finish(null)
}
});
setTimeout(() => finish(null), 7000);
});
}
async function processVideoIds(videoIds) {
const ids = Array.isArray(videoIds)
? videoIds.map((id) => String(id || '').trim()).filter(Boolean)
: [];
const results = {};
for (const videoId of ids) {
const duration = await getYouTubeDurationLight(videoId);
results[videoId] = duration;
if (Number.isFinite(Number(duration)) && Number(duration) > 0) {
chrome.runtime.sendMessage({
action: 'reportVideoDuration',
videoId,
duration: Number(duration),
source: 'duration-worker'
}, () => {
if (chrome.runtime.lastError) {
console.warn('[JumpKey Worker] reportVideoDuration error:', chrome.runtime.lastError.message);
}
});
}
await new Promise((res) => setTimeout(res, 120));
}
return results;
}
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (!message || !message.action) {
return false;
}
if (message.action === 'durationWorkerPing') {
sendResponse({ ok: true, title: document.title, busy: isProcessing });
return false;
}
if (message.action === 'durationWorkerProcess') {
if (isProcessing) {
sendResponse({ ok: false, error: 'busy' });
return false;
}
isProcessing = true;
(async () => {
try {
const results = await processVideoIds(message.videoIds || []);
sendResponse({ ok: true, results });
} catch (err) {
sendResponse({ ok: false, error: err?.message || String(err) });
} finally {
isProcessing = false;
}
})();
return true;
}
return false;
});
console.log('[JumpKey Worker] Duration worker ready:', WORKER_TITLE);
})();