/* eslint-disable @next/next/no-img-element */ import { Draggable } from '@/components/Draggable' import { siteConfig } from '@/lib/config' import { deepClone } from '@/lib/utils' import Link from 'next/link' import { useEffect, useState } from 'react' import DownloadButton from './DownloadButton' import FullScreenButton from './FullScreenButton' /** * 嵌入游戏 * @param {*} param0 * @returns */ export default function GameEmbed({ post, siteInfo }) { const game = deepClone(post) const newWindow = game?.ext?.new_window || false const originUrl = game?.ext?.href // 提示用户在新窗口打开 const [tipNewWindow, setTipNewWindow] = useState(newWindow) const [loading, setLoading] = useState(true) /** * 新窗口中打开游戏。 * 并且在回到此页面后后刷新iframe,尝试重新加载iframe */ const openInNewWindow = () => { // 关闭提示框 setTipNewWindow(false) // 添加监听器 document.addEventListener('visibilitychange', handleVisibilityChange) // 定义监听器函数 function handleVisibilityChange() { if (document.visibilityState === 'hidden') { // console.log("用户切换到了其他标签页"); } else { // console.log("用户回到了当前页面"); setLoading(true) // 刷新网页 reloadIframe() // 移除监听器 document.removeEventListener('visibilitychange', handleVisibilityChange) } } } /** * 隐藏提示框 */ const hiddenTips = () => { setTipNewWindow(false) } function reloadIframe() { var iframe = document.getElementById('game-wrapper') iframe.contentWindow.location.reload() } // 定义一个函数来处理iframe加载成功事件 function iframeLoaded() { if (game) { setLoading(false) } } useEffect(() => { // 是否弹窗提示新网页打开 setTipNewWindow(newWindow) const iframe = document.getElementById('game-wrapper') // 绑定加载事件 if (iframe?.attachEvent) { iframe?.attachEvent('onload', iframeLoaded) } else if (iframe) { iframe.onload = iframeLoaded } // 更改iFrame的title if ( document ?.getElementById('game-wrapper') ?.contentDocument.querySelector('title')?.textContent ) { document .getElementById('game-wrapper') .contentDocument.querySelector('title').textContent = `${ game?.title || '' } - Play ${game?.title || ''} on ${siteConfig('TITLE')}` } }, [post]) return (