From e86264ca7b7f4cd51f63aa75802fc31d086bd262 Mon Sep 17 00:00:00 2001 From: lyllyl-bp Date: Wed, 12 Aug 2026 10:54:00 +0900 Subject: [PATCH] [webview_flutter] Add document-start JavaScript API Adds addDocumentStartJavaScript to WebViewController and the platform interface, returning a DocumentStartJavaScript handle that can be used to stop injecting the script into future document loads. Implements support on Android and WKWebView, including Android feature gating and WKWebView re-registration when user scripts are reset. Throws UnsupportedError on unsupported platforms (including web), and updates examples, documentation, changelogs, generated bindings, and tests --- .../webview_flutter/CHANGELOG.md | 6 + .../webview_flutter/webview_flutter/README.md | 21 ++ .../webview_flutter/example/lib/main.dart | 59 ++++ .../lib/src/webview_controller.dart | 40 +++ .../webview_flutter/lib/webview_flutter.dart | 1 + .../webview_flutter/pubspec.yaml | 8 +- .../test/webview_controller_test.dart | 40 ++- .../test/webview_controller_test.mocks.dart | 74 +++-- .../test/webview_widget_test.mocks.dart | 71 +++-- .../webview_flutter_android/CHANGELOG.md | 5 + .../webviewflutter/AndroidWebkitLibrary.g.kt | 122 ++++++++ .../webviewflutter/ProxyApiRegistrar.java | 6 + .../webviewflutter/ScriptHandlerProxyApi.java | 25 ++ .../webviewflutter/WebViewProxyApi.java | 16 + .../webviewflutter/ScriptHandlerTest.java | 23 ++ .../plugins/webviewflutter/WebViewTest.java | 25 ++ .../webview_flutter_test.dart | 48 +++ .../lib/src/android_webkit.g.dart | 110 +++++++ .../lib/src/android_webkit_constants.dart | 5 + .../lib/src/android_webview_controller.dart | 51 ++++ .../pigeons/android_webkit.dart | 13 + .../webview_flutter_android/pubspec.yaml | 4 +- .../test/android_webview_controller_test.dart | 63 ++++ ...android_webview_controller_test.mocks.dart | 273 ++++++++++++------ ...oid_webview_cookie_manager_test.mocks.dart | 33 ++- .../webview_android_widget_test.mocks.dart | 70 +++-- .../CHANGELOG.md | 4 +- .../lib/src/platform_webview_controller.dart | 35 +++ .../pubspec.yaml | 2 +- ...tform_document_start_java_script_test.dart | 26 ++ .../platform_webview_controller_test.dart | 8 + .../webview_flutter_web/CHANGELOG.md | 4 +- .../lib/src/web_webview_controller.dart | 11 + .../webview_flutter_web/pubspec.yaml | 4 +- .../test/web_webview_controller_test.dart | 9 + .../webview_flutter_wkwebview/CHANGELOG.md | 4 + .../webview_flutter_test.dart | 48 +++ .../lib/src/webkit_webview_controller.dart | 87 +++++- .../webview_flutter_wkwebview/pubspec.yaml | 4 +- .../test/webkit_webview_controller_test.dart | 125 ++++++++ 40 files changed, 1402 insertions(+), 181 deletions(-) create mode 100644 packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/ScriptHandlerProxyApi.java create mode 100644 packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/ScriptHandlerTest.java create mode 100644 packages/webview_flutter/webview_flutter_platform_interface/test/platform_document_start_java_script_test.dart diff --git a/packages/webview_flutter/webview_flutter/CHANGELOG.md b/packages/webview_flutter/webview_flutter/CHANGELOG.md index bae44ba8f51a..af796ab60073 100644 --- a/packages/webview_flutter/webview_flutter/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter/CHANGELOG.md @@ -1,3 +1,9 @@ +## 4.15.0 + +* Adds `WebViewController.addDocumentStartJavaScript` for injecting JavaScript + that runs at the start of future document loads. It returns a + `DocumentStartJavaScriptRegistration` that can be used to stop injecting the JavaScript. + ## 4.14.1 * Adds documentation for `NavigationDelegate` callback parameters. diff --git a/packages/webview_flutter/webview_flutter/README.md b/packages/webview_flutter/webview_flutter/README.md index d96e4443b050..cf7f9ed8ade9 100644 --- a/packages/webview_flutter/webview_flutter/README.md +++ b/packages/webview_flutter/webview_flutter/README.md @@ -61,6 +61,27 @@ See the Dartdocs for [WebViewController](https://pub.dev/documentation/webview_f and [WebViewWidget](https://pub.dev/documentation/webview_flutter/latest/webview_flutter/WebViewWidget-class.html) for more details. +### Running JavaScript at document start + +`WebViewController.addDocumentStartJavaScript` registers JavaScript that runs at the start of every +document loaded after the call, before the scripts of the loaded page run: + + +```dart +final DocumentStartJavaScriptRegistration documentStartJavaScriptRegistration = await webViewController + .addDocumentStartJavaScript( + 'window.exampleValue = "Hello from a document start script!";', + ); +``` + +The script runs in every frame of the document, including cross-origin `