Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit cd5dc3b

Browse files
authored
[webview_flutter] Update webview platform interface with new methods for running JavaScript. (#4401)
1 parent 0558169 commit cd5dc3b

File tree

6 files changed

+81
-12
lines changed

6 files changed

+81
-12
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.0
2+
3+
* Added `runJavascript` and `runJavascriptReturningResult` interface methods to supersede `evaluateJavascript`.
4+
15
## 1.1.0
26

37
* Add `zoomEnabled` functionality to `WebSettings`.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,21 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
123123
}
124124

125125
@override
126-
Future<String> evaluateJavascript(String javascriptString) {
126+
Future<String> evaluateJavascript(String javascript) {
127127
return _channel
128-
.invokeMethod<String>('evaluateJavascript', javascriptString)
128+
.invokeMethod<String>('evaluateJavascript', javascript)
129+
.then((result) => result!);
130+
}
131+
132+
@override
133+
Future<void> runJavascript(String javascript) async {
134+
await _channel.invokeMethod<String>('runJavascript', javascript);
135+
}
136+
137+
@override
138+
Future<String> runJavascriptReturningResult(String javascript) {
139+
return _channel
140+
.invokeMethod<String>('runJavascriptReturningResult', javascript)
129141
.then((result) => result!);
130142
}
131143

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,30 @@ abstract class WebViewPlatformController {
106106
/// Evaluates a JavaScript expression in the context of the current page.
107107
///
108108
/// The Future completes with an error if a JavaScript error occurred, or if the type of the
109-
/// evaluated expression is not supported(e.g on iOS not all non primitive type can be evaluated).
110-
Future<String> evaluateJavascript(String javascriptString) {
109+
/// evaluated expression is not supported (e.g on iOS not all non-primitive types can be evaluated).
110+
Future<String> evaluateJavascript(String javascript) {
111111
throw UnimplementedError(
112112
"WebView evaluateJavascript is not implemented on the current platform");
113113
}
114114

115+
/// Runs the given JavaScript in the context of the current page.
116+
///
117+
/// The Future completes with an error if a JavaScript error occurred.
118+
Future<void> runJavascript(String javascript) {
119+
throw UnimplementedError(
120+
"WebView runJavascript is not implemented on the current platform");
121+
}
122+
123+
/// Runs the given JavaScript in the context of the current page, and returns the result.
124+
///
125+
/// The Future completes with an error if a JavaScript error occurred, or if the
126+
/// type the given expression evaluates to is unsupported. Unsupported values include
127+
/// certain non-primitive types on iOS, as well as `undefined` or `null` on iOS 14+.
128+
Future<String> runJavascriptReturningResult(String javascript) {
129+
throw UnimplementedError(
130+
"WebView runJavascriptReturningResult is not implemented on the current platform");
131+
}
132+
115133
/// Adds new JavaScript channels to the set of enabled channels.
116134
///
117135
/// For each value in this list the platform's webview should make sure that a corresponding
@@ -130,7 +148,7 @@ abstract class WebViewPlatformController {
130148

131149
/// Removes JavaScript channel names from the set of enabled channels.
132150
///
133-
/// This disables channels that were previously enabled by [addJavaScriptChannels] or through
151+
/// This disables channels that were previously enabled by [addJavascriptChannels] or through
134152
/// [CreationParams.javascriptChannelNames].
135153
Future<void> removeJavascriptChannels(Set<String> javascriptChannelNames) {
136154
throw UnimplementedError(

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/javascript_channel.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
import 'javascript_message.dart';
66

7-
/// Callback type for handling messages sent from Javascript running in a web view.
7+
/// Callback type for handling messages sent from JavaScript running in a web view.
88
typedef void JavascriptMessageHandler(JavascriptMessage message);
99

1010
final RegExp _validChannelNames = RegExp('^[a-zA-Z_][a-zA-Z0-9_]*\$');
1111

1212
/// A named channel for receiving messaged from JavaScript code running inside a web view.
1313
class JavascriptChannel {
14-
/// Constructs a Javascript channel.
14+
/// Constructs a JavaScript channel.
1515
///
1616
/// The parameters `name` and `onMessageReceived` must not be null.
1717
JavascriptChannel({
@@ -24,7 +24,7 @@ class JavascriptChannel {
2424
/// The channel's name.
2525
///
2626
/// Passing this channel object as part of a [WebView.javascriptChannels] adds a channel object to
27-
/// the Javascript window object's property named `name`.
27+
/// the JavaScript window object's property named `name`.
2828
///
2929
/// The name must start with a letter or underscore(_), followed by any combination of those
3030
/// characters plus digits.

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 1.1.0
7+
version: 1.2.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void main() {
3030
case 'canGoBack':
3131
case 'canGoForward':
3232
return true;
33+
case 'runJavascriptReturningResult':
3334
case 'evaluateJavascript':
3435
return methodCall.arguments as String;
3536
case 'getScrollX':
@@ -266,16 +267,50 @@ void main() {
266267
test('evaluateJavascript', () async {
267268
final String evaluateJavascript =
268269
await webViewPlatform.evaluateJavascript(
269-
'This simulates some Javascript code.',
270+
'This simulates some JavaScript code.',
270271
);
271272

272-
expect('This simulates some Javascript code.', evaluateJavascript);
273+
expect('This simulates some JavaScript code.', evaluateJavascript);
273274
expect(
274275
log,
275276
<Matcher>[
276277
isMethodCall(
277278
'evaluateJavascript',
278-
arguments: 'This simulates some Javascript code.',
279+
arguments: 'This simulates some JavaScript code.',
280+
),
281+
],
282+
);
283+
});
284+
285+
test('runJavascript', () async {
286+
await webViewPlatform.runJavascript(
287+
'This simulates some JavaScript code.',
288+
);
289+
290+
expect(
291+
log,
292+
<Matcher>[
293+
isMethodCall(
294+
'runJavascript',
295+
arguments: 'This simulates some JavaScript code.',
296+
),
297+
],
298+
);
299+
});
300+
301+
test('runJavascriptReturningResult', () async {
302+
final String evaluateJavascript =
303+
await webViewPlatform.runJavascriptReturningResult(
304+
'This simulates some JavaScript code.',
305+
);
306+
307+
expect('This simulates some JavaScript code.', evaluateJavascript);
308+
expect(
309+
log,
310+
<Matcher>[
311+
isMethodCall(
312+
'runJavascriptReturningResult',
313+
arguments: 'This simulates some JavaScript code.',
279314
),
280315
],
281316
);

0 commit comments

Comments
 (0)