update
This commit is contained in:
@@ -18,51 +18,29 @@ function extractVideoFromCard(element) {
|
||||
return {
|
||||
title,
|
||||
youtuber,
|
||||
thumbnail_url: thumbnail || `https://img.youtube.com/vi/${match[1]}/hqdefault.jpg`,
|
||||
youtube_url: `https://www.youtube.com/watch?v=${match[1]}`,
|
||||
thumbnailUrl: thumbnail || `https://img.youtube.com/vi/${match[1]}/hqdefault.jpg`,
|
||||
youtubeUrl: `https://www.youtube.com/watch?v=${match[1]}`,
|
||||
};
|
||||
}
|
||||
|
||||
function collectVideos(elements) {
|
||||
const videos = [];
|
||||
for (const el of elements) {
|
||||
const video = extractVideoFromCard(el);
|
||||
if (!video) continue;
|
||||
if (sentUrls.has(video.youtube_url)) continue;
|
||||
sentUrls.add(video.youtube_url);
|
||||
videos.push(video);
|
||||
}
|
||||
return videos;
|
||||
}
|
||||
|
||||
// --- Debounced Batch-Versand ---
|
||||
|
||||
let pendingVideos = [];
|
||||
let sendTimer = null;
|
||||
|
||||
async function queueVideos(videos) {
|
||||
pendingVideos.push(...videos);
|
||||
if (!sendTimer) {
|
||||
sendTimer = setTimeout(async () => {
|
||||
if (pendingVideos.length > 0) {
|
||||
const stored = await browser.storage.local.get("profileId");
|
||||
const profileId = stored.profileId || null;
|
||||
const batch = pendingVideos.map((v) => ({ ...v, profile_id: profileId }));
|
||||
console.log(`[YT-Erfasser] ${batch.length} Videos senden (Profil: ${profileId})`);
|
||||
browser.runtime.sendMessage(batch);
|
||||
}
|
||||
pendingVideos = [];
|
||||
sendTimer = null;
|
||||
}, 250);
|
||||
}
|
||||
async function sendVideo(video) {
|
||||
const stored = await browser.storage.local.get("profileId");
|
||||
const profileId = stored.profileId || null;
|
||||
const payload = { ...video, profileId };
|
||||
console.log(`[YT-Erfasser] Video senden (Profil: ${profileId})`, payload.title);
|
||||
browser.runtime.sendMessage(payload);
|
||||
}
|
||||
|
||||
// --- IntersectionObserver: nur sichtbare Cards erfassen ---
|
||||
|
||||
const visibilityObserver = new IntersectionObserver((entries) => {
|
||||
const cards = entries.filter((e) => e.isIntersecting).map((e) => e.target);
|
||||
if (cards.length > 0) {
|
||||
queueVideos(collectVideos(cards));
|
||||
for (const entry of entries) {
|
||||
if (!entry.isIntersecting) continue;
|
||||
const video = extractVideoFromCard(entry.target);
|
||||
if (!video) continue;
|
||||
if (sentUrls.has(video.youtubeUrl)) continue;
|
||||
sentUrls.add(video.youtubeUrl);
|
||||
sendVideo(video);
|
||||
}
|
||||
}, { threshold: 0.5 });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user