Skip to content

Commit 11152d2

Browse files
authored
[webview_flutter] Add interface for showing javascript dialog message (#4704)
* there are cases where Web calls System Popup with javascript on webview_flutter * At this time, the message comes in the WKUIDelegate part in iOS. * https://developer.apple.com/documentation/webkit/wkuidelegate/1537406-webview * https://developer.apple.com/documentation/webkit/wkuidelegate/1536489-webview * Android also has a interface on WebChromeClient * https://developer.android.com/reference/android/webkit/WebChromeClient#onJsAlert(android.webkit.WebView,%20java.lang.String,%20java.lang.String,%20android.webkit.JsResult) * It was implemented according to the requirements of the code review of the #4555 * Related issue: flutter/flutter#30358 (comment) * Related Interface PR: #5670
1 parent 90baeee commit 11152d2

File tree

7 files changed

+106
-16
lines changed

7 files changed

+106
-16
lines changed

packages/webview_flutter/webview_flutter/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 4.6.0
2+
3+
* Adds support for custom handling of JavaScript dialogs. See
4+
`WebViewController.setOnJavaScriptAlertDialog`, `WebViewController.setOnJavaScriptConfirmDialog`
5+
and `WebViewController.setOnJavaScriptTextInputDialog`.
6+
* Updates minimum Dart version to 3.2.3 and minimum Flutter version to 3.16.6.
7+
18
## 4.5.0
29

310
* Adds support for HTTP basic authentication. See `NavigationDelegate(onReceivedHttpAuthRequest)`.

packages/webview_flutter/webview_flutter/example/pubspec.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ description: Demonstrates how to use the webview_flutter plugin.
33
publish_to: none
44

55
environment:
6-
sdk: ">=3.0.0 <4.0.0"
7-
flutter: ">=3.10.0"
6+
sdk: ^3.2.3
7+
flutter: ">=3.16.6"
88

99
dependencies:
1010
flutter:
@@ -17,8 +17,8 @@ dependencies:
1717
# The example app is bundled with the plugin so we use a path dependency on
1818
# the parent directory to use the current plugin's version.
1919
path: ../
20-
webview_flutter_android: ^3.13.0
21-
webview_flutter_wkwebview: ^3.10.0
20+
webview_flutter_android: ^3.14.0
21+
webview_flutter_wkwebview: ^3.11.0
2222

2323
dev_dependencies:
2424
build_runner: ^2.1.5
@@ -27,7 +27,7 @@ dev_dependencies:
2727
sdk: flutter
2828
integration_test:
2929
sdk: flutter
30-
webview_flutter_platform_interface: ^2.7.0
30+
webview_flutter_platform_interface: ^2.10.0
3131

3232
flutter:
3333
uses-material-design: true

packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,30 @@ class WebViewController {
370370
return platform.setOnConsoleMessage(onConsoleMessage);
371371
}
372372

373+
/// Sets a callback that notifies the host application that the web page
374+
/// wants to display a JavaScript alert() dialog.
375+
Future<void> setOnJavaScriptAlertDialog(
376+
Future<void> Function(JavaScriptAlertDialogRequest request)
377+
onJavaScriptAlertDialog) async {
378+
return platform.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog);
379+
}
380+
381+
/// Sets a callback that notifies the host application that the web page
382+
/// wants to display a JavaScript confirm() dialog.
383+
Future<void> setOnJavaScriptConfirmDialog(
384+
Future<bool> Function(JavaScriptConfirmDialogRequest request)
385+
onJavaScriptConfirmDialog) async {
386+
return platform.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog);
387+
}
388+
389+
/// Sets a callback that notifies the host application that the web page
390+
/// wants to display a JavaScript prompt() dialog.
391+
Future<void> setOnJavaScriptTextInputDialog(
392+
Future<String> Function(JavaScriptTextInputDialogRequest request)
393+
onJavaScriptTextInputDialog) async {
394+
return platform.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog);
395+
}
396+
373397
/// Gets the value used for the HTTP `User-Agent:` request header.
374398
Future<String?> getUserAgent() {
375399
return platform.getUserAgent();

packages/webview_flutter/webview_flutter/lib/webview_flutter.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
export 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'
66
show
77
HttpAuthRequest,
8+
JavaScriptAlertDialogRequest,
9+
JavaScriptConfirmDialogRequest,
810
JavaScriptConsoleMessage,
911
JavaScriptLogLevel,
1012
JavaScriptMessage,
1113
JavaScriptMode,
14+
JavaScriptTextInputDialogRequest,
1215
LoadRequestMethod,
1316
NavigationDecision,
1417
NavigationRequest,

packages/webview_flutter/webview_flutter/pubspec.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: webview_flutter
22
description: A Flutter plugin that provides a WebView widget on Android and iOS.
33
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 4.5.0
5+
version: 4.6.0
66

77
environment:
8-
sdk: ">=3.0.0 <4.0.0"
9-
flutter: ">=3.10.0"
8+
sdk: ^3.2.3
9+
flutter: ">=3.16.6"
1010

1111
flutter:
1212
plugin:
@@ -19,9 +19,9 @@ flutter:
1919
dependencies:
2020
flutter:
2121
sdk: flutter
22-
webview_flutter_android: ^3.13.0
23-
webview_flutter_platform_interface: ^2.7.0
24-
webview_flutter_wkwebview: ^3.10.0
22+
webview_flutter_android: ^3.14.0
23+
webview_flutter_platform_interface: ^2.10.0
24+
webview_flutter_wkwebview: ^3.11.0
2525

2626
dev_dependencies:
2727
build_runner: ^2.1.5

packages/webview_flutter/webview_flutter/test/legacy/webview_flutter_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,13 +1319,13 @@ class MatchesWebSettings extends Matcher {
13191319
bool matches(
13201320
covariant WebSettings webSettings, Map<dynamic, dynamic> matchState) {
13211321
return _webSettings!.javascriptMode == webSettings.javascriptMode &&
1322-
_webSettings!.hasNavigationDelegate ==
1322+
_webSettings.hasNavigationDelegate ==
13231323
webSettings.hasNavigationDelegate &&
1324-
_webSettings!.debuggingEnabled == webSettings.debuggingEnabled &&
1325-
_webSettings!.gestureNavigationEnabled ==
1324+
_webSettings.debuggingEnabled == webSettings.debuggingEnabled &&
1325+
_webSettings.gestureNavigationEnabled ==
13261326
webSettings.gestureNavigationEnabled &&
1327-
_webSettings!.userAgent == webSettings.userAgent &&
1328-
_webSettings!.zoomEnabled == webSettings.zoomEnabled;
1327+
_webSettings.userAgent == webSettings.userAgent &&
1328+
_webSettings.zoomEnabled == webSettings.zoomEnabled;
13291329
}
13301330
}
13311331

packages/webview_flutter/webview_flutter/test/webview_controller_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,62 @@ void main() {
403403
verify(mockPlatformWebViewController.setOnConsoleMessage(onConsoleMessage));
404404
});
405405

