Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 3, 2024
1 parent 341d30f commit a87c530
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,16 +574,17 @@ async function startSponsorSchedule(
return;
}

logDebug(`Considering to start skipping: ${!getVideo()}, ${getVideo()?.paused}`);
if (!getVideo()) return;
const video = getVideo();
logDebug(`Considering to start skipping: ${!video}, ${video?.paused}`);
if (!video) return;
if (currentTime === undefined || currentTime === null) {
currentTime = getVirtualTime();
}
clearWaitingTime();

updateActiveSegment(currentTime);

if (getVideo().paused || (getVideo().currentTime >= getVideo().duration - 0.01 && getVideo().duration > 1)) return;
if (video.paused || (video.currentTime >= video.duration - 0.01 && video.duration > 1)) return;
const skipInfo = getNextSkipIndex(currentTime, includeIntersectingSegments, includeNonIntersectingSegments);

const currentSkip = skipInfo.array[skipInfo.index];
Expand All @@ -598,7 +599,7 @@ async function startSponsorSchedule(
skipInfo.index !== -1 && timeUntilSponsor < skipBuffer && shouldAutoSkip(currentSkip)
)
) {
getVideo().muted = false;
video.muted = false;
videoMuted = false;

for (const notice of skipNotices) {
Expand Down Expand Up @@ -712,7 +713,7 @@ async function startSponsorSchedule(
if (timeUntilSponsor < skipBuffer) {
await skippingFunction(currentTime);
} else {
let delayTime = timeUntilSponsor * 1000 * (1 / getVideo().playbackRate);
let delayTime = (timeUntilSponsor * 1000) / getVideo().playbackRate;
if (delayTime < (isFirefox() ? 750 : 300)) {
let forceStartIntervalTime: number | null = null;
if (isFirefox() && delayTime > 300) {
Expand Down Expand Up @@ -764,13 +765,13 @@ async function startSponsorSchedule(
logDebug(`Starting timeout to skip ${getVideo().currentTime} to skip at ${skipTime[0]}`);

// 提前触发跳过通知
let advance: number = 0;
if (Config.config.advanceSkipNotice && Config.config.skipNoticeDurationBefore > 0) {
advance = Config.config.skipNoticeDurationBefore * 1000;
delayTime -= Config.config.skipNoticeDurationBefore * 1000;
}

const offset = isFirefox() ? 600 : 150;
// Schedule for right before to be more precise than normal timeout
currentSkipSchedule = setTimeout(skippingFunction, Math.max(0, delayTime - offset - advance));
currentSkipSchedule = setTimeout(skippingFunction, Math.max(0, delayTime - offset));
}
}
}
Expand Down Expand Up @@ -837,12 +838,11 @@ function checkDanmaku(text: string, offset: number) {
}, offset * 1000 - 100);
}

let observer: MutationObserver = null;
let danmakuObserver: MutationObserver = null;
const processedDanmaku = new Set<string>();

function danmakuForSkip() {
if (!Config.config.enableDanmakuSkip || Config.config.disableSkipping) return;
if (observer) return;
if (danmakuObserver) return;

const targetNode = document.querySelector(".bpx-player-row-dm-wrap"); // 选择父节点
const config = { attributes: true, subtree: true }; // 观察属性变化
Expand All @@ -868,12 +868,12 @@ function danmakuForSkip() {
}
}
};
observer = new MutationObserver(callback);
observer.observe(targetNode, config);
danmakuObserver = new MutationObserver(callback);
danmakuObserver.observe(targetNode, config);
addCleanupListener(() => {
if (observer) {
observer.disconnect();
observer = null;
if (danmakuObserver) {
danmakuObserver.disconnect();
danmakuObserver = null;
processedDanmaku.clear();
return;
}
Expand Down

0 comments on commit a87c530

Please sign in to comment.