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] Adds option to register JavaScript console callback #4541

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5b3f013
Adds JavaScript console override on WKWebView
mvanbeusekom Jul 21, 2023
2a388de
Adds JavaScript console override on WKWebView
mvanbeusekom Jul 21, 2023
3aa11af
Fix setConsoleLogCallback unit-test
mvanbeusekom Jul 21, 2023
0b08773
Fix analyze warning
mvanbeusekom Jul 21, 2023
fe22549
Adds the JavaScript info loglevel
mvanbeusekom Jul 24, 2023
0c45243
Fix integration test and dependencies
mvanbeusekom Jul 24, 2023
94775bb
wrap onConsoleMessage
bparrishMines Jul 25, 2023
d6b0600
Adds console log callback to Android and App facing packages
mvanbeusekom Jul 27, 2023
2eee568
Fixed formatting
mvanbeusekom Jul 27, 2023
e21cd64
Adds missing license block
mvanbeusekom Jul 27, 2023
e9b0f3d
Update packages/webview_flutter/webview_flutter_wkwebview/lib/src/web…
mvanbeusekom Aug 3, 2023
42b1210
Update packages/webview_flutter/webview_flutter_wkwebview/lib/src/web…
mvanbeusekom Aug 3, 2023
2a4dc3f
Merge remote-tracking branch 'upstream/main' into feature/forward_jav…
mvanbeusekom Aug 3, 2023
dafed42
Apply PR feedback
mvanbeusekom Aug 3, 2023
9e7fed2
Merge branch 'feature/forward_javascript_console' of github.com:basef…
mvanbeusekom Aug 3, 2023
de2a099
Apply PR feedback
mvanbeusekom Aug 3, 2023
6759419
Merge branch 'main' into feature/forward_javascript_console
mvanbeusekom Aug 3, 2023
4482d9a
Map TIP to JavaScriptConsoleLevel.debug
mvanbeusekom Aug 3, 2023
0a4f984
Merge branch 'feature/forward_javascript_console' of github.com:basef…
mvanbeusekom Aug 3, 2023
9b02117
Fix formatting and analyze warning
mvanbeusekom Aug 4, 2023
94ec766
Merge branch 'main' into feature/forward_javascript_console
mvanbeusekom Aug 4, 2023
56b2a5e
Generate updates test mocks
mvanbeusekom Aug 4, 2023
07f68b2
Merge branch 'main' of https://github.com/flutter/packages into featu…
mvanbeusekom Aug 4, 2023
4910c2e
Merge branch 'feature/forward_javascript_console' of github.com:basef…
mvanbeusekom Aug 4, 2023
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
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.3.0

* Adds support to register a callback to receive JavaScript console messages. See `WebViewController.setConsoleLogCallback`.

## 4.2.2

* Fixes documentation typo.
Expand Down
52 changes: 52 additions & 0 deletions packages/webview_flutter/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ const String kTransparentBackgroundPage = '''
</html>
''';

const String kLogExamplePage = '''
<!DOCTYPE html>
<html lang="en">
<head>
<title>Load file or HTML string example</title>
</head>
<body onload="console.log('Logging that the page is loading.')">

<h1>Local demo page</h1>
<p>
This page is used to test the forwarding of console logs to Dart.
</p>

<style>
.btn-group button {
padding: 24px; 24px;
display: block;
width: 25%;
margin: 5px 0px 0px 0px;
}
</style>

<div class="btn-group">
<button onclick="console.error('This is an error message.')">Error</button>
<button onclick="console.warn('This is a warning message.')">Warning</button>
<button onclick="console.info('This is a info message.')">Info</button>
<button onclick="console.debug('This is a debug message.')">Debug</button>
<button onclick="console.log('This is a log message.')">Log</button>
</div>

</body>
</html>
''';

class WebViewExample extends StatefulWidget {
const WebViewExample({super.key});

Expand Down Expand Up @@ -208,6 +242,7 @@ enum MenuOptions {
loadHtmlString,
transparentBackground,
setCookie,
logExample,
}

class SampleMenu extends StatelessWidget {
Expand Down Expand Up @@ -264,6 +299,9 @@ class SampleMenu extends StatelessWidget {
case MenuOptions.setCookie:
_onSetCookie();
break;
case MenuOptions.logExample:
_onLogExample();
break;
}
},
itemBuilder: (BuildContext context) => <PopupMenuItem<MenuOptions>>[
Expand Down Expand Up @@ -320,6 +358,10 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.setCookie,
child: Text('Set cookie'),
),
const PopupMenuItem<MenuOptions>(
value: MenuOptions.logExample,
child: Text('Log example'),
),
],
);
}
Expand Down Expand Up @@ -463,6 +505,16 @@ class SampleMenu extends StatelessWidget {

return indexFile.path;
}

Future<void> _onLogExample() {
webViewController
.setOnConsoleMessage((JavaScriptConsoleMessage consoleMessage) {
debugPrint(
'== JS == ${consoleMessage.level.name}: ${consoleMessage.message}');
});

return webViewController.loadHtmlString(kLogExamplePage);
}
}