406+
test('setOnJavaScriptAlertDialog', () async {
407+
final MockPlatformWebViewController mockPlatformWebViewController =
408+
MockPlatformWebViewController();
409+
410+
final WebViewController webViewController = WebViewController.fromPlatform(
411+
mockPlatformWebViewController,
412+
);
413+
414+
Future<void> onJavaScriptAlertDialog(
415+
JavaScriptAlertDialogRequest request) async {
416+
return;
417+
}
418+
419+
await webViewController.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog);
420+
verify(mockPlatformWebViewController
421+
.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog));
422+
});
423+
424+
test('setOnJavaScriptConfirmDialog', () async {
425+
final MockPlatformWebViewController mockPlatformWebViewController =
426+
MockPlatformWebViewController();
427+
428+
final WebViewController webViewController = WebViewController.fromPlatform(
429+
mockPlatformWebViewController,
430+
);
431+
432+
Future<bool> onJavaScriptConfirmDialog(
433+
JavaScriptConfirmDialogRequest request) async {
434+
return true;
435+
}
436+
437+
await webViewController
438+
.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog);
439+
verify(mockPlatformWebViewController
440+
.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog));
441+
});
442+
443+
test('setOnJavaScriptTextInputDialog', () async {
444+
final MockPlatformWebViewController mockPlatformWebViewController =
445+
MockPlatformWebViewController();
446+
447+
final WebViewController webViewController = WebViewController.fromPlatform(
448+
mockPlatformWebViewController,
449+
);
450+
451+
Future<String> onJavaScriptTextInputDialog(
452+
JavaScriptTextInputDialogRequest request) async {
453+
return 'text';
454+
}
455+
456+
await webViewController
457+
.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog);
458+
verify(mockPlatformWebViewController
459+
.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog));
460+
});
461+
406462
test('getUserAgent', () async {
407463
final MockPlatformWebViewController mockPlatformWebViewController =
408464
MockPlatformWebViewController();

0 commit comments

Comments
 (0)