diff --git a/src/components/player/atoms/XPrimeAdOverlay.tsx b/src/components/player/atoms/XPrimeAdOverlay.tsx index f364a20f..a044e32b 100644 --- a/src/components/player/atoms/XPrimeAdOverlay.tsx +++ b/src/components/player/atoms/XPrimeAdOverlay.tsx @@ -7,13 +7,18 @@ import { usePlayerStore } from "@/stores/player/store"; import { usePreferencesStore } from "@/stores/preferences"; export function XPrimeAdOverlay() { - const sourceId = usePlayerStore((s) => s.sourceId); - const status = usePlayerStore((s) => s.status); + const { sourceId, status } = usePlayerStore((s) => ({ + sourceId: s.sourceId, + status: s.status, + })); const disableXPrimeAds = usePreferencesStore((s) => s.disableXPrimeAds); const [show, setShow] = useState(false); useEffect(() => { - if (sourceId === "xprime" && status === "playing" && !disableXPrimeAds) { + // Only show overlay when all conditions are met + const shouldShow = + sourceId === "xprimetv" && status === "playing" && !disableXPrimeAds; + if (shouldShow && !show) { setShow(true); const timer = setTimeout(() => { setShow(false); @@ -21,8 +26,10 @@ export function XPrimeAdOverlay() { return () => clearTimeout(timer); } - setShow(false); - }, [sourceId, status, disableXPrimeAds]); + if (!shouldShow && show) { + setShow(false); + } + }, [sourceId, status, disableXPrimeAds, show]); if (!show) { return null; diff --git a/src/components/player/display/base.ts b/src/components/player/display/base.ts index 3b40fc5b..b10fe44c 100644 --- a/src/components/player/display/base.ts +++ b/src/components/player/display/base.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ import fscreen from "fscreen"; import Hls, { Level } from "hls.js"; @@ -105,6 +106,27 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { (value: void | PromiseLike) => void >(); + function injectXPrimeAd(sourceId?: string | null) { + const currentSourceId = sourceId ?? usePlayerStore.getState().sourceId; + console.log("currentSourceId", currentSourceId); + const disableXPrimeAds = usePreferencesStore.getState().disableXPrimeAds; + if ( + currentSourceId === "xprimetv" && + !disableXPrimeAds && + !document.querySelector( + 'script[data-cfasync="false"][src*="jg.prisagedibbuk.com"]', + ) + ) { + console.log("injecting XPrime ad"); + const script = document.createElement("script"); + script.setAttribute("data-cfasync", "false"); + script.async = true; + script.type = "text/javascript"; + script.src = "//jg.prisagedibbuk.com/r47OViiCQMeGnyQ/131974"; + document.head.appendChild(script); + } + } + function reportLevels() { if (!hls) return; const levels = hls.levels; @@ -356,6 +378,7 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { videoElement.addEventListener("play", () => { emit("play", undefined); emit("loading", false); + injectXPrimeAd(); }); videoElement.addEventListener("error", () => { const err = videoElement?.error ?? null; @@ -383,24 +406,6 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { }); } } - - // Inject popup ad for xprime sources - const sourceId = usePlayerStore.getState().sourceId; - const disableXPrimeAds = usePreferencesStore.getState().disableXPrimeAds; - if ( - sourceId === "xprime" && - !disableXPrimeAds && - !document.querySelector( - 'script[data-cfasync="false"][src*="jg.prisagedibbuk.com"]', - ) - ) { - const script = document.createElement("script"); - script.setAttribute("data-cfasync", "false"); - script.async = true; - script.type = "text/javascript"; - script.src = "//jg.prisagedibbuk.com/r47OViiCQMeGnyQ/131974"; - document.head.appendChild(script); - } }); videoElement.addEventListener("waiting", () => emit("loading", true)); videoElement.addEventListener("volumechange", () => @@ -578,6 +583,10 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { // Set autoplay flag if starting from beginning (indicates autoplay transition) shouldAutoplayAfterLoad = ops.startAt === 0; setSource(); + // Inject ads after source change if conditions are met + if (ops.source) { + injectXPrimeAd(); + } }, changeQuality(newAutomaticQuality, newPreferredQuality) { if (source?.type !== "hls") return;