Skip to content

Commit

Permalink
Make sure the ad keeps playing
Browse files Browse the repository at this point in the history
  • Loading branch information
pbartkowiak-fandom committed Aug 8, 2023
1 parent f3d2db8 commit bfed75c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/jwplayer/players/shared/JwPlayerWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import JWEvents from 'jwplayer/players/shared/JWEvents';
import addBaseTrackingEvents from 'jwplayer/players/shared/addBaseTrackingEvents';
import slugify from 'jwplayer/utils/slugify';
import getSponsoredVideos from 'utils/getSponsoredVideos';
import { isInViewportCheck } from 'jwplayer/utils/utils';

interface WindowJWPlayer extends Window {
jwplayer?: JWPlayerApi;
Expand Down Expand Up @@ -95,9 +96,13 @@ const JwPlayerWrapper: React.FC<JwPlayerWrapperProps> = ({
...config,
});

playerInstance.on(JWEvents.AD_PAUSE, ({ pauseReason, viewable }: JWPauseEvent) => {
playerInstance.on(JWEvents.AD_PAUSE, ({ pauseReason }: JWPauseEvent) => {
const playerElement = document.getElementById('featured-video__player');
// The 'viewable' property from the JWPauseEvent is not reliable
const isInViewport = isInViewportCheck(playerElement);

// Keep playing the ad when the user closed the mini player
if (viewable === 0 && pauseReason === 'external') {
if (isInViewport === false && pauseReason === 'external') {
playerInstance.play();
}
});
Expand Down
13 changes: 13 additions & 0 deletions src/jwplayer/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ export const getDismissedFn = (inputName: string) => () => {
}
return false;
};

export const isInViewportCheck = (element: HTMLElement) => {
if (element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
return false;
};

0 comments on commit bfed75c

Please sign in to comment.