gpt4 book ai didi

Flutter 如何从 flutterWebviewPlugin.evalJavascript (addEventListener) 中检索 javascript 值

转载 作者:行者123 更新时间:2023-12-03 23:27:58 29 4
gpt4 key购买 nike

我有一个 flutter 代码片段,它从我的 iframe 页面监听 postMessage。
(flutter_webview_plugin: ^0.3.9+1)

flutterWebviewPlugin.onStateChanged.listen((viewState) async {
String script = 'window.addEventListener("message", receiveMessage, false);' +
'function receiveMessage(event) {console.log(\'receiving data from child , data as follows: \',event.data)}';
flutterWebviewPlugin.evalJavascript(script);
}

如果 ,我想触发特定的 flutter 功能事件数据返回 具体值 ,
相机 value 会触发我的 具体功能调用相机插件等等。意思是说我的目标 iFrame 将尝试通过 进行跨域通信。留言方法。

对于 Cordova,我可以这样做:
window.addEventListener( "message" , function( event )
{
else if( event.data.indexOf( "camera" ) >= 0 )
{
//Trigger Camera Function

我该如何为 flutter 做这件事?

最佳答案

2个解决方案:

解决方案 1 : 推荐

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

final Set<JavascriptChannel> jsChannels = [
JavascriptChannel(
name: 'Print',
onMessageReceived: (JavascriptMessage message) {
print('message.message: ${message.message}');
}),
].toSet();

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
"/": (_) => WebviewScaffold(
url: Uri.dataFromString(
'<html><button onclick="Print.postMessage(\'test\');">Click me</button></html>',
mimeType: 'text/html')
.toString(),
appBar: new AppBar(title: new Text("Widget webview")),
javascriptChannels: jsChannels,
),
},
);
}
}

I/flutter (22119): message.message: test



解决方案 2 : 如果你想保留你的 window.post . (也许你想使用另一个平台的 iframe)
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

final Set<JavascriptChannel> jsChannels = [
JavascriptChannel(
name: 'Print',
onMessageReceived: (JavascriptMessage message) {
print('message.message: ${message.message}');
}),
].toSet();

void main() {
final flutterWebviewPlugin = FlutterWebviewPlugin();
flutterWebviewPlugin.onStateChanged.listen((state) async {
if (state.type == WebViewState.finishLoad) {
String script =
'window.addEventListener("message", receiveMessage, false);' +
'function receiveMessage(event) {Print.postMessage(event.data);}';
flutterWebviewPlugin.evalJavascript(script);
}
});
return runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
"/": (_) => WebviewScaffold(
url: Uri.dataFromString(
'<html><button onclick="window.postMessage(\'test\', \'*\');">Click me</button></html>',
mimeType: 'text/html')
.toString(),
appBar: new AppBar(title: new Text("Widget webview")),
javascriptChannels: jsChannels,
),
},
);
}
}

I/flutter (22119): message.message: test


pubspec.yaml :
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
flutter_webview_plugin: 0.3.9+1

引用 :
  • https://github.com/fluttercommunity/flutter_webview_plugin/issues/309
  • https://github.com/fluttercommunity/flutter_webview_plugin/pull/457
  • https://github.com/fluttercommunity/flutter_webview_plugin/issues/364
  • https://github.com/fluttercommunity/flutter_webview_plugin/pull/523/files
  • https://github.com/fluttercommunity/flutter_webview_plugin/issues/305
  • 关于Flutter 如何从 flutterWebviewPlugin.evalJavascript (addEventListener) 中检索 javascript 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58985946/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com