class NavigationControls extends StatelessWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ flutter:
- assets/sample_video.mp4
- assets/www/index.html
- assets/www/styles/style.css

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{webview_flutter: {path: ../../../webview_flutter/webview_flutter}, webview_flutter_android: {path: ../../../webview_flutter/webview_flutter_android}, webview_flutter_platform_interface: {path: ../../../webview_flutter/webview_flutter_platform_interface}, webview_flutter_wkwebview: {path: ../../../webview_flutter/webview_flutter_wkwebview}}
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ class WebViewController {
Future<void> setUserAgent(String? userAgent) {
return platform.setUserAgent(userAgent);
}

/// Sets a callback that notifies the host application on any log messages
/// written to the JavaScript console.
///
/// Platforms may not preserve all the log level information so clients should
/// not rely on a 1:1 mapping between the JavaScript calls.
///
/// On iOS setting this callback will inject a custom [WKUserScript] which
/// overrides the default implementation of `console.debug`, `console.error`,
/// `console.info`, `console.log` and `console.warning` methods. The iOS
/// WebKit framework unfortunately doesn't provide a build in method to
/// forward console messages.
Comment on lines +363 to +367
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to include this info in the app-facing package since this is already included in the webview_flutter_wkebview implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means that the documentation will not show up in the IDE of developers calling the method. I put it here on purpose so developers setting the method are clearly informed.

Future<void> setOnConsoleMessage(
void Function(JavaScriptConsoleMessage message) onConsoleMessage) {
return platform.setOnConsoleMessage(onConsoleMessage);
}
}

/// Permissions request when web content requests access to protected resources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library webview_flutter;

export 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'
show
JavaScriptConsoleMessage,
JavaScriptMessage,
JavaScriptMode,
LoadRequestMethod,
Expand Down
7 changes: 6 additions & 1 deletion 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.2.2
version: 4.3.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand All @@ -29,3 +29,8 @@ dev_dependencies:
sdk: flutter
mockito: 5.4.1
plugin_platform_interface: ^2.1.3

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#changing-federated-plugins
dependency_overrides:
{webview_flutter_android: {path: ../../webview_flutter/webview_flutter_android}, webview_flutter_platform_interface: {path: ../../webview_flutter/webview_flutter_platform_interface}, webview_flutter_wkwebview: {path: ../../webview_flutter/webview_flutter_wkwebview}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/legacy/webview_flutter_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i9;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/navigation_delegate_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i8;

Expand Down Expand Up @@ -208,6 +210,16 @@ class MockPlatformNavigationDelegate extends _i1.Mock
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);
@override
_i8.Future<void> setOnHttpError(_i3.HttpResponseErrorCallback? onHttpError) =>
(super.noSuchMethod(
Invocation.method(
#setOnHttpError,
[onHttpError],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);
@override
_i8.Future<void> setOnProgress(_i3.ProgressCallback? onProgress) =>
(super.noSuchMethod(
Invocation.method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ void main() {
requestCallback(const TestPlatformWebViewPermissionRequest());
expect(permissionRequestCallbackCalled, isTrue);
});

test('setConsoleLogCallback', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();
final WebViewController webViewController = WebViewController.fromPlatform(
mockPlatformWebViewController,
);

void onConsoleMessage(JavaScriptConsoleMessage message) {}

await webViewController.setOnConsoleMessage(onConsoleMessage);

verify(mockPlatformWebViewController.setOnConsoleMessage(onConsoleMessage));
});
}

class TestPlatformWebViewPermissionRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/webview_controller_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:ui' as _i3;
Expand Down Expand Up @@ -353,6 +355,17 @@ class MockPlatformWebViewController extends _i1.Mock
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setOnConsoleMessage(
void Function(_i2.JavaScriptConsoleMessage)? onConsoleMessage) =>
(super.noSuchMethod(
Invocation.method(
#setOnConsoleMessage,
[onConsoleMessage],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
}

/// A class which mocks [PlatformNavigationDelegate].
Expand Down Expand Up @@ -405,6 +418,16 @@ class MockPlatformNavigationDelegate extends _i1.Mock
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setOnHttpError(_i6.HttpResponseErrorCallback? onHttpError) =>
(super.noSuchMethod(
Invocation.method(
#setOnHttpError,
[onHttpError],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setOnProgress(_i6.ProgressCallback? onProgress) =>
(super.noSuchMethod(
Invocation.method(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/webview_cookie_manager_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/webview_widget_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i7;
import 'dart:ui' as _i3;
Expand Down Expand Up @@ -371,6 +373,17 @@ class MockPlatformWebViewController extends _i1.Mock
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);
@override
_i7.Future<void> setOnConsoleMessage(
void Function(_i2.JavaScriptConsoleMessage)? onConsoleMessage) =>
(super.noSuchMethod(
Invocation.method(
#setOnConsoleMessage,
[onConsoleMessage],
),
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);
}

/// A class which mocks [PlatformWebViewWidget].
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.10.0

* Adds support to register a callback to receive JavaScript console messages. See `AndroidWebViewController.setConsoleLogCallback`.

## 3.9.2

* Fixes bug where `PlatformWebViewWidget` doesn't rebuild when the controller or PlatformView
Expand Down
Loading