Files
playX/public/test-host.html

337 lines
8.9 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PlayX Iframe Host Test</title>
<style>
:root {
color-scheme: light;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #f4f6fb;
color: #1f2937;
}
.layout {
display: grid;
grid-template-columns: 360px minmax(0, 1fr);
height: 100vh;
}
.panel {
padding: 24px;
background: #ffffff;
border-right: 1px solid #e5e7eb;
overflow: auto;
}
.preview {
width: 100%;
height: 800px;
overflow: auto;
}
.preview-frame {
width: 800px;
height: 300px;
overflow: hidden;
border: 1px solid #d1d5db;
border-radius: 16px;
background: #ffffff;
}
h1 {
margin: 0 0 8px;
font-size: 20px;
}
p {
margin: 0 0 20px;
line-height: 1.6;
color: #4b5563;
}
label {
display: block;
margin-bottom: 14px;
font-size: 14px;
font-weight: 600;
}
input {
width: 100%;
margin-top: 8px;
padding: 10px 12px;
border: 1px solid #cbd5e1;
border-radius: 10px;
font-size: 14px;
}
.actions {
display: flex;
gap: 12px;
margin-top: 18px;
}
button {
appearance: none;
border: 0;
border-radius: 10px;
padding: 10px 14px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
}
.primary {
background: #111827;
color: #ffffff;
}
.secondary {
background: #e5e7eb;
color: #111827;
}
.log {
margin-top: 20px;
padding: 12px;
border-radius: 12px;
background: #0f172a;
color: #dbeafe;
font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
white-space: pre-wrap;
min-height: 160px;
max-height: min(360px, calc(100vh - 260px));
overflow: auto;
}
iframe {
display: block;
width: 100%;
height: 100%;
border: 0;
}
@media (max-width: 960px) {
.layout {
grid-template-columns: 1fr;
}
.panel {
border-right: 0;
border-bottom: 1px solid #e5e7eb;
}
.preview-frame {
width: 100%;
height: 70vh;
}
}
</style>
</head>
<body>
<div class="layout">
<aside class="panel">
<h1>PlayX Host Test</h1>
<p>Use this page to test iframe embedding and the <code>IFRAME_READY</code> / <code>IFRAME_CONTEXT</code> handshake flow.</p>
<label>
Username
<input id="username" value="+60333333333" />
</label>
<label>
Token
<input id="token" value="" readonly />
</label>
<label>
Language
<input id="language" value="zh-my" />
</label>
<div class="actions">
<button id="send" class="primary" type="button">Fetch Token &amp; Send Context</button>
<button id="reload" class="secondary" type="button">Reload Iframe</button>
</div>
<div id="log" class="log"></div>
</aside>
<main class="preview">
<iframe id="playx-frame" title="PlayX App" src="/"></iframe>
</main>
</div>
<script>
const frame = document.getElementById('playx-frame')
const usernameInput = document.getElementById('username')
const tokenInput = document.getElementById('token')
const languageInput = document.getElementById('language')
const sendButton = document.getElementById('send')
const reloadButton = document.getElementById('reload')
const logNode = document.getElementById('log')
const iframeOrigin = window.location.origin
const apiBaseUrl = ['127.0.0.1', 'localhost'].includes(window.location.hostname)
? `${window.location.origin}/api`
: 'https://playx-api.cjdhr.top/api'
const maxLogEntries = 80
const logEntries = []
const heightLogDelay = 300
let pendingHeightLog = null
let heightLogTimer = null
let currentTokenUsername = ''
let autoContextSent = false
let sendContextInFlight = false
function appendLog(message, data) {
const stamp = new Date().toLocaleTimeString()
const payload = data ? ` ${JSON.stringify(data, null, 2)}` : ''
logEntries.unshift(`[${stamp}] ${message}${payload}`)
logEntries.length = Math.min(logEntries.length, maxLogEntries)
logNode.textContent = logEntries.join('\n')
logNode.scrollTop = 0
}
function scheduleHeightLog(message) {
pendingHeightLog = message
if (heightLogTimer != null) {
window.clearTimeout(heightLogTimer)
}
heightLogTimer = window.setTimeout(() => {
appendLog('Received IFRAME_HEIGHT from iframe', pendingHeightLog)
pendingHeightLog = null
heightLogTimer = null
}, heightLogDelay)
}
function buildPayload() {
return {
type: 'IFRAME_CONTEXT',
payload: {
token: tokenInput.value.trim(),
language: languageInput.value.trim(),
},
}
}
async function ensureToken() {
const username = usernameInput.value.trim()
if (!username) {
throw new Error('Username is required.')
}
const existingToken = tokenInput.value.trim()
if (existingToken && currentTokenUsername === username) {
return existingToken
}
const response = await fetch(`${apiBaseUrl}/v1/temLogin?username=${encodeURIComponent(username)}`, {
method: 'GET',
})
if (!response.ok) {
throw new Error(`temLogin request failed: ${response.status}`)
}
const payload = await response.json()
const token = payload?.data?.userInfo?.token?.trim()
console.log('[test-host] temLogin response:', payload)
if (!token) {
throw new Error(`temLogin response missing token: ${JSON.stringify(payload)}`)
}
tokenInput.value = token
currentTokenUsername = username
appendLog('Fetched token from temLogin', {
username,
tokenPreview: `${token.slice(0, 12)}...`,
})
return token
}
async function sendContext() {
if (sendContextInFlight) {
return
}
sendContextInFlight = true
sendButton.disabled = true
try {
await ensureToken()
} catch (error) {
appendLog('Failed to fetch token', {
message: error instanceof Error ? error.message : String(error),
})
sendButton.disabled = false
sendContextInFlight = false
return
}
const message = buildPayload()
console.log('[test-host] postMessage payload:', message)
frame.contentWindow?.postMessage(message, iframeOrigin)
appendLog('Sent IFRAME_CONTEXT to iframe', {
language: message.payload.language,
tokenPreview: `${message.payload.token.slice(0, 12)}...`,
})
sendButton.disabled = false
sendContextInFlight = false
}
frame.addEventListener('load', () => {
autoContextSent = false
})
sendButton.addEventListener('click', async () => {
await sendContext()
})
reloadButton.addEventListener('click', () => {
autoContextSent = false
frame.contentWindow?.location.reload()
})
window.addEventListener('message', (event) => {
if (event.source === frame.contentWindow && event.data?.type === 'IFRAME_READY') {
appendLog('Received IFRAME_READY from iframe')
if (autoContextSent) {
return
}
autoContextSent = true
void sendContext()
return
}
if (event.source === frame.contentWindow && event.data?.type === 'IFRAME_HEIGHT') {
const height = Number(event.data?.payload?.height)
if (!Number.isFinite(height) || height <= 0) {
return
}
scheduleHeightLog(event.data)
return
}
})
</script>
</body>
</html>