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 6 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0

* Added `runJavascript` and `runJavascriptReturningResult` interface methods to supersede `evaluateJavaScript`.

## 1.0.0

* Extracted platform interface from `webview_flutter`.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,21 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
}

@override
Future<String> evaluateJavascript(String javascriptString) {
Future<String> evaluateJavascript(String javascript) {
return _channel
.invokeMethod<String>('evaluateJavascript', javascriptString)
.invokeMethod<String>('evaluateJavascript', javascript)
.then((result) => result!);
}

@override
Future<void> runJavascript(String javascript) async {
await _channel.invokeMethod<String>('runJavascript', javascript);
}

@override
Future<String> runJavascriptReturningResult(String javascript) {
return _channel
.invokeMethod<String>('runJavascriptReturningResult', javascript)
.then((result) => result!);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,38 @@ abstract class WebViewPlatformController {
"WebView clearCache is not implemented on the current platform");
}

/// Evaluates a JavaScript expression in the context of the current page.
/// Evaluates a Javascript expression in the context of the current page.
///
/// The Future completes with an error if a JavaScript error occurred, or if the type of the
/// evaluated expression is not supported(e.g on iOS not all non primitive type can be evaluated).
Future<String> evaluateJavascript(String javascriptString) {
/// The Future completes with an error if a Javascript error occurred, or if the type of the
/// evaluated expression is not supported (e.g on iOS not all non primitive types can be evaluated).
Future<String> evaluateJavascript(String javascript) {
throw UnimplementedError(
"WebView evaluateJavascript is not implemented on the current platform");
}

/// Adds new JavaScript channels to the set of enabled channels.
/// Runs the given Javascript in the context of the current page.
///
/// The Future completes with an error if a Javascript error occurred.
Future<void> runJavascript(String javascript) {
throw UnimplementedError(
"WebView runJavascript is not implemented on the current platform");
}

/// Runs the given Javascript in the context of the current page, and returns the result.
///
/// The Future completes with an error if a Javascript error occurred, or if the
/// type the given expression evaluates to is unsupported. Unsupported values include
/// certain non primitive types on iOS, as well as `undefined` or `null` on iOS 14+.
Future<String> runJavascriptReturningResult(String javascript) {
throw UnimplementedError(
"WebView runJavascriptReturningResult is not implemented on the current platform");
}

/// Adds new Javascript channels to the set of enabled channels.
///
/// For each value in this list the platform's webview should make sure that a corresponding
/// property with a postMessage method is set on `window`. For example for a JavaScript channel
/// named `Foo` it should be possible for JavaScript code executing in the webview to do
/// property with a postMessage method is set on `window`. For example for a Javascript channel
/// named `Foo` it should be possible for Javascript code executing in the webview to do
///
/// ```javascript
/// Foo.postMessage('hello');
Expand All @@ -128,9 +146,9 @@ abstract class WebViewPlatformController {
"WebView addJavascriptChannels is not implemented on the current platform");
}

/// Removes JavaScript channel names from the set of enabled channels.
/// Removes Javascript channel names from the set of enabled channels.
///
/// This disables channels that were previously enabled by [addJavaScriptChannels] or through
/// This disables channels that were previously enabled by [addJavascriptChannels] or through
/// [CreationParams.javascriptChannelNames].
Future<void> removeJavascriptChannels(Set<String> javascriptChannelNames) {
throw UnimplementedError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.0
version: 1.1.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void main() {
case 'canGoBack':
case 'canGoForward':
return true;
case 'runJavascriptReturningResult':
case 'evaluateJavascript':
return methodCall.arguments as String;
case 'getScrollX':
Expand Down Expand Up @@ -280,6 +281,40 @@ void main() {
);
});

test('runJavascript', () async {
await webViewPlatform.runJavascript(
'This simulates some Javascript code.',
);

expect(
log,
<Matcher>[
isMethodCall(
'runJavascript',
arguments: 'This simulates some Javascript code.',
),
],
);
});

test('runJavascriptReturningResult', () async {
final String evaluateJavascript =
await webViewPlatform.runJavascriptReturningResult(
'This simulates some Javascript code.',
);

expect('This simulates some Javascript code.', evaluateJavascript);
expect(
log,
<Matcher>[
isMethodCall(
'runJavascriptReturningResult',
arguments: 'This simulates some Javascript code.',
),
],
);
});

test('addJavascriptChannels', () async {
final Set<String> channels = <String>{'channel one', 'channel two'};
await webViewPlatform.addJavascriptChannels(channels);
Expand Down