Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
e25faa3
[webview_flutter_platform_interface] Update webview platform interfac…
BeMacized Oct 1, 2021
f19b5c3
Fix tests
BeMacized Oct 1, 2021
3a22d00
[webview_flutter] Implemented `runJavaScript` and `runJavaScriptForRe…
BeMacized Oct 1, 2021
2bcec1c
Remove accidental development team inclusion from project.pbxproj
BeMacized Oct 1, 2021
8e5a2de
[webview_flutter] Deprecate `evaluateJavascript` in favour of `runJav…
BeMacized Oct 1, 2021
5a84981
Updated changelog
BeMacized Oct 1, 2021
ff493a2
Implemented PR feedback
BeMacized Oct 1, 2021
6bf082f
Merge branch 'webview/js_eval_fix_platform' into webview/js_eval_fix_…
BeMacized Oct 1, 2021
ff125c9
Updated runJavaScriptForResult behaviour
BeMacized Oct 1, 2021
79707d7
Merge branch 'webview/js_eval_fix_native' into webview/js_eval_fix_app
BeMacized Oct 1, 2021
73f3407
Updated documentation
BeMacized Oct 1, 2021
9f661e8
Implemented PR feedback partially
BeMacized Oct 4, 2021
cbc88e8
Implement PR feedback
BeMacized Oct 4, 2021
9f537c7
Merge branch 'webview/js_eval_fix_platform' into webview/js_eval_fix_…
BeMacized Oct 4, 2021
26004c3
Update changelog
BeMacized Oct 4, 2021
283f8a7
Merge branch 'webview/js_eval_fix_platform' into webview/js_eval_fix_…
BeMacized Oct 4, 2021
b95d986
Implement PR feedback from interface PR
BeMacized Oct 4, 2021
3d8b9ce
Update changelog
BeMacized Oct 4, 2021
840a73e
Merge branch 'webview/js_eval_fix_native' into webview/js_eval_fix_app
BeMacized Oct 4, 2021
45c4a7d
Fix issues from merge.
BeMacized Oct 4, 2021
0ecf641
Revert inclusion of development team
BeMacized Oct 4, 2021
d7b57f4
Merge branch 'webview/js_eval_fix_native' into webview/js_eval_fix_app
BeMacized Oct 4, 2021
02e1106
Implement PR feedback
BeMacized Oct 5, 2021
d7780dd
Merge branch 'webview/js_eval_fix_platform' into webview/js_eval_fix_…
BeMacized Oct 5, 2021
d8f51ed
Implemented platform interface PR feedback
BeMacized Oct 5, 2021
3ccb8ae
Merge branch 'webview/js_eval_fix_native' into webview/js_eval_fix_app
BeMacized Oct 5, 2021
c884665
Implemented platform interface PR feedback
BeMacized Oct 5, 2021
285c2cc
Merge branch 'master' into webview/js_eval_fix_platform
BeMacized Oct 6, 2021
90cb648
Merge branch 'webview/js_eval_fix_platform' into webview/js_eval_fix_…
BeMacized Oct 6, 2021
156f77f
Merge branch 'webview/js_eval_fix_native' into webview/js_eval_fix_app
BeMacized Oct 6, 2021
bfcacdc
Update pubspec dependency
BeMacized Oct 6, 2021
bcb368c
Merge branch 'master' into webview/js_eval_fix_native
BeMacized Oct 6, 2021
485ead3
Fixed capitalisation
BeMacized Oct 6, 2021
a7d10c0
Fixed capitalisation
BeMacized Oct 6, 2021
d98bc6a
Fix warning
BeMacized Oct 7, 2021
738a9ad
Partially implement PR feedback
BeMacized Oct 8, 2021
527bd67
Partially implement PR feedback
BeMacized Oct 8, 2021
acf5ff9
Format
BeMacized Oct 8, 2021
aed3830
Update podfile
BeMacized Oct 8, 2021
6967c4f
Update podfile
BeMacized Oct 8, 2021
37ba26e
Update podfiles
BeMacized Oct 8, 2021
13076ba
Update iOS project files
BeMacized Oct 8, 2021
0c87362
Update podspec
BeMacized Oct 8, 2021
28ac939
Merge remote-tracking branch 'origin/webview/js_eval_fix_native' into…
BeMacized Oct 8, 2021
962ad02
Remove unnecessary podfile configuration
BeMacized Oct 8, 2021
9184d48
Implemented PR feedback
BeMacized Oct 8, 2021
a111502
Merge branch 'master' into webview/js_eval_fix_native
BeMacized Oct 18, 2021
80cbe9b
Format
BeMacized Oct 18, 2021
9fdab9d
Revert podfile changes
BeMacized Oct 22, 2021
6e609d7
Implemented PR feedback
BeMacized Oct 22, 2021
f6d8cb4
Fix formatting
BeMacized Oct 22, 2021
b019d3c
Fix formatting
BeMacized Oct 22, 2021
2b0f490
Fixed test.
BeMacized Oct 22, 2021
fa9526c
Re-add integration tests for deprecated evaluateJavascript method.
BeMacized Oct 26, 2021
45e799b
Merge branch 'master' into webview/js_eval_fix_native
BeMacized Oct 28, 2021
40a6b38
Merge branch 'master' into webview/js_eval_fix_native
BeMacized Oct 28, 2021
e2f4710
Fix merge conflicts
BeMacized Oct 28, 2021
29cca43
Merge branch 'webview/js_eval_fix_native' into webview/js_eval_fix_app
BeMacized Nov 1, 2021
fcf1723
Merge branch 'master' into webview/js_eval_fix_app
BeMacized Nov 1, 2021
a3656a3
Add missing test and format
BeMacized Nov 1, 2021
662e563
Fixed dependencies and updated changelog
BeMacized Nov 1, 2021
834465e
Ignore deprecated uses of evaluateJavascript in tests
BeMacized Nov 1, 2021
20b5a90
Fix analysis errors
BeMacized Nov 1, 2021
5c2885e
Fix unit tests
BeMacized Nov 1, 2021
6341867
Merge branch 'master' into webview/js_eval_fix_app
BeMacized Nov 2, 2021
37504c9
Implemented PR feedback
BeMacized Nov 4, 2021
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
Prev Previous commit
Next Next commit
[webview_flutter] Deprecate evaluateJavascript in favour of `runJav…
…aScript` and `runJavaScriptForResult`
  • Loading branch information
