Skip to content

Commit

Permalink
[webview_flutter] Add interface for showing javascript dialog message (
Browse files Browse the repository at this point in the history
…flutter#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 flutter#4555
* Related issue: flutter/flutter#30358 (comment)
* Related Interface PR: flutter#5670
  • Loading branch information
jsharp83 committed Feb 9, 2024
1 parent 90baeee commit 11152d2
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 16 deletions.
7 changes: 7 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 4.6.0

* Adds support for custom handling of JavaScript dialogs. See
`WebViewController.setOnJavaScriptAlertDialog`, `WebViewController.setOnJavaScriptConfirmDialog`
and `WebViewController.setOnJavaScriptTextInputDialog`.
* Updates minimum Dart version to 3.2.3 and minimum Flutter version to 3.16.6.

## 4.5.0

* Adds support for HTTP basic authentication. See `NavigationDelegate(onReceivedHttpAuthRequest)`.
Expand Down
10 changes: 5 additions & 5 deletions packages/webview_flutter/webview_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ description: Demonstrates how to use the webview_flutter plugin.
publish_to: none

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
sdk: ^3.2.3
flutter: ">=3.16.6"

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

dev_dependencies:
build_runner: ^2.1.5
Expand All @@ -27,7 +27,7 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
webview_flutter_platform_interface: ^2.7.0
webview_flutter_platform_interface: ^2.10.0

flutter:
uses-material-design: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,30 @@ class WebViewController {
return platform.setOnConsoleMessage(onConsoleMessage);
}

/// Sets a callback that notifies the host application that the web page
/// wants to display a JavaScript alert() dialog.
Future<void> setOnJavaScriptAlertDialog(
Future<void> Function(JavaScriptAlertDialogRequest request)
onJavaScriptAlertDialog) async {
return platform.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog);
}

/// Sets a callback that notifies the host application that the web page
/// wants to display a JavaScript confirm() dialog.
Future<void> setOnJavaScriptConfirmDialog(
Future<bool> Function(JavaScriptConfirmDialogRequest request)
onJavaScriptConfirmDialog) async {
return platform.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog);
}

/// Sets a callback that notifies the host application that the web page
/// wants to display a JavaScript prompt() dialog.
Future<void> setOnJavaScriptTextInputDialog(
Future<String> Function(JavaScriptTextInputDialogRequest request)
onJavaScriptTextInputDialog) async {
return platform.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog);
}

/// Gets the value used for the HTTP `User-Agent:` request header.
Future<String?> getUserAgent() {
return platform.getUserAgent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
export 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'
show
HttpAuthRequest,
JavaScriptAlertDialogRequest,
JavaScriptConfirmDialogRequest,
JavaScriptConsoleMessage,
JavaScriptLogLevel,
JavaScriptMessage,
JavaScriptMode,
JavaScriptTextInputDialogRequest,
LoadRequestMethod,
NavigationDecision,
NavigationRequest,
Expand Down
12 changes: 6 additions & 6 deletions packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 4.5.0
version: 4.6.0

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
sdk: ^3.2.3
flutter: ">=3.16.6"

flutter:
plugin:
Expand All @@ -19,9 +19,9 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_android: ^3.13.0
webview_flutter_platform_interface: ^2.7.0
webview_flutter_wkwebview: ^3.10.0
webview_flutter_android: ^3.14.0
webview_flutter_platform_interface: ^2.10.0
webview_flutter_wkwebview: ^3.11.0

dev_dependencies:
build_runner: ^2.1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,13 +1319,13 @@ class MatchesWebSettings extends Matcher {
bool matches(
covariant WebSettings webSettings, Map<dynamic, dynamic> matchState) {
return _webSettings!.javascriptMode == webSettings.javascriptMode &&
_webSettings!.hasNavigationDelegate ==
_webSettings.hasNavigationDelegate ==
webSettings.hasNavigationDelegate &&
_webSettings!.debuggingEnabled == webSettings.debuggingEnabled &&
_webSettings!.gestureNavigationEnabled ==
_webSettings.debuggingEnabled == webSettings.debuggingEnabled &&
_webSettings.gestureNavigationEnabled ==
webSettings.gestureNavigationEnabled &&
_webSettings!.userAgent == webSettings.userAgent &&
_webSettings!.zoomEnabled == webSettings.zoomEnabled;
_webSettings.userAgent == webSettings.userAgent &&
_webSettings.zoomEnabled == webSettings.zoomEnabled;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,62 @@ void main() {
verify(mockPlatformWebViewController.setOnConsoleMessage(onConsoleMessage));
});

test('setOnJavaScriptAlertDialog', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();

final WebViewController webViewController = WebViewController.fromPlatform(
mockPlatformWebViewController,
);

Future<void> onJavaScriptAlertDialog(
JavaScriptAlertDialogRequest request) async {
return;
}

await webViewController.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog);
verify(mockPlatformWebViewController
.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog));
});

test('setOnJavaScriptConfirmDialog', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();

final WebViewController webViewController = WebViewController.fromPlatform(
mockPlatformWebViewController,
);

Future<bool> onJavaScriptConfirmDialog(
JavaScriptConfirmDialogRequest request) async {
return true;
}

await webViewController
.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog);
verify(mockPlatformWebViewController
.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog));
});

test('setOnJavaScriptTextInputDialog', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();

final WebViewController webViewController = WebViewController.fromPlatform(
mockPlatformWebViewController,
);

Future<String> onJavaScriptTextInputDialog(
JavaScriptTextInputDialogRequest request) async {
return 'text';
}

await webViewController
.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog);
verify(mockPlatformWebViewController
.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog));
});

test('getUserAgent', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();
Expand Down

0 comments on commit 11152d2

Please sign in to comment.