Skip to content

[webview_flutter] Add javascript panel interface for flutter parts #5797

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 4.4.3
## 4.5.0

* Adds support to show JavaScript dialog. See `WebViewController.setOnJavaScriptAlertDialog`, `WebViewController.setOnJavaScriptConfirmDialog` and `WebViewController.setOnJavaScriptTextInputDialog`.
* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.
* Fixes new lint warnings.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.12.0
webview_flutter_wkwebview: ^3.9.0
webview_flutter_android: ^3.13.0
webview_flutter_wkwebview: ^3.10.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.3.0
webview_flutter_platform_interface: ^2.9.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 @@ -4,10 +4,13 @@

export 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'
show
JavaScriptAlertDialogRequest,
JavaScriptConfirmDialogRequest,
JavaScriptConsoleMessage,
JavaScriptLogLevel,
JavaScriptMessage,
JavaScriptMode,
JavaScriptTextInputDialogRequest,
LoadRequestMethod,
NavigationDecision,
NavigationRequest,
Expand Down
8 changes: 4 additions & 4 deletions packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.4.3
version: 4.5.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand All @@ -19,9 +19,9 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_android: ^3.12.0
webview_flutter_platform_interface: ^2.6.0
webview_flutter_wkwebview: ^3.9.0
webview_flutter_android: ^3.13.0
webview_flutter_platform_interface: ^2.9.0
webview_flutter_wkwebview: ^3.10.0

dev_dependencies:
build_runner: ^2.1.5
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
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,45 @@ class MockPlatformWebViewController extends _i1.Mock
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> setOnJavaScriptAlertDialog(
_i5.Future<void> Function(_i2.JavaScriptAlertDialogRequest)?
onJavaScriptAlertDialog) =>
(super.noSuchMethod(
Invocation.method(
#setOnJavaScriptAlertDialog,
[onJavaScriptAlertDialog],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> setOnJavaScriptConfirmDialog(
_i5.Future<bool> Function(_i2.JavaScriptConfirmDialogRequest)?
onJavaScriptConfirmDialog) =>
(super.noSuchMethod(
Invocation.method(
#setOnJavaScriptConfirmDialog,
[onJavaScriptConfirmDialog],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);

@override
_i5.Future<void> setOnJavaScriptTextInputDialog(
_i5.Future<String> Function(_i2.JavaScriptTextInputDialogRequest)?
onJavaScriptTextInputDialog) =>
(super.noSuchMethod(
Invocation.method(
#setOnJavaScriptTextInputDialog,
[onJavaScriptTextInputDialog],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
}

/// A class which mocks [PlatformNavigationDelegate].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,45 @@ class MockPlatformWebViewController extends _i1.Mock
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);

@override
_i7.Future<void> setOnJavaScriptAlertDialog(
_i7.Future<void> Function(_i2.JavaScriptAlertDialogRequest)?
onJavaScriptAlertDialog) =>
(super.noSuchMethod(
Invocation.method(
#setOnJavaScriptAlertDialog,
[onJavaScriptAlertDialog],
),
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);

@override
_i7.Future<void> setOnJavaScriptConfirmDialog(
_i7.Future<bool> Function(_i2.JavaScriptConfirmDialogRequest)?
onJavaScriptConfirmDialog) =>
(super.noSuchMethod(
Invocation.method(
#setOnJavaScriptConfirmDialog,
[onJavaScriptConfirmDialog],
),
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);

@override
_i7.Future<void> setOnJavaScriptTextInputDialog(
_i7.Future<String> Function(_i2.JavaScriptTextInputDialogRequest)?
onJavaScriptTextInputDialog) =>
(super.noSuchMethod(
Invocation.method(
#setOnJavaScriptTextInputDialog,
[onJavaScriptTextInputDialog],
),
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);
}

/// A class which mocks [PlatformWebViewWidget].
Expand Down