Skip to content
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

[webview_flutter] Add interface for showing javascript dialog message #4704

Merged
merged 12 commits into from
Feb 9, 2024
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