Files
playX/public/test-host.html

255 lines
6.4 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>PLAYX_READY</code> / <code>IFRAME_CONTEXT</code> handshake flow.</p>
<label>
Username
<input id="username" value="+60777777777" />
</label>
<label>
Language
<input id="language" value="zh-CN" />
</label>
<div class="actions">
<button id="send" class="primary" type="button">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 previewFrame = document.getElementById('preview-frame')
const usernameInput = document.getElementById('username')
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 maxLogEntries = 80
const logEntries = []
const heightLogDelay = 300
let pendingHeightLog = null
let heightLogTimer = null
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 PLAYX_HEIGHT from iframe', pendingHeightLog)
pendingHeightLog = null
heightLogTimer = null
}, heightLogDelay)
}
function buildPayload() {
return {
type: 'IFRAME_CONTEXT',
payload: {
username: usernameInput.value.trim(),
language: languageInput.value.trim(),
},
}
}
function sendContext() {
const message = buildPayload()
frame.contentWindow?.postMessage(message, iframeOrigin)
}
frame.addEventListener('load', () => {
})
sendButton.addEventListener('click', () => {
sendContext()
})
reloadButton.addEventListener('click', () => {
frame.contentWindow?.location.reload()
})
window.addEventListener('message', (event) => {
if (event.source === frame.contentWindow && event.data?.type === 'PLAYX_READY') {
sendContext()
return
}
if (event.source === frame.contentWindow && event.data?.type === 'PLAYX_HEIGHT') {
const height = Number(event.data?.payload?.height)
if (!Number.isFinite(height) || height <= 0) {
return
}
scheduleHeightLog(event.data)
return
}
})
</script>
</body>
</html>