-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[webview_flutter_web] Add support for webview->host JavaScript channels #3655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5b5b6e3
adf825a
1be348e
01c905f
01cb968
b313f90
bd2f600
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:collection'; | ||
| import 'dart:convert'; | ||
| import 'dart:html' as html; | ||
|
|
||
|
|
@@ -57,6 +58,10 @@ class WebWebViewController extends PlatformWebViewController { | |
| WebWebViewControllerCreationParams get _webWebViewParams => | ||
| params as WebWebViewControllerCreationParams; | ||
|
|
||
| /// Mapping between channel names and message event handlers. | ||
| HashMap<String, void Function(html.Event)> javascriptChannels = | ||
| HashMap<String, void Function(html.Event)>(); | ||
|
|
||
| @override | ||
| Future<void> loadHtmlString(String html, {String? baseUrl}) async { | ||
| // ignore: unsafe_html | ||
|
|
@@ -106,6 +111,31 @@ class WebWebViewController extends PlatformWebViewController { | |
| encoding: encoding, | ||
| ).toString(); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> addJavaScriptChannel( | ||
| JavaScriptChannelParams javaScriptChannelParams, | ||
| ) async { | ||
| void handler(html.Event event) { | ||
| if (event is html.MessageEvent) { | ||
| javaScriptChannelParams.onMessageReceived( | ||
| JavaScriptMessage(message: event.data.toString())); | ||
| } | ||
| } | ||
|
|
||
| javascriptChannels[javaScriptChannelParams.name] = handler; | ||
| html.window.addEventListener('message', handler); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a programmer registers multiple 'message' handlers (one per
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is true. This can easily happen when you have multiple webviews in a single application. Since the webviews do not know anything from each other, we did not see another option. I think if you have a low amount of handlers (e.g. a handful), there is no performance hit.
On Android and iOS it is only possible to send string messages. We tried to mimic that API by limiting the allowed message types to string-values only. This way everything can be handled the same way in Flutter (i.e. all incoming messages are always string). If one wants to send complex messages one can always send a stringified object. |
||
| } | ||
|
|
||
| @override | ||
| Future<void> removeJavaScriptChannel(String javaScriptChannelName) async { | ||
| final void Function(html.Event)? handler = | ||
| javascriptChannels[javaScriptChannelName]; | ||
|
|
||
| if (handler != null) { | ||
| html.window.removeEventListener('message', handler); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// An implementation of [PlatformWebViewWidget] using Flutter the for Web API. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
window.flutterApp? Who's setting this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
webview_flutter is setting this on iOS and Android. If it's not available we assume we are on web and use the alternative.