修复白顺

This commit is contained in:
hy001 2026-04-16 12:11:21 +08:00
parent 80e9f3a7d0
commit 47f8f10fa5
3 changed files with 117 additions and 18 deletions

View File

@ -88,6 +88,8 @@
logEl.textContent = line + logEl.textContent;
}
let hasLoadedGame = false;
function requestConfig() {
if (!window.NativeBridge || typeof window.NativeBridge.getConfig !== "function") {
writeLog("getConfig", { error: "NativeBridge not ready" });
@ -95,10 +97,14 @@
}
window.NativeBridge.getConfig(function (config) {
writeLog("getConfig callback", config);
if (!hasLoadedGame) {
setTimeout(notifyLoaded, 300);
}
});
}
function notifyLoaded() {
hasLoadedGame = true;
window.NativeBridge &&
window.NativeBridge.gameLoaded &&
window.NativeBridge.gameLoaded({ status: "loaded" });
@ -126,6 +132,23 @@
window.addEventListener("walletUpdate", function (event) {
writeLog("walletUpdate event", event.detail || {});
});
window.addEventListener("baishunBridgeReady", function () {
writeLog("baishunBridgeReady", { status: "received" });
requestConfig();
});
window.addEventListener("baishunConfig", function (event) {
writeLog("baishunConfig event", event.detail || {});
});
window.addEventListener("load", function () {
setTimeout(function () {
if (!hasLoadedGame) {
requestConfig();
}
}, 150);
});
</script>
</body>
</html>

View File

@ -76,6 +76,12 @@ class BaishunJsBridge {
window.dispatchEvent(new CustomEvent('walletUpdate', { detail: payload || {} }));
}
};
if (typeof window.onBaishunBridgeReady === 'function') {
window.onBaishunBridgeReady(window.NativeBridge);
}
if (typeof window.dispatchEvent === 'function') {
window.dispatchEvent(new CustomEvent('baishunBridgeReady', { detail: { nativeBridge: true } }));
}
})();
''';
}
@ -88,11 +94,13 @@ class BaishunJsBridge {
window.__baishunLastConfig = config;
if (typeof window.__baishunGetConfigCallback === 'function') {
window.__baishunGetConfigCallback(config);
return;
}
if (typeof window.onBaishunConfig === 'function') {
window.onBaishunConfig(config);
}
if (typeof window.dispatchEvent === 'function') {
window.dispatchEvent(new CustomEvent('baishunConfig', { detail: config }));
}
})();
''';
}
@ -100,9 +108,7 @@ class BaishunJsBridge {
static String buildWalletUpdateScript({Map<String, dynamic>? payload}) {
final safePayload = jsonEncode(
payload ??
<String, dynamic>{
'timestamp': DateTime.now().millisecondsSinceEpoch,
},
<String, dynamic>{'timestamp': DateTime.now().millisecondsSinceEpoch},
);
return '''
(function() {
@ -113,4 +119,26 @@ class BaishunJsBridge {
})();
''';
}
static String injectBootstrapHtml({
required String html,
required Uri entryUri,
required BaishunBridgeConfigModel config,
}) {
final baseHref = htmlEscape.convert(entryUri.toString());
final injection = '''
<base href="$baseHref" />
<script>
${bootstrapScript()}
${buildConfigScript(config)}
</script>
''';
final headPattern = RegExp(r'<head[^>]*>', caseSensitive: false);
final headMatch = headPattern.firstMatch(html);
if (headMatch != null) {
return html.replaceRange(headMatch.end, headMatch.end, injection);
}
return '$injection$html';
}
}

View File

@ -1,4 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
@ -49,7 +52,7 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (String _) async {
await _injectBridge();
await _primeBridgeHandshake();
},
onWebResourceError: (WebResourceError error) {
if (!mounted) {
@ -83,7 +86,13 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
if (uri == null) {
throw Exception('Invalid game entry url: $entryUrl');
}
await _controller.loadRequest(uri);
final html = await _fetchRemoteEntryHtml(uri);
final injectedHtml = BaishunJsBridge.injectBootstrapHtml(
html: html,
entryUri: uri,
config: widget.launchModel.bridgeConfig,
);
await _controller.loadHtmlString(injectedHtml, baseUrl: uri.toString());
} catch (error) {
if (!mounted) {
return;
@ -95,6 +104,24 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
}
}
Future<String> _fetchRemoteEntryHtml(Uri uri) async {
final client = HttpClient();
client.userAgent = 'YumiBaishunBridge/1.0';
try {
final request = await client.getUrl(uri);
final response = await request.close();
if (response.statusCode < 200 || response.statusCode >= 300) {
throw HttpException(
'Failed to load remote entry html (${response.statusCode})',
uri: uri,
);
}
return response.transform(utf8.decoder).join();
} finally {
client.close(force: true);
}
}
Future<void> _injectBridge() async {
try {
await _controller.runJavaScript(BaishunJsBridge.bootstrapScript());
@ -104,6 +131,21 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
} catch (_) {}
}
Future<void> _primeBridgeHandshake() async {
await _injectBridge();
for (final delay in <Duration>[
const Duration(milliseconds: 400),
const Duration(milliseconds: 1200),
]) {
Future<void>.delayed(delay, () async {
if (!mounted || !_isLoading || _errorMessage != null) {
return;
}
await _injectBridge();
});
}
}
Future<void> _handleBridgeMessage(JavaScriptMessage message) async {
final bridgeMessage = BaishunBridgeMessage.parse(message.message);
switch (bridgeMessage.action) {
@ -135,7 +177,9 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
Future<void> _notifyWalletUpdate() async {
try {
await _controller.runJavaScript(BaishunJsBridge.buildWalletUpdateScript());
await _controller.runJavaScript(
BaishunJsBridge.buildWalletUpdateScript(),
);
} catch (_) {}
}
@ -206,7 +250,10 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
children: [
IconButton(
onPressed: _closeAndExit,
icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
icon: const Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
),
Expanded(
child: Text(
@ -229,10 +276,17 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
Expanded(
child: Stack(
children: [
Positioned.fill(child: WebViewWidget(controller: _controller)),
Positioned.fill(
child: WebViewWidget(controller: _controller),
),
if (_errorMessage != null) _buildErrorState(),
if (_isLoading && _errorMessage == null)
const BaishunLoadingView(message: 'Waiting for gameLoaded...'),
const IgnorePointer(
ignoring: true,
child: BaishunLoadingView(
message: 'Waiting for gameLoaded...',
),
),
],
),
),
@ -266,19 +320,13 @@ class _BaishunGamePageState extends State<BaishunGamePage> {
Text(
_errorMessage ?? '',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white70,
fontSize: 12.sp,
),
style: TextStyle(color: Colors.white70, fontSize: 12.sp),
),
SizedBox(height: 16.w),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: _reload,
child: const Text('Retry'),
),
TextButton(onPressed: _reload, child: const Text('Retry')),
SizedBox(width: 10.w),
TextButton(
onPressed: () {