Skip to content

Commit

Permalink
fix functions to only postMessage if iframe is available
Browse files Browse the repository at this point in the history
  • Loading branch information
Julia Wegmayr committed Nov 27, 2024
1 parent 438c146 commit 9688201
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/site/cms-site/src/blocks/helpers/controlVideos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@ export const pauseDamVideo = (videoRef: RefObject<HTMLVideoElement>) => {
};

export const pauseYoutubeVideo = () => {
const iframe = document.getElementsByTagName("iframe")[0].contentWindow;
iframe?.postMessage(`{"event":"command","func":"pauseVideo","args":""}`, "*");
const iframe = document.getElementsByTagName("iframe")[0];
if (iframe?.contentWindow) {
iframe.contentWindow.postMessage(`{"event":"command","func":"pauseVideo","args":""}`, "*");
}
};

export const playYoutubeVideo = () => {
const iframe = document.getElementsByTagName("iframe")[0].contentWindow;
iframe?.postMessage(`{"event":"command","func":"playVideo","args":""}`, "*");
const iframe = document.getElementsByTagName("iframe")[0];
if (iframe?.contentWindow) {
iframe.contentWindow.postMessage(`{"event":"command","func":"playVideo","args":""}`, "*");
}
};

export const pauseVimeoVideo = () => {
const iframe = document.getElementsByTagName("iframe")[0].contentWindow;
iframe?.postMessage(JSON.stringify({ method: "pause" }), "https://player.vimeo.com");
const iframe = document.getElementsByTagName("iframe")[0];
if (iframe?.contentWindow) {
iframe.contentWindow.postMessage(JSON.stringify({ method: "pause" }), "https://player.vimeo.com");
}
};

export const playVimeoVideo = () => {
const iframe = document.getElementsByTagName("iframe")[0].contentWindow;
iframe?.postMessage(JSON.stringify({ method: "play" }), "https://player.vimeo.com");
const iframe = document.getElementsByTagName("iframe")[0];
if (iframe?.contentWindow) {
iframe.contentWindow.postMessage(JSON.stringify({ method: "play" }), "https://player.vimeo.com");
}
};

0 comments on commit 9688201

Please sign in to comment.