diff --git a/themes/game/components/GameEmbed.js b/themes/game/components/GameEmbed.js new file mode 100644 index 00000000..6f69d986 --- /dev/null +++ b/themes/game/components/GameEmbed.js @@ -0,0 +1,167 @@ +/* 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 [url, setUrl] = useState(originUrl) + const [loading, setLoading] = useState(true) + + /** + * 新窗口中打开 + */ + const openInNewWindow = () => { + setTipNewWindow(false) + setTimeout(function () { + setUrl('') + setUrl(originUrl) + }, 10000) // 10000毫秒 = 10秒 + } + + // 将当前游戏加入到最近游玩 + useEffect(() => { + setUrl(originUrl) + + const iframe = document.getElementById('game-wrapper') + + // 定义一个函数来处理iframe加载成功事件 + function iframeLoaded() { + if (game) { + setLoading(false) + } + } + + // 绑定加载事件 + 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')}` + } + }, [game]) + + return ( +