BeMacized committed Oct 1, 2021
commit 8e5a2de4b399765ed6647c0d02deee172936068c
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 @@
## 2.2.0

*

## 2.1.1

* Fixed `_CastError` that was thrown when running the example App.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void main() {
await pageLoads.stream.firstWhere((String url) => url == currentUrl);

final String content = await controller
.evaluateJavascript('document.documentElement.innerText');
.runJavaScriptForResult('document.documentElement.innerText');
expect(content.contains('flutter_test_header'), isTrue);
}, skip: Platform.isAndroid && _skipDueToIssue86757);

Expand Down Expand Up @@ -146,11 +146,7 @@ void main() {
await pageLoaded.future;

expect(messagesReceived, isEmpty);
// Append a return value "1" in the end will prevent an iOS platform exception.
// See: https://github.com/flutter/flutter/issues/66318#issuecomment-701105380
// TODO(cyanglaz): remove the workaround "1" in the end when the below issue is fixed.
// https://github.com/flutter/flutter/issues/66318
await controller.evaluateJavascript('Echo.postMessage("hello");1;');
await controller.runJavaScript('Echo.postMessage("hello");');
expect(messagesReceived, equals(<String>['hello']));
}, skip: Platform.isAndroid && _skipDueToIssue86757);

Expand Down Expand Up @@ -397,7 +393,7 @@ void main() {
WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;

String isPaused = await controller.evaluateJavascript('isPaused();');
String isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(false));

controllerCompleter = Completer<WebViewController>();
Expand Down Expand Up @@ -426,7 +422,7 @@ void main() {
controller = await controllerCompleter.future;
await pageLoaded.future;

isPaused = await controller.evaluateJavascript('isPaused();');
isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(true));
});

Expand Down Expand Up @@ -457,7 +453,7 @@ void main() {
final WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;

String isPaused = await controller.evaluateJavascript('isPaused();');
String isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(false));

pageLoaded = Completer<void>();
Expand Down Expand Up @@ -485,7 +481,7 @@ void main() {

await pageLoaded.future;

isPaused = await controller.evaluateJavascript('isPaused();');
isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(false));
});

Expand Down Expand Up @@ -535,7 +531,7 @@ void main() {
await videoPlaying.future;

String fullScreen =
await controller.evaluateJavascript('isFullScreen();');
await controller.runJavaScriptForResult('isFullScreen();');
expect(fullScreen, _webviewBool(false));
});

Expand Down Expand Up @@ -587,7 +583,7 @@ void main() {
await videoPlaying.future;

String fullScreen =
await controller.evaluateJavascript('isFullScreen();');
await controller.runJavaScriptForResult('isFullScreen();');
expect(fullScreen, _webviewBool(true));
}, skip: Platform.isAndroid);
});
Expand Down Expand Up @@ -653,7 +649,7 @@ void main() {
await pageStarted.future;
await pageLoaded.future;

String isPaused = await controller.evaluateJavascript('isPaused();');
String isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(false));

controllerCompleter = Completer<WebViewController>();
Expand Down Expand Up @@ -687,7 +683,7 @@ void main() {
await pageStarted.future;
await pageLoaded.future;

isPaused = await controller.evaluateJavascript('isPaused();');
isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(true));
});

Expand Down Expand Up @@ -723,7 +719,7 @@ void main() {
await pageStarted.future;
await pageLoaded.future;

String isPaused = await controller.evaluateJavascript('isPaused();');
String isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(false));

pageStarted = Completer<void>();
Expand Down Expand Up @@ -756,7 +752,7 @@ void main() {
await pageStarted.future;
await pageLoaded.future;

isPaused = await controller.evaluateJavascript('isPaused();');
isPaused = await controller.runJavaScriptForResult('isPaused();');
expect(isPaused, _webviewBool(false));
});
});
Expand Down Expand Up @@ -1021,14 +1017,14 @@ void main() {

final WebViewController controller = await controllerCompleter.future;
await pageLoaded.future;
final String viewportRectJSON = await _evaluateJavascript(
final String viewportRectJSON = await _runJavaScriptForResult(
controller, 'JSON.stringify(viewport.getBoundingClientRect())');
final Map<String, dynamic> viewportRectRelativeToViewport =
jsonDecode(viewportRectJSON);

// Check that the input is originally outside of the viewport.

final String initialInputClientRectJSON = await _evaluateJavascript(
final String initialInputClientRectJSON = await _runJavaScriptForResult(
controller, 'JSON.stringify(inputEl.getBoundingClientRect())');
final Map<String, dynamic> initialInputClientRectRelativeToViewport =
jsonDecode(initialInputClientRectJSON);
Expand All @@ -1038,11 +1034,11 @@ void main() {
viewportRectRelativeToViewport['bottom'],
isFalse);

await controller.evaluateJavascript('inputEl.focus()');
await controller.runJavaScript('inputEl.focus()');

// Check that focusing the input brought it into view.

final String lastInputClientRectJSON = await _evaluateJavascript(
final String lastInputClientRectJSON = await _runJavaScriptForResult(
controller, 'JSON.stringify(inputEl.getBoundingClientRect())');
final Map<String, dynamic> lastInputClientRectRelativeToViewport =
jsonDecode(lastInputClientRectJSON);
Expand Down Expand Up @@ -1100,7 +1096,7 @@ void main() {
await pageLoads.stream.first; // Wait for initial page load.
final WebViewController controller = await controllerCompleter.future;
await controller
.evaluateJavascript('location.href = "https://www.example.com/"');
.runJavaScript('location.href = "https://www.example.com/"');

await pageLoads.stream.first; // Wait for the next page load.
final String? currentUrl = await controller.currentUrl();
Expand Down Expand Up @@ -1231,7 +1227,7 @@ void main() {
await pageLoads.stream.first; // Wait for initial page load.
final WebViewController controller = await controllerCompleter.future;
await controller
.evaluateJavascript('location.href = "https://www.youtube.com/"');
.runJavaScript('location.href = "https://www.youtube.com/"');

// There should never be any second page load, since our new URL is
// blocked. Still wait for a potential page change for some time in order
Expand Down Expand Up @@ -1272,7 +1268,7 @@ void main() {
await pageLoads.stream.first; // Wait for initial page load.
final WebViewController controller = await controllerCompleter.future;
await controller
.evaluateJavascript('location.href = "https://www.example.com"');
.runJavaScript('location.href = "https://www.example.com"');

await pageLoads.stream.first; // Wait for second page to load.
final String? currentUrl = await controller.currentUrl();
Expand Down Expand Up @@ -1328,7 +1324,7 @@ void main() {
);
final WebViewController controller = await controllerCompleter.future;
await controller
.evaluateJavascript('window.open("https://flutter.dev/", "_blank")');
.runJavaScript('window.open("https://flutter.dev/", "_blank")');
await pageLoaded.future;
final String? currentUrl = await controller.currentUrl();
expect(currentUrl, 'https://flutter.dev/');
Expand Down Expand Up @@ -1364,8 +1360,7 @@ void main() {
await pageLoaded.future;
pageLoaded = Completer<void>();

await controller
.evaluateJavascript('window.open("https://www.example.com/")');
await controller.runJavaScript('window.open("https://www.example.com/")');
await pageLoaded.future;
pageLoaded = Completer<void>();
expect(controller.currentUrl(), completion('https://www.example.com/'));
Expand Down Expand Up @@ -1436,9 +1431,10 @@ void main() {
final WebViewController controller = await controllerCompleter.future;
await pageLoadCompleter.future;

expect(controller.evaluateJavascript('iframeLoaded'), completion('true'));
expect(controller.runJavaScriptForResult('iframeLoaded'),
completion('true'));
expect(
controller.evaluateJavascript(
controller.runJavaScriptForResult(
'document.querySelector("p") && document.querySelector("p").textContent'),
completion('null'),
);
Expand All @@ -1458,13 +1454,13 @@ String _webviewBool(bool value) {

/// Returns the value used for the HTTP User-Agent: request header in subsequent HTTP requests.
Future<String> _getUserAgent(WebViewController controller) async {
return _evaluateJavascript(controller, 'navigator.userAgent;');
return _runJavaScriptForResult(controller, 'navigator.userAgent;');
}

Future<String> _evaluateJavascript(
Future<String> _runJavaScriptForResult(
WebViewController controller, String js) async {
if (defaultTargetPlatform == TargetPlatform.iOS) {
return await controller.evaluateJavascript(js);
return await controller.runJavaScriptForResult(js);
}
return jsonDecode(await controller.evaluateJavascript(js));
return jsonDecode(await controller.runJavaScriptForResult(js));
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ class SampleMenu extends StatelessWidget {
WebViewController controller, BuildContext context) async {
// Send a message with the user agent string to the Toaster JavaScript channel we registered
// with the WebView.
await controller.evaluateJavascript(
await controller.runJavaScript(
'Toaster.postMessage("User Agent: " + navigator.userAgent);');
}

void _onListCookies(
WebViewController controller, BuildContext context) async {
final String cookies =
await controller.evaluateJavascript('document.cookie');
await controller.runJavaScriptForResult('document.cookie');
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(SnackBar(
content: Column(
Expand All @@ -232,7 +232,7 @@ class SampleMenu extends StatelessWidget {
}

void _onAddToCache(WebViewController controller, BuildContext context) async {
await controller.evaluateJavascript(
await controller.runJavaScript(
'caches.open("test_caches_entry"); localStorage["test_localStorage"] = "dummy_entry";');
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(const SnackBar(
Expand All @@ -241,7 +241,7 @@ class SampleMenu extends StatelessWidget {
}

void _onListCache(WebViewController controller, BuildContext context) async {
await controller.evaluateJavascript('caches.keys()'
await controller.runJavaScript('caches.keys()'
'.then((cacheKeys) => JSON.stringify({"cacheKeys" : cacheKeys, "localStorage" : localStorage}))'
'.then((caches) => Toaster.postMessage(caches))');
}
Expand Down
41 changes: 38 additions & 3 deletions packages/webview_flutter/webview_flutter/lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,52 @@ class WebViewController {
/// When evaluating Javascript in a [WebView], it is best practice to wait for
/// the [WebView.onPageFinished] callback. This guarantees all the Javascript
/// embedded in the main frame HTML has been loaded.
@Deprecated('Use [runJavaScript] or [runJavascriptForResult]')
Future<String> evaluateJavascript(String javascriptString) {
if (_settings.javascriptMode == JavascriptMode.disabled) {
return Future<String>.error(FlutterError(
'JavaScript mode must be enabled/unrestricted when calling evaluateJavascript.'));
}
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
return _webViewPlatformController.evaluateJavascript(javascriptString);
}

/// Runs the given JavaScript in the context of the current page.
/// If you are looking for the result, use [runJavascriptForResult] instead.
/// The Future completes with an error if a JavaScript error occurred.
///
/// When running Javascript in a [WebView], it is best practice to wait for
// the [WebView.onPageFinished] callback. This guarantees all the Javascript
// embedded in the main frame HTML has been loaded.
Future<void> runJavaScript(String javaScriptString) {
if (_settings.javascriptMode == JavascriptMode.disabled) {
return Future<void>.error(FlutterError(
'JavaScript mode must be enabled/unrestricted when calling runJavaScript.'));
}
return _webViewPlatformController.runJavaScript(javaScriptString);
}

/// Runs the given JavaScript in the context of the current page, and returns the result.
///
/// On Android returns the evaluation result as a JSON formatted string.
///
/// On iOS depending on the value type the return value would be one of:
/// - For primitive JavaScript types: the value string formatted (e.g JavaScript 100 returns '100').
/// - For JavaScript arrays of supported types: a string formatted NSArray(e.g '(1,2,3), note that the string for NSArray is formatted and might contain newlines and extra spaces.').
Copy link
Contributor

Choose a reason for hiding this comment

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

Please wrap to 80 characters.

/// - Other non-primitive types are not supported on iOS and will return as null.
///
/// The Future completes with an error if a JavaScript error occurred.
///
/// When evaluating Javascript in a [WebView], it is best practice to wait for
/// the [WebView.onPageFinished] callback. This guarantees all the Javascript
/// embedded in the main frame HTML has been loaded.
Future<String> runJavaScriptForResult(String javaScriptString) {
if (_settings.javascriptMode == JavascriptMode.disabled) {
return Future<String>.error(FlutterError(
'JavaScript mode must be enabled/unrestricted when calling runJavaScriptForResult.'));
}
return _webViewPlatformController.runJavaScriptForResult(javaScriptString);
}

/// Returns the title of the currently loaded page.
Future<String?> getTitle() {
return _webViewPlatformController.getTitle();
Expand Down
14 changes: 10 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/plugins/tree/master/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: 2.1.1
version: 2.2.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -19,9 +19,15 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_platform_interface: ^1.0.0
webview_flutter_android: ^2.0.13
webview_flutter_wkwebview: ^2.0.13
# TODO (BeMacized): Change to version dependency once interface is published
webview_flutter_platform_interface:
path: ../webview_flutter_platform_interface
# TODO (BeMacized): Change to version dependency once interface is published
webview_flutter_android:
path: ../webview_flutter_android
# TODO (BeMacized): Change to version dependency once interface is published
webview_flutter_wkwebview:
path: ../webview_flutter_wkwebview

dev_dependencies:
flutter_driver:
Expand Down
Loading