2026-04-23 16:23:40 +08:00

42 lines
1.3 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>跨端测试</title>
</head>
<body>
<h1>WebView2 自定义协议演示</h1>
<button id="getUserBtn">获取用户信息</button>
<button id="processBtn">处理数据</button>
<pre id="output"></pre>
<script src="./api.js"></script>
<script>
const output = document.getElementById('output');
document.getElementById('getUserBtn').onclick = async () => {
try {
const result = await window.api.getUser();
output.textContent = JSON.stringify(result, null, 2);
} catch (err) {
output.textContent = `错误: ${err.message}`;
}
};
document.getElementById('processBtn').onclick = async () => {
try {
const result = await window.api.processData('hello world');
output.textContent = JSON.stringify(result, null, 2);
} catch (err) {
output.textContent = `错误: ${err.message}`;
}
};
const isWV2 = window.isWebView2 === true;
setTimeout(() => {
document.body.insertAdjacentHTML('beforeend', `<p>当前环境: ${isWV2 ? 'WebView2 (自定义协议)' : '普通浏览器 (HTTP API)'}</p>`);
}, 100)
</script>
</body>
</html>