Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d8f7484

Browse files
committedDec 1, 2024
fix #2197, Added useOnAjaxReadyStateChange and useOnAjaxProgress properties to InAppWebViewSettings, updated ajax and fetch webview events code docs
1 parent 4e4bbb5 commit d8f7484

File tree

33 files changed

+429
-129
lines changed

33 files changed

+429
-129
lines changed
 

‎flutter_inappwebview/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
- `flutter_inappwebview_macos`: `^1.2.0-beta.2` -> `^1.2.0-beta.3`
88
- `flutter_inappwebview_web`: `^1.2.0-beta.2` -> `^1.2.0-beta.3`
99
- `flutter_inappwebview_windows`: `^0.7.0-beta.2` -> `^0.7.0-beta.3`
10+
- Fixed "When useShouldInterceptAjaxRequest is true, some ajax requests doesn't work" [#2197](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2197)
1011

1112
#### Platform Interface
1213
- Added `saveState`, `restoreState` methods to `PlatformInAppWebViewController` class
14+
- Added `useOnAjaxReadyStateChange`, `useOnAjaxProgress` properties to `InAppWebViewSettings`
1315

1416
#### Android Platform
1517
- Implemented `saveState`, `restoreState` InAppWebViewController methods

‎flutter_inappwebview/lib/src/in_app_webview/headless_in_app_webview.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class HeadlessInAppWebView {
8686
@Deprecated('Use onNavigationResponse instead')
8787
FutureOr<IOSNavigationResponseAction?> Function(InAppWebViewController controller, IOSWKNavigationResponse navigationResponse)? iosOnNavigationResponse,
8888
@Deprecated('Use shouldAllowDeprecatedTLS instead') FutureOr<IOSShouldAllowDeprecatedTLSAction?> Function(InAppWebViewController controller, URLAuthenticationChallenge challenge)? iosShouldAllowDeprecatedTLS,
89-
FutureOr<AjaxRequestAction> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
89+
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
9090
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxReadyStateChange,
9191
void Function(InAppWebViewController controller, ConsoleMessage consoleMessage)? onConsoleMessage,
9292
FutureOr<bool?> Function(InAppWebViewController controller, CreateWindowAction createWindowAction)? onCreateWindow,

‎flutter_inappwebview/lib/src/in_app_webview/in_app_webview.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class InAppWebView extends StatefulWidget {
8686
@Deprecated('Use onNavigationResponse instead')
8787
FutureOr<IOSNavigationResponseAction?> Function(InAppWebViewController controller, IOSWKNavigationResponse navigationResponse)? iosOnNavigationResponse,
8888
@Deprecated('Use shouldAllowDeprecatedTLS instead') FutureOr<IOSShouldAllowDeprecatedTLSAction?> Function(InAppWebViewController controller, URLAuthenticationChallenge challenge)? iosShouldAllowDeprecatedTLS,
89-
FutureOr<AjaxRequestAction> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
89+
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
9090
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxReadyStateChange,
9191
void Function(InAppWebViewController controller, ConsoleMessage consoleMessage)? onConsoleMessage,
9292
FutureOr<bool?> Function(InAppWebViewController controller, CreateWindowAction createWindowAction)? onCreateWindow,

‎flutter_inappwebview_android/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Updated flutter_inappwebview_platform_interface version to ^1.4.0-beta.3
44
- Implemented `saveState`, `restoreState` InAppWebViewController methods
55
- Merged "Android: implemented PlatformPrintJobController.onComplete" [#2216](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2216) (thanks to [Doflatango](https://github.com/Doflatango))
6+
- Fixed "When useShouldInterceptAjaxRequest is true, some ajax requests doesn't work" [#2197](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2197)
67

78
## 1.2.0-beta.2
89

‎flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/plugin_scripts_js/InterceptAjaxRequestJS.java‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@
1010
public class InterceptAjaxRequestJS {
1111

1212
public static final String INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME = "IN_APP_WEBVIEW_INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT";
13+
1314
public static String FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
1415
return
1516
JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._useShouldInterceptAjaxRequest";
1617
}
18+
19+
public static String FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() {
20+
return JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._useOnAjaxReadyStateChange";
21+
}
22+
23+
public static String FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() {
24+
return JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._useOnAjaxProgress";
25+
}
26+
1727
public static String FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() {
1828
return
1929
JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._interceptOnlyAsyncAjaxRequests";
2030
}
31+
2132
public static PluginScript INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(@Nullable Set<String> allowedOriginRules,
22-
boolean forMainFrameOnly) {
33+
boolean forMainFrameOnly,
34+
boolean initialUseOnAjaxReadyStateChange,
35+
boolean initialUseOnAjaxProgress) {
2336
return
2437
new PluginScript(
2538
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
26-
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
39+
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_SOURCE(initialUseOnAjaxReadyStateChange, initialUseOnAjaxProgress),
2740
UserScriptInjectionTime.AT_DOCUMENT_START,
2841
null,
2942
true,
@@ -35,7 +48,7 @@ public static PluginScript INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(@Nullable Set
3548
public static PluginScript createInterceptOnlyAsyncAjaxRequestsPluginScript(boolean onlyAsync) {
3649
return new PluginScript(
3750
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
38-
"window." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() + " = " + onlyAsync +";",
51+
"window." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() + " = " + onlyAsync + ";",
3952
UserScriptInjectionTime.AT_DOCUMENT_START,
4053
null,
4154
true,
@@ -44,11 +57,13 @@ public static PluginScript createInterceptOnlyAsyncAjaxRequestsPluginScript(bool
4457
);
4558
}
4659

47-
public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
60+
public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE(boolean initialUseOnAjaxReadyStateChange, boolean initialUseOnAjaxProgress) {
4861
return
4962
"(function(ajax) {" +
5063
" var w = (window.top == null || window.top === window) ? window : window.top;" +
5164
" w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " = true;" +
65+
" w." + FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() + " = " + initialUseOnAjaxReadyStateChange + ";" +
66+
" w." + FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() + " = " + initialUseOnAjaxProgress + ";" +
5267
" var send = ajax.prototype.send;" +
5368
" var open = ajax.prototype.open;" +
5469
" var setRequestHeader = ajax.prototype.setRequestHeader;" +
@@ -98,8 +113,11 @@ public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
98113
" setRequestHeader.call(this, header, value);" +
99114
" };" +
100115
" function handleEvent(e) {" +
101-
" var self = this;" +
102116
" var w = (window.top == null || window.top === window) ? window : window.top;" +
117+
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " === false || w." + FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() + " == null || w." + FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() + " === false) {" +
118+
" return;" +
119+
" }" +
120+
" var self = this;" +
103121
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == true) {" +
104122
" var headers = this.getAllResponseHeaders();" +
105123
" var responseHeaders = {};" +
@@ -154,9 +172,9 @@ public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
154172
" var w = (window.top == null || window.top === window) ? window : window.top;" +
155173
" var canBeIntercepted = self._flutter_inappwebview_isAsync || w." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() + " === false;" +
156174
" if (canBeIntercepted && (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == true)) {" +
157-
" if (!this._flutter_inappwebview_already_onreadystatechange_wrapped) {" +
175+
" if (w." + FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() + " === true && !this._flutter_inappwebview_already_onreadystatechange_wrapped) {" +
158176
" this._flutter_inappwebview_already_onreadystatechange_wrapped = true;" +
159-
" var onreadystatechange = this.onreadystatechange;" +
177+
" var realOnreadystatechange = this.onreadystatechange;" +
160178
" this.onreadystatechange = function() {" +
161179
" var w = (window.top == null || window.top === window) ? window : window.top;" +
162180
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == true) {" +
@@ -198,13 +216,13 @@ public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
198216
" return;" +
199217
" };" +
200218
" }" +
201-
" if (onreadystatechange != null) {" +
202-
" onreadystatechange();" +
219+
" if (realOnreadystatechange != null) {" +
220+
" realOnreadystatechange();" +
203221
" }" +
204222
" });" +
205223
" });" +
206-
" } else if (onreadystatechange != null) {" +
207-
" onreadystatechange();" +
224+
" } else if (realOnreadystatechange != null) {" +
225+
" realOnreadystatechange();" +
208226
" }" +
209227
" };" +
210228
" }" +

‎flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebView.java‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,11 @@ public void prepareAndAddUserScripts() {
614614
interceptOnlyAsyncAjaxRequestsPluginScript = InterceptAjaxRequestJS.createInterceptOnlyAsyncAjaxRequestsPluginScript(customSettings.interceptOnlyAsyncAjaxRequests);
615615
if (customSettings.useShouldInterceptAjaxRequest) {
616616
userContentController.addPluginScript(interceptOnlyAsyncAjaxRequestsPluginScript);
617-
userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(customSettings.pluginScriptsOriginAllowList,
618-
customSettings.pluginScriptsForMainFrameOnly));
617+
userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(
618+
customSettings.pluginScriptsOriginAllowList,
619+
customSettings.pluginScriptsForMainFrameOnly,
620+
customSettings.useOnAjaxReadyStateChange,
621+
customSettings.useOnAjaxProgress));
619622
}
620623
if (customSettings.useShouldInterceptFetchRequest) {
621624
userContentController.addPluginScript(InterceptFetchRequestJS.INTERCEPT_FETCH_REQUEST_JS_PLUGIN_SCRIPT(customSettings.pluginScriptsOriginAllowList,
@@ -835,11 +838,22 @@ public void setSettings(InAppWebViewSettings newCustomSettings, HashMap<String,
835838
enablePluginScriptAtRuntime(
836839
InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
837840
newCustomSettings.useShouldInterceptAjaxRequest,
838-
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(customSettings.pluginScriptsOriginAllowList,
839-
customSettings.pluginScriptsForMainFrameOnly)
841+
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(
842+
customSettings.pluginScriptsOriginAllowList,
843+
customSettings.pluginScriptsForMainFrameOnly,
844+
newCustomSettings.useOnAjaxReadyStateChange,
845+
newCustomSettings.useOnAjaxProgress)
840846
);
841847
}
842848

849+
if (newSettingsMap.get("useOnAjaxReadyStateChange") != null && customSettings.useOnAjaxReadyStateChange != newCustomSettings.useOnAjaxReadyStateChange) {
850+
evaluateJavascript("((window.top == null || window.top === window) ? window : window.top)." + InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() + " = " + newCustomSettings.useOnAjaxReadyStateChange + ";", null);
851+
}
852+
853+
if (newSettingsMap.get("useOnAjaxProgress") != null && customSettings.useOnAjaxProgress != newCustomSettings.useOnAjaxProgress) {
854+
evaluateJavascript("((window.top == null || window.top === window) ? window : window.top)." + InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() + " = " + newCustomSettings.useOnAjaxProgress + ";", null);
855+
}
856+
843857
if (newSettingsMap.get("interceptOnlyAsyncAjaxRequests") != null && customSettings.interceptOnlyAsyncAjaxRequests != newCustomSettings.interceptOnlyAsyncAjaxRequests) {
844858
enablePluginScriptAtRuntime(
845859
InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE(),
@@ -1191,12 +1205,12 @@ public Map<String, Object> getCustomSettingsMap() {
11911205
public void enablePluginScriptAtRuntime(final String flagVariable,
11921206
final boolean enable,
11931207
final PluginScript pluginScript) {
1194-
evaluateJavascript("window." + flagVariable, null, new ValueCallback<String>() {
1208+
evaluateJavascript("((window.top == null || window.top === window) ? window : window.top)." + flagVariable, null, new ValueCallback<String>() {
11951209
@Override
11961210
public void onReceiveValue(String value) {
11971211
boolean alreadyLoaded = value != null && !value.equalsIgnoreCase("null");
11981212
if (alreadyLoaded) {
1199-
String enableSource = "window." + flagVariable + " = " + enable + ";";
1213+
String enableSource = "((window.top == null || window.top === window) ? window : window.top)." + flagVariable + " = " + enable + ";";
12001214
evaluateJavascript(enableSource, null, null);
12011215
if (!enable) {
12021216
userContentController.removePluginScript(pluginScript);

‎flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/in_app_webview/InAppWebViewSettings.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class InAppWebViewSettings implements ISettings<InAppWebViewInterface> {
4949
public List<Map<String, Map<String, Object>>> contentBlockers = new ArrayList<>();
5050
public Integer preferredContentMode = PreferredContentModeOptionType.RECOMMENDED.toValue();
5151
public Boolean useShouldInterceptAjaxRequest = false;
52+
public Boolean useOnAjaxReadyStateChange = false;
53+
public Boolean useOnAjaxProgress = false;
5254
public Boolean interceptOnlyAsyncAjaxRequests = true;
5355
public Boolean useShouldInterceptFetchRequest = false;
5456
public Boolean incognito = false;
@@ -219,6 +221,12 @@ public InAppWebViewSettings parse(@NonNull Map<String, Object> settings) {
219221
case "useShouldInterceptAjaxRequest":
220222
useShouldInterceptAjaxRequest = (Boolean) value;
221223
break;
224+
case "useOnAjaxReadyStateChange":
225+
useOnAjaxReadyStateChange = (Boolean) value;
226+
break;
227+
case "useOnAjaxProgress":
228+
useOnAjaxProgress = (Boolean) value;
229+
break;
222230
case "interceptOnlyAsyncAjaxRequests":
223231
interceptOnlyAsyncAjaxRequests = (Boolean) value;
224232
break;
@@ -497,6 +505,8 @@ public Map<String, Object> toMap() {
497505
settings.put("contentBlockers", contentBlockers);
498506
settings.put("preferredContentMode", preferredContentMode);
499507
settings.put("useShouldInterceptAjaxRequest", useShouldInterceptAjaxRequest);
508+
settings.put("useOnAjaxReadyStateChange", useOnAjaxReadyStateChange);
509+
settings.put("useOnAjaxProgress", useOnAjaxProgress);
500510
settings.put("interceptOnlyAsyncAjaxRequests", interceptOnlyAsyncAjaxRequests);
501511
settings.put("useShouldInterceptFetchRequest", useShouldInterceptFetchRequest);
502512
settings.put("incognito", incognito);

‎flutter_inappwebview_android/lib/src/in_app_webview/headless_in_app_webview.dart‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,19 @@ class AndroidHeadlessInAppWebView extends PlatformHeadlessInAppWebView
368368
settings.useOnDownloadStart == null) {
369369
settings.useOnDownloadStart = true;
370370
}
371-
if (params.shouldInterceptAjaxRequest != null &&
372-
settings.useShouldInterceptAjaxRequest == null) {
373-
settings.useShouldInterceptAjaxRequest = true;
371+
if ((params.shouldInterceptAjaxRequest != null ||
372+
params.onAjaxProgress != null ||
373+
params.onAjaxReadyStateChange != null)) {
374+
if (settings.useShouldInterceptAjaxRequest == null) {
375+
settings.useShouldInterceptAjaxRequest = true;
376+
}
377+
if (params.onAjaxReadyStateChange != null &&
378+
settings.useOnAjaxReadyStateChange == null) {
379+
settings.useOnAjaxReadyStateChange = true;
380+
}
381+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
382+
settings.useOnAjaxProgress = true;
383+
}
374384
}
375385
if (params.shouldInterceptFetchRequest != null &&
376386
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_android/lib/src/in_app_webview/in_app_webview.dart‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,18 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {
431431
settings.useOnDownloadStart = true;
432432
}
433433
if ((params.shouldInterceptAjaxRequest != null ||
434-
params.onAjaxProgress != null ||
435-
params.onAjaxReadyStateChange != null) &&
436-
settings.useShouldInterceptAjaxRequest == null) {
437-
settings.useShouldInterceptAjaxRequest = true;
434+
params.onAjaxProgress != null ||
435+
params.onAjaxReadyStateChange != null)) {
436+
if (settings.useShouldInterceptAjaxRequest == null) {
437+
settings.useShouldInterceptAjaxRequest = true;
438+
}
439+
if (params.onAjaxReadyStateChange != null &&
440+
settings.useOnAjaxReadyStateChange == null) {
441+
settings.useOnAjaxReadyStateChange = true;
442+
}
443+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
444+
settings.useOnAjaxProgress = true;
445+
}
438446
}
439447
if (params.shouldInterceptFetchRequest != null &&
440448
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_android/lib/src/in_app_webview/in_app_webview_controller.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,13 +1455,13 @@ class AndroidInAppWebViewController extends PlatformInAppWebViewController
14551455

14561456
if (webviewParams != null &&
14571457
webviewParams!.onAjaxReadyStateChange != null)
1458-
return (await webviewParams!.onAjaxReadyStateChange!(
1458+
return jsonEncode((await webviewParams!.onAjaxReadyStateChange!(
14591459
_controllerFromPlatform, request))
1460-
?.toNativeValue();
1460+
?.toNativeValue());
14611461
else
1462-
return (await _inAppBrowserEventHandler!
1462+
return jsonEncode((await _inAppBrowserEventHandler!
14631463
.onAjaxReadyStateChange(request))
1464-
?.toNativeValue();
1464+
?.toNativeValue());
14651465
}
14661466
return null;
14671467
case "onAjaxProgress":
@@ -1474,13 +1474,13 @@ class AndroidInAppWebViewController extends PlatformInAppWebViewController
14741474

14751475
if (webviewParams != null &&
14761476
webviewParams!.onAjaxProgress != null)
1477-
return (await webviewParams!.onAjaxProgress!(
1477+
return jsonEncode((await webviewParams!.onAjaxProgress!(
14781478
_controllerFromPlatform, request))
1479-
?.toNativeValue();
1479+
?.toNativeValue());
14801480
else
1481-
return (await _inAppBrowserEventHandler!
1481+
return jsonEncode((await _inAppBrowserEventHandler!
14821482
.onAjaxProgress(request))
1483-
?.toNativeValue();
1483+
?.toNativeValue());
14841484
}
14851485
return null;
14861486
case "shouldInterceptFetchRequest":

‎flutter_inappwebview_ios/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Merged "Add proxy support for iOS" [#2362](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2362) (thanks to [yerkejs](https://github.com/yerkejs))
77
- Fixed "[iOS] Webview opened with windowId does not receive javascript handler callback." [#2393](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2393)
88
- Fixed internal javascript callback handlers when the WebView has windowId not null
9+
- Fixed "When useShouldInterceptAjaxRequest is true, some ajax requests doesn't work" [#2197](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2197)
910

1011
## 1.2.0-beta.2
1112

‎flutter_inappwebview_ios/ios/Classes/InAppWebView/InAppWebView.swift‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,10 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
626626
if let interceptOnlyAsyncAjaxRequestsPluginScript = interceptOnlyAsyncAjaxRequestsPluginScript {
627627
configuration.userContentController.addPluginScript(interceptOnlyAsyncAjaxRequestsPluginScript)
628628
}
629-
configuration.userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: pluginScriptsOriginAllowList, forMainFrameOnly: pluginScriptsForMainFrameOnly))
629+
configuration.userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: pluginScriptsOriginAllowList,
630+
forMainFrameOnly: pluginScriptsForMainFrameOnly,
631+
initialUseOnAjaxReadyStateChange: settings.useOnAjaxReadyStateChange,
632+
initialUseOnAjaxProgress: settings.useOnAjaxProgress))
630633
}
631634
if settings.useShouldInterceptFetchRequest {
632635
configuration.userContentController.addPluginScript(InterceptFetchRequestJS.INTERCEPT_FETCH_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: pluginScriptsOriginAllowList, forMainFrameOnly: pluginScriptsForMainFrameOnly))
@@ -1137,13 +1140,35 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
11371140
enablePluginScriptAtRuntime(flagVariable: InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
11381141
enable: newSettings.useShouldInterceptAjaxRequest,
11391142
pluginScript: InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: newSettings.pluginScriptsOriginAllowList,
1140-
forMainFrameOnly: newSettings.pluginScriptsForMainFrameOnly))
1143+
forMainFrameOnly: newSettings.pluginScriptsForMainFrameOnly,
1144+
initialUseOnAjaxReadyStateChange: newSettings.useOnAjaxReadyStateChange,
1145+
initialUseOnAjaxProgress: newSettings.useOnAjaxProgress))
11411146
}
11421147
} else {
11431148
newSettings.useShouldInterceptAjaxRequest = false
11441149
}
11451150
}
11461151

1152+
if newSettingsMap["useOnAjaxReadyStateChange"] != nil && settings?.useOnAjaxReadyStateChange != newSettings.useOnAjaxReadyStateChange {
1153+
if let applePayAPIEnabled = settings?.applePayAPIEnabled, !applePayAPIEnabled {
1154+
if javaScriptBridgeEnabled {
1155+
evaluateJavaScript("\(InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE()) = \(newSettings.useOnAjaxReadyStateChange);")
1156+
}
1157+
} else {
1158+
newSettings.useOnAjaxReadyStateChange = false
1159+
}
1160+
}
1161+
1162+
if newSettingsMap["useOnAjaxProgress"] != nil && settings?.useOnAjaxProgress != newSettings.useOnAjaxProgress {
1163+
if let applePayAPIEnabled = settings?.applePayAPIEnabled, !applePayAPIEnabled {
1164+
if javaScriptBridgeEnabled {
1165+
evaluateJavaScript("\(InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) = \(newSettings.useOnAjaxProgress);")
1166+
}
1167+
} else {
1168+
newSettings.useOnAjaxProgress = false
1169+
}
1170+
}
1171+
11471172
if newSettingsMap["interceptOnlyAsyncAjaxRequests"] != nil && settings?.interceptOnlyAsyncAjaxRequests != newSettings.interceptOnlyAsyncAjaxRequests {
11481173
if let applePayAPIEnabled = settings?.applePayAPIEnabled, !applePayAPIEnabled,
11491174
let interceptOnlyAsyncAjaxRequestsPluginScript = interceptOnlyAsyncAjaxRequestsPluginScript {

‎flutter_inappwebview_ios/ios/Classes/InAppWebView/InAppWebViewSettings.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class InAppWebViewSettings: ISettings<InAppWebView> {
2727
var contentBlockers: [[String: [String : Any]]] = []
2828
var minimumFontSize = 0
2929
var useShouldInterceptAjaxRequest = false
30+
var useOnAjaxReadyStateChange = false
31+
var useOnAjaxProgress = false
3032
var interceptOnlyAsyncAjaxRequests = true
3133
var useShouldInterceptFetchRequest = false
3234
var incognito = false

‎flutter_inappwebview_ios/ios/Classes/PluginScriptsJS/InterceptAjaxRequestJS.swift‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ public class InterceptAjaxRequestJS {
1515
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._useShouldInterceptAjaxRequest"
1616
}
1717

18+
public static func FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() -> String {
19+
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._useOnAjaxReadyStateChange"
20+
}
21+
22+
public static func FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() -> String {
23+
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._useOnAjaxProgress"
24+
}
25+
1826
public static func FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() -> String {
1927
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._interceptOnlyAsyncAjaxRequests"
2028
}
2129

22-
public static func INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: [String]?, forMainFrameOnly: Bool) -> PluginScript {
30+
public static func INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: [String]?, forMainFrameOnly: Bool, initialUseOnAjaxReadyStateChange: Bool = false, initialUseOnAjaxProgress: Bool = false) -> PluginScript {
2331
return PluginScript(
2432
groupName: INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
25-
source: INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
33+
source: INTERCEPT_AJAX_REQUEST_JS_SOURCE(initialUseOnAjaxReadyStateChange: initialUseOnAjaxReadyStateChange, initialUseOnAjaxProgress: initialUseOnAjaxProgress),
2634
injectionTime: .atDocumentStart,
2735
forMainFrameOnly: forMainFrameOnly,
2836
allowedOriginRules: allowedOriginRules,
@@ -41,9 +49,11 @@ public class InterceptAjaxRequestJS {
4149
);
4250
}
4351

44-
public static func INTERCEPT_AJAX_REQUEST_JS_SOURCE() -> String {
52+
public static func INTERCEPT_AJAX_REQUEST_JS_SOURCE(initialUseOnAjaxReadyStateChange: Bool, initialUseOnAjaxProgress: Bool) -> String {
4553
return """
4654
\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) = true;
55+
\(FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE()) = \(initialUseOnAjaxReadyStateChange);
56+
\(FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) = \(initialUseOnAjaxProgress);
4757
(function(ajax) {
4858
var send = ajax.prototype.send;
4959
var open = ajax.prototype.open;
@@ -94,6 +104,9 @@ public class InterceptAjaxRequestJS {
94104
setRequestHeader.call(this, header, value);
95105
};
96106
function handleEvent(e) {
107+
if (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) === false || \(FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) == null || \(FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) === false) {
108+
return;
109+
}
97110
var self = this;
98111
if (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == null || \(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == true) {
99112
var headers = this.getAllResponseHeaders();
@@ -148,9 +161,9 @@ public class InterceptAjaxRequestJS {
148161
var self = this;
149162
var canBeIntercepted = self._flutter_inappwebview_isAsync || \(FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE()) === false;
150163
if (canBeIntercepted && (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == null || \(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == true)) {
151-
if (!this._flutter_inappwebview_already_onreadystatechange_wrapped) {
164+
if (\(FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE()) === true && !this._flutter_inappwebview_already_onreadystatechange_wrapped) {
152165
this._flutter_inappwebview_already_onreadystatechange_wrapped = true;
153-
var onreadystatechange = this.onreadystatechange;
166+
var realOnreadystatechange = this.onreadystatechange;
154167
this.onreadystatechange = function() {
155168
if (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == null || \(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == true) {
156169
var headers = this.getAllResponseHeaders();
@@ -191,13 +204,13 @@ public class InterceptAjaxRequestJS {
191204
return;
192205
};
193206
}
194-
if (onreadystatechange != null) {
195-
onreadystatechange();
207+
if (realOnreadystatechange != null) {
208+
realOnreadystatechange();
196209
}
197210
});
198211
});
199-
} else if (onreadystatechange != null) {
200-
onreadystatechange();
212+
} else if (realOnreadystatechange != null) {
213+
realOnreadystatechange();
201214
}
202215
};
203216
}

‎flutter_inappwebview_ios/lib/src/in_app_webview/headless_in_app_webview.dart‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,18 @@ class IOSHeadlessInAppWebView extends PlatformHeadlessInAppWebView
367367
settings.useOnDownloadStart == null) {
368368
settings.useOnDownloadStart = true;
369369
}
370-
if (params.shouldInterceptAjaxRequest != null &&
371-
settings.useShouldInterceptAjaxRequest == null) {
372-
settings.useShouldInterceptAjaxRequest = true;
370+
if ((params.shouldInterceptAjaxRequest != null ||
371+
params.onAjaxProgress != null ||
372+
params.onAjaxReadyStateChange != null)) {
373+
if (settings.useShouldInterceptAjaxRequest == null) {
374+
settings.useShouldInterceptAjaxRequest = true;
375+
}
376+
if (params.onAjaxReadyStateChange != null && settings.useOnAjaxReadyStateChange == null) {
377+
settings.useOnAjaxReadyStateChange = true;
378+
}
379+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
380+
settings.useOnAjaxProgress = true;
381+
}
373382
}
374383
if (params.shouldInterceptFetchRequest != null &&
375384
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_ios/lib/src/in_app_webview/in_app_webview.dart‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
33
import 'package:flutter/widgets.dart';
44
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
5-
import 'headless_in_app_webview.dart';
65

76
import '../find_interaction/find_interaction_controller.dart';
8-
import 'in_app_webview_controller.dart';
9-
import '../pull_to_refresh/main.dart';
107
import '../pull_to_refresh/pull_to_refresh_controller.dart';
8+
import 'headless_in_app_webview.dart';
9+
import 'in_app_webview_controller.dart';
1110

1211
/// Object specifying creation parameters for creating a [PlatformInAppWebViewWidget].
1312
///
@@ -372,10 +371,18 @@ class IOSInAppWebViewWidget extends PlatformInAppWebViewWidget {
372371
settings.useOnDownloadStart = true;
373372
}
374373
if ((params.shouldInterceptAjaxRequest != null ||
375-
params.onAjaxProgress != null ||
376-
params.onAjaxReadyStateChange != null) &&
377-
settings.useShouldInterceptAjaxRequest == null) {
378-
settings.useShouldInterceptAjaxRequest = true;
374+
params.onAjaxProgress != null ||
375+
params.onAjaxReadyStateChange != null)) {
376+
if (settings.useShouldInterceptAjaxRequest == null) {
377+
settings.useShouldInterceptAjaxRequest = true;
378+
}
379+
if (params.onAjaxReadyStateChange != null &&
380+
settings.useOnAjaxReadyStateChange == null) {
381+
settings.useOnAjaxReadyStateChange = true;
382+
}
383+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
384+
settings.useOnAjaxProgress = true;
385+
}
379386
}
380387
if (params.shouldInterceptFetchRequest != null &&
381388
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_ios/lib/src/in_app_webview/in_app_webview_controller.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,13 +1451,13 @@ class IOSInAppWebViewController extends PlatformInAppWebViewController
14511451

14521452
if (webviewParams != null &&
14531453
webviewParams!.onAjaxReadyStateChange != null)
1454-
return (await webviewParams!.onAjaxReadyStateChange!(
1454+
return jsonEncode((await webviewParams!.onAjaxReadyStateChange!(
14551455
_controllerFromPlatform, request))
1456-
?.toNativeValue();
1456+
?.toNativeValue());
14571457
else
1458-
return (await _inAppBrowserEventHandler!
1458+
return jsonEncode((await _inAppBrowserEventHandler!
14591459
.onAjaxReadyStateChange(request))
1460-
?.toNativeValue();
1460+
?.toNativeValue());
14611461
}
14621462
return null;
14631463
case "onAjaxProgress":
@@ -1470,13 +1470,13 @@ class IOSInAppWebViewController extends PlatformInAppWebViewController
14701470

14711471
if (webviewParams != null &&
14721472
webviewParams!.onAjaxProgress != null)
1473-
return (await webviewParams!.onAjaxProgress!(
1473+
return jsonEncode((await webviewParams!.onAjaxProgress!(
14741474
_controllerFromPlatform, request))
1475-
?.toNativeValue();
1475+
?.toNativeValue());
14761476
else
1477-
return (await _inAppBrowserEventHandler!
1477+
return jsonEncode((await _inAppBrowserEventHandler!
14781478
.onAjaxProgress(request))
1479-
?.toNativeValue();
1479+
?.toNativeValue());
14801480
}
14811481
return null;
14821482
case "shouldInterceptFetchRequest":

‎flutter_inappwebview_macos/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Implemented `PlatformProxyController` class
66
- Fixed internal javascript callback handlers when the WebView has windowId not null
77
- Fixed crash of unhandled `onPrintRequest` WebView event
8+
- Fixed "When useShouldInterceptAjaxRequest is true, some ajax requests doesn't work" [#2197](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2197)
89

910
## 1.2.0-beta.2
1011

‎flutter_inappwebview_macos/lib/src/in_app_webview/headless_in_app_webview.dart‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,19 @@ class MacOSHeadlessInAppWebView extends PlatformHeadlessInAppWebView
361361
settings.useOnDownloadStart == null) {
362362
settings.useOnDownloadStart = true;
363363
}
364-
if (params.shouldInterceptAjaxRequest != null &&
365-
settings.useShouldInterceptAjaxRequest == null) {
366-
settings.useShouldInterceptAjaxRequest = true;
364+
if ((params.shouldInterceptAjaxRequest != null ||
365+
params.onAjaxProgress != null ||
366+
params.onAjaxReadyStateChange != null)) {
367+
if (settings.useShouldInterceptAjaxRequest == null) {
368+
settings.useShouldInterceptAjaxRequest = true;
369+
}
370+
if (params.onAjaxReadyStateChange != null &&
371+
settings.useOnAjaxReadyStateChange == null) {
372+
settings.useOnAjaxReadyStateChange = true;
373+
}
374+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
375+
settings.useOnAjaxProgress = true;
376+
}
367377
}
368378
if (params.shouldInterceptFetchRequest != null &&
369379
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_macos/lib/src/in_app_webview/in_app_webview.dart‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,18 @@ class MacOSInAppWebViewWidget extends PlatformInAppWebViewWidget {
365365
settings.useOnDownloadStart = true;
366366
}
367367
if ((params.shouldInterceptAjaxRequest != null ||
368-
params.onAjaxProgress != null ||
369-
params.onAjaxReadyStateChange != null) &&
370-
settings.useShouldInterceptAjaxRequest == null) {
371-
settings.useShouldInterceptAjaxRequest = true;
368+
params.onAjaxProgress != null ||
369+
params.onAjaxReadyStateChange != null)) {
370+
if (settings.useShouldInterceptAjaxRequest == null) {
371+
settings.useShouldInterceptAjaxRequest = true;
372+
}
373+
if (params.onAjaxReadyStateChange != null &&
374+
settings.useOnAjaxReadyStateChange == null) {
375+
settings.useOnAjaxReadyStateChange = true;
376+
}
377+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
378+
settings.useOnAjaxProgress = true;
379+
}
372380
}
373381
if (params.shouldInterceptFetchRequest != null &&
374382
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_macos/lib/src/in_app_webview/in_app_webview_controller.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,13 @@ class MacOSInAppWebViewController extends PlatformInAppWebViewController
14581458

14591459
if (webviewParams != null &&
14601460
webviewParams!.onAjaxReadyStateChange != null)
1461-
return (await webviewParams!.onAjaxReadyStateChange!(
1461+
return jsonEncode((await webviewParams!.onAjaxReadyStateChange!(
14621462
_controllerFromPlatform, request))
1463-
?.toNativeValue();
1463+
?.toNativeValue());
14641464
else
1465-
return (await _inAppBrowserEventHandler!
1465+
return jsonEncode((await _inAppBrowserEventHandler!
14661466
.onAjaxReadyStateChange(request))
1467-
?.toNativeValue();
1467+
?.toNativeValue());
14681468
}
14691469
return null;
14701470
case "onAjaxProgress":
@@ -1477,13 +1477,13 @@ class MacOSInAppWebViewController extends PlatformInAppWebViewController
14771477

14781478
if (webviewParams != null &&
14791479
webviewParams!.onAjaxProgress != null)
1480-
return (await webviewParams!.onAjaxProgress!(
1480+
return jsonEncode((await webviewParams!.onAjaxProgress!(
14811481
_controllerFromPlatform, request))
1482-
?.toNativeValue();
1482+
?.toNativeValue());
14831483
else
1484-
return (await _inAppBrowserEventHandler!
1484+
return jsonEncode((await _inAppBrowserEventHandler!
14851485
.onAjaxProgress(request))
1486-
?.toNativeValue();
1486+
?.toNativeValue());
14871487
}
14881488
return null;
14891489
case "shouldInterceptFetchRequest":

‎flutter_inappwebview_macos/macos/Classes/InAppWebView/InAppWebView.swift‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ public class InAppWebView: WKWebView, WKUIDelegate,
237237
if let interceptOnlyAsyncAjaxRequestsPluginScript = interceptOnlyAsyncAjaxRequestsPluginScript {
238238
configuration.userContentController.addPluginScript(interceptOnlyAsyncAjaxRequestsPluginScript)
239239
}
240-
configuration.userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: pluginScriptsOriginAllowList, forMainFrameOnly: pluginScriptsForMainFrameOnly))
240+
configuration.userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: pluginScriptsOriginAllowList,
241+
forMainFrameOnly: pluginScriptsForMainFrameOnly,
242+
initialUseOnAjaxReadyStateChange: settings.useOnAjaxReadyStateChange,
243+
initialUseOnAjaxProgress: settings.useOnAjaxProgress))
241244
}
242245
if settings.useShouldInterceptFetchRequest {
243246
configuration.userContentController.addPluginScript(InterceptFetchRequestJS.INTERCEPT_FETCH_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: pluginScriptsOriginAllowList, forMainFrameOnly: pluginScriptsForMainFrameOnly))
@@ -632,13 +635,35 @@ public class InAppWebView: WKWebView, WKUIDelegate,
632635
enablePluginScriptAtRuntime(flagVariable: InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
633636
enable: newSettings.useShouldInterceptAjaxRequest,
634637
pluginScript: InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: newSettings.pluginScriptsOriginAllowList,
635-
forMainFrameOnly: newSettings.pluginScriptsForMainFrameOnly))
638+
forMainFrameOnly: newSettings.pluginScriptsForMainFrameOnly,
639+
initialUseOnAjaxReadyStateChange: newSettings.useOnAjaxReadyStateChange,
640+
initialUseOnAjaxProgress: newSettings.useOnAjaxProgress))
636641
}
637642
} else {
638643
newSettings.useShouldInterceptAjaxRequest = false
639644
}
640645
}
641646

647+
if newSettingsMap["useOnAjaxReadyStateChange"] != nil && settings?.useOnAjaxReadyStateChange != newSettings.useOnAjaxReadyStateChange {
648+
if let applePayAPIEnabled = settings?.applePayAPIEnabled, !applePayAPIEnabled {
649+
if javaScriptBridgeEnabled {
650+
evaluateJavaScript("\(InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE()) = \(newSettings.useOnAjaxReadyStateChange);")
651+
}
652+
} else {
653+
newSettings.useOnAjaxReadyStateChange = false
654+
}
655+
}
656+
657+
if newSettingsMap["useOnAjaxProgress"] != nil && settings?.useOnAjaxProgress != newSettings.useOnAjaxProgress {
658+
if let applePayAPIEnabled = settings?.applePayAPIEnabled, !applePayAPIEnabled {
659+
if javaScriptBridgeEnabled {
660+
evaluateJavaScript("\(InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) = \(newSettings.useOnAjaxProgress);")
661+
}
662+
} else {
663+
newSettings.useOnAjaxProgress = false
664+
}
665+
}
666+
642667
if newSettingsMap["interceptOnlyAsyncAjaxRequests"] != nil && settings?.interceptOnlyAsyncAjaxRequests != newSettings.interceptOnlyAsyncAjaxRequests {
643668
if let applePayAPIEnabled = settings?.applePayAPIEnabled, !applePayAPIEnabled,
644669
let interceptOnlyAsyncAjaxRequestsPluginScript = interceptOnlyAsyncAjaxRequestsPluginScript {

‎flutter_inappwebview_macos/macos/Classes/InAppWebView/InAppWebViewSettings.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class InAppWebViewSettings: ISettings<InAppWebView> {
2525
var contentBlockers: [[String: [String : Any]]] = []
2626
var minimumFontSize = 0
2727
var useShouldInterceptAjaxRequest = false
28+
var useOnAjaxReadyStateChange = false
29+
var useOnAjaxProgress = false
2830
var interceptOnlyAsyncAjaxRequests = true
2931
var useShouldInterceptFetchRequest = false
3032
var incognito = false

‎flutter_inappwebview_macos/macos/Classes/PluginScriptsJS/InterceptAjaxRequestJS.swift‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ public class InterceptAjaxRequestJS {
1515
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._useShouldInterceptAjaxRequest"
1616
}
1717

18+
public static func FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() -> String {
19+
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._useOnAjaxReadyStateChange"
20+
}
21+
22+
public static func FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() -> String {
23+
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._useOnAjaxProgress"
24+
}
25+
1826
public static func FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() -> String {
1927
return "window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())._interceptOnlyAsyncAjaxRequests"
2028
}
2129

22-
public static func INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: [String]?, forMainFrameOnly: Bool) -> PluginScript {
30+
public static func INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(allowedOriginRules: [String]?, forMainFrameOnly: Bool, initialUseOnAjaxReadyStateChange: Bool = false, initialUseOnAjaxProgress: Bool = false) -> PluginScript {
2331
return PluginScript(
2432
groupName: INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
25-
source: INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
33+
source: INTERCEPT_AJAX_REQUEST_JS_SOURCE(initialUseOnAjaxReadyStateChange: initialUseOnAjaxReadyStateChange, initialUseOnAjaxProgress: initialUseOnAjaxProgress),
2634
injectionTime: .atDocumentStart,
2735
forMainFrameOnly: forMainFrameOnly,
2836
allowedOriginRules: allowedOriginRules,
@@ -41,9 +49,11 @@ public class InterceptAjaxRequestJS {
4149
);
4250
}
4351

44-
public static func INTERCEPT_AJAX_REQUEST_JS_SOURCE() -> String {
52+
public static func INTERCEPT_AJAX_REQUEST_JS_SOURCE(initialUseOnAjaxReadyStateChange: Bool, initialUseOnAjaxProgress: Bool) -> String {
4553
return """
4654
\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) = true;
55+
\(FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE()) = \(initialUseOnAjaxReadyStateChange);
56+
\(FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) = \(initialUseOnAjaxProgress);
4757
(function(ajax) {
4858
var send = ajax.prototype.send;
4959
var open = ajax.prototype.open;
@@ -94,6 +104,9 @@ public class InterceptAjaxRequestJS {
94104
setRequestHeader.call(this, header, value);
95105
};
96106
function handleEvent(e) {
107+
if (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) === false || \(FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) == null || \(FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS()) === false) {
108+
return;
109+
}
97110
var self = this;
98111
if (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == null || \(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == true) {
99112
var headers = this.getAllResponseHeaders();
@@ -148,9 +161,9 @@ public class InterceptAjaxRequestJS {
148161
var self = this;
149162
var canBeIntercepted = self._flutter_inappwebview_isAsync || \(FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE()) === false;
150163
if (canBeIntercepted && (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == null || \(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == true)) {
151-
if (!this._flutter_inappwebview_already_onreadystatechange_wrapped) {
164+
if (\(FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE()) === true && !this._flutter_inappwebview_already_onreadystatechange_wrapped) {
152165
this._flutter_inappwebview_already_onreadystatechange_wrapped = true;
153-
var onreadystatechange = this.onreadystatechange;
166+
var realOnreadystatechange = this.onreadystatechange;
154167
this.onreadystatechange = function() {
155168
if (\(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == null || \(FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE()) == true) {
156169
var headers = this.getAllResponseHeaders();
@@ -191,13 +204,13 @@ public class InterceptAjaxRequestJS {
191204
return;
192205
};
193206
}
194-
if (onreadystatechange != null) {
195-
onreadystatechange();
207+
if (realOnreadystatechange != null) {
208+
realOnreadystatechange();
196209
}
197210
});
198211
});
199-
} else if (onreadystatechange != null) {
200-
onreadystatechange();
212+
} else if (realOnreadystatechange != null) {
213+
realOnreadystatechange();
201214
}
202215
};
203216
}

‎flutter_inappwebview_platform_interface/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 1.4.0-beta.3
22

33
- Added `saveState`, `restoreState` methods to `PlatformInAppWebViewController` class
4+
- Added `useOnAjaxReadyStateChange`, `useOnAjaxProgress` properties to `InAppWebViewSettings`
45

56
## 1.4.0-beta.2
67

‎flutter_inappwebview_platform_interface/lib/src/in_app_browser/platform_in_app_browser.dart‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'in_app_browser_menu_item.dart';
2424
import 'in_app_browser_settings.dart';
2525
import '../debug_logging_settings.dart';
2626
import '../pull_to_refresh/platform_pull_to_refresh_controller.dart';
27+
import '../platform_webview_feature.dart';
2728

2829
/// Object specifying creation parameters for creating a [PlatformInAppBrowser].
2930
///
@@ -873,11 +874,17 @@ abstract class PlatformInAppBrowserEvents {
873874

874875
///Event fired when an `XMLHttpRequest` is sent to a server.
875876
///It gives the host application a chance to take control over the request before sending it.
877+
///This event is implemented using JavaScript under the hood.
878+
///
879+
///Due to the async nature of this event implementation, it will intercept only async `XMLHttpRequest`s ([AjaxRequest.isAsync] with `true`).
880+
///To be able to intercept sync `XMLHttpRequest`s, use [InAppWebViewSettings.interceptOnlyAsyncAjaxRequests] to `false`.
881+
///If necessary, you should implement your own logic using for example an [UserScript] overriding the
882+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
876883
///
877884
///[ajaxRequest] represents the `XMLHttpRequest`.
878885
///
879886
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] setting to `true`.
880-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
887+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
881888
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
882889
///used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
883890
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the ajax requests will be intercept for sure.
@@ -892,12 +899,18 @@ abstract class PlatformInAppBrowserEvents {
892899

893900
///Event fired whenever the `readyState` attribute of an `XMLHttpRequest` changes.
894901
///It gives the host application a chance to abort the request.
902+
///This event is implemented using JavaScript under the hood.
903+
///
904+
///Due to the async nature of this event implementation,
905+
///using it could cause some issues, so, be careful when using it.
906+
///In this case, you should implement your own logic using for example an [UserScript] overriding the
907+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
895908
///
896909
///[ajaxRequest] represents the [XMLHttpRequest].
897910
///
898-
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] setting to `true`.
899-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
900-
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
911+
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] and [InAppWebViewSettings.useOnAjaxReadyStateChange] settings to `true`.
912+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
913+
///can inject javascript code right after the document element is created but before any other content is loaded, the javascript code
901914
///used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
902915
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the ajax requests will be intercept for sure.
903916
///
@@ -912,11 +925,12 @@ abstract class PlatformInAppBrowserEvents {
912925

913926
///Event fired as an `XMLHttpRequest` progress.
914927
///It gives the host application a chance to abort the request.
928+
///This event is implemented using JavaScript under the hood.
915929
///
916930
///[ajaxRequest] represents the [XMLHttpRequest].
917931
///
918-
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] setting to `true`.
919-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
932+
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] and [InAppWebViewSettings.useOnAjaxProgress] settings to `true`.
933+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
920934
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
921935
///used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
922936
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the ajax requests will be intercept for sure.
@@ -931,11 +945,12 @@ abstract class PlatformInAppBrowserEvents {
931945

932946
///Event fired when a request is sent to a server through [Fetch API](https://developer.mozilla.org/it/docs/Web/API/Fetch_API).
933947
///It gives the host application a chance to take control over the request before sending it.
948+
///This event is implemented using JavaScript under the hood.
934949
///
935950
///[fetchRequest] represents a resource request.
936951
///
937952
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptFetchRequest] setting to `true`.
938-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
953+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
939954
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
940955
///used to intercept fetch requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
941956
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the fetch requests will be intercept for sure.

‎flutter_inappwebview_platform_interface/lib/src/in_app_webview/in_app_webview_settings.dart‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,46 @@ class InAppWebViewSettings_ {
264264
///Due to the async nature of [PlatformWebViewCreationParams.shouldInterceptAjaxRequest] event implementation,
265265
///it will intercept only async `XMLHttpRequest`s ([AjaxRequest.isAsync] with `true`).
266266
///To be able to intercept sync `XMLHttpRequest`s, use [InAppWebViewSettings.interceptOnlyAsyncAjaxRequests] to `false`.
267+
///If necessary, you should implement your own logic using for example an [UserScript] overriding the
268+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
267269
///
268-
///If the [PlatformWebViewCreationParams.shouldInterceptAjaxRequest] event or
269-
///any other Ajax event is implemented and this value is `null`,
270+
///If the [PlatformWebViewCreationParams.shouldInterceptAjaxRequest] event is implemented and this value is `null`,
270271
///it will be automatically inferred as `true`, otherwise, the default value is `false`.
271272
///This logic will not be applied for [PlatformInAppBrowser], where you must set the value manually.
272273
@SupportedPlatforms(
273274
platforms: [AndroidPlatform(), IOSPlatform(), MacOSPlatform()])
274275
bool? useShouldInterceptAjaxRequest;
275276

277+
///Set to `true` to be able to listen at the [PlatformWebViewCreationParams.onAjaxReadyStateChange] event.
278+
///Also, [useShouldInterceptAjaxRequest] must be set to `true` to take effect.
279+
///
280+
///Due to the async nature of [PlatformWebViewCreationParams.onAjaxReadyStateChange] event implementation,
281+
///using it could cause some issues, so, be careful when using it.
282+
///In this case, you should implement your own logic using for example an [UserScript] overriding the
283+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
284+
///
285+
///If the [PlatformWebViewCreationParams.onAjaxReadyStateChange] event is implemented and this value is `null`,
286+
///it will be automatically inferred as `true`, otherwise, the default value is `false`.
287+
///This logic will not be applied for [PlatformInAppBrowser], where you must set the value manually.
288+
@SupportedPlatforms(
289+
platforms: [AndroidPlatform(), IOSPlatform(), MacOSPlatform()])
290+
bool? useOnAjaxReadyStateChange;
291+
292+
///Set to `true` to be able to listen at the [PlatformWebViewCreationParams.onAjaxProgress] event.
293+
///Also, [useShouldInterceptAjaxRequest] must be set to `true` to take effect.
294+
///
295+
///Due to the async nature of [PlatformWebViewCreationParams.onAjaxProgress] event implementation,
296+
///using it could cause some issues, so, be careful when using it.
297+
///In this case, you should implement your own logic using for example an [UserScript] overriding the
298+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
299+
///
300+
///If the [PlatformWebViewCreationParams.onAjaxProgress] event is implemented and this value is `null`,
301+
///it will be automatically inferred as `true`, otherwise, the default value is `false`.
302+
///This logic will not be applied for [PlatformInAppBrowser], where you must set the value manually.
303+
@SupportedPlatforms(
304+
platforms: [AndroidPlatform(), IOSPlatform(), MacOSPlatform()])
305+
bool? useOnAjaxProgress;
306+
276307
///Set to `false` to be able to listen to also sync `XMLHttpRequest`s at the
277308
///[PlatformWebViewCreationParams.shouldInterceptAjaxRequest] event.
278309
///
@@ -2078,6 +2109,8 @@ as it can cause framerate drops on animations in Android 9 and lower (see [Hybri
20782109
this.contentBlockers = const [],
20792110
this.preferredContentMode = UserPreferredContentMode_.RECOMMENDED,
20802111
this.useShouldInterceptAjaxRequest,
2112+
this.useOnAjaxReadyStateChange,
2113+
this.useOnAjaxProgress,
20812114
this.interceptOnlyAsyncAjaxRequests = true,
20822115
this.useShouldInterceptFetchRequest,
20832116
this.incognito = false,

‎flutter_inappwebview_platform_interface/lib/src/in_app_webview/in_app_webview_settings.g.dart‎

Lines changed: 46 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flutter_inappwebview_platform_interface/lib/src/in_app_webview/platform_webview.dart‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'platform_inappwebview_controller.dart';
1313
import '../print_job/main.dart';
1414
import 'platform_inappwebview_widget.dart';
1515
import 'platform_headless_in_app_webview.dart';
16+
import '../platform_webview_feature.dart';
1617

1718
///{@template flutter_inappwebview_platform_interface.PlatformWebViewCreationParams}
1819
///Class that represents a WebView. Used by [PlatformInAppWebViewWidget],
@@ -428,14 +429,17 @@ class PlatformWebViewCreationParams<T> {
428429
///{@template flutter_inappwebview_platform_interface.PlatformWebViewCreationParams.shouldInterceptAjaxRequest}
429430
///Event fired when an `XMLHttpRequest` is sent to a server.
430431
///It gives the host application a chance to take control over the request before sending it.
432+
///This event is implemented using JavaScript under the hood.
431433
///
432434
///Due to the async nature of this event implementation, it will intercept only async `XMLHttpRequest`s ([AjaxRequest.isAsync] with `true`).
433435
///To be able to intercept sync `XMLHttpRequest`s, use [InAppWebViewSettings.interceptOnlyAsyncAjaxRequests] to `false`.
436+
///If necessary, you should implement your own logic using for example an [UserScript] overriding the
437+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
434438
///
435439
///[ajaxRequest] represents the `XMLHttpRequest`.
436440
///
437441
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] setting to `true`.
438-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
442+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
439443
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
440444
///used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
441445
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the ajax requests will be intercept for sure.
@@ -451,12 +455,18 @@ class PlatformWebViewCreationParams<T> {
451455
///{@template flutter_inappwebview_platform_interface.PlatformWebViewCreationParams.onAjaxReadyStateChange}
452456
///Event fired whenever the `readyState` attribute of an `XMLHttpRequest` changes.
453457
///It gives the host application a chance to abort the request.
458+
///This event is implemented using JavaScript under the hood.
459+
///
460+
///Due to the async nature of this event implementation,
461+
///using it could cause some issues, so, be careful when using it.
462+
///In this case, you should implement your own logic using for example an [UserScript] overriding the
463+
///[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) JavaScript object.
454464
///
455465
///[ajaxRequest] represents the [XMLHttpRequest].
456466
///
457-
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] setting to `true`.
458-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
459-
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
467+
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] and [InAppWebViewSettings.useOnAjaxReadyStateChange] settings to `true`.
468+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
469+
///can inject javascript code right after the document element is created but before any other content is loaded, the javascript code
460470
///used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
461471
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the ajax requests will be intercept for sure.
462472
///
@@ -471,11 +481,12 @@ class PlatformWebViewCreationParams<T> {
471481
///{@template flutter_inappwebview_platform_interface.PlatformWebViewCreationParams.onAjaxProgress}
472482
///Event fired as an `XMLHttpRequest` progress.
473483
///It gives the host application a chance to abort the request.
484+
///This event is implemented using JavaScript under the hood.
474485
///
475486
///[ajaxRequest] represents the [XMLHttpRequest].
476487
///
477-
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] setting to `true`.
478-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
488+
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptAjaxRequest] and [InAppWebViewSettings.useOnAjaxProgress] settings to `true`.
489+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
479490
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
480491
///used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
481492
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the ajax requests will be intercept for sure.
@@ -491,11 +502,12 @@ class PlatformWebViewCreationParams<T> {
491502
///{@template flutter_inappwebview_platform_interface.PlatformWebViewCreationParams.shouldInterceptFetchRequest}
492503
///Event fired when a request is sent to a server through [Fetch API](https://developer.mozilla.org/it/docs/Web/API/Fetch_API).
493504
///It gives the host application a chance to take control over the request before sending it.
505+
///This event is implemented using JavaScript under the hood.
494506
///
495507
///[fetchRequest] represents a resource request.
496508
///
497509
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewSettings.useShouldInterceptFetchRequest] setting to `true`.
498-
///Also, unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
510+
///Also, on Android that doesn't support the [WebViewFeature.DOCUMENT_START_SCRIPT], unlike iOS that has [WKUserScript](https://developer.apple.com/documentation/webkit/wkuserscript) that
499511
///can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code
500512
///used to intercept fetch requests is loaded as soon as possible so it won't be instantaneous as iOS but just after some milliseconds (< ~100ms).
501513
///Inside the `window.addEventListener("flutterInAppWebViewPlatformReady")` event, the fetch requests will be intercept for sure.

‎flutter_inappwebview_windows/example/pubspec.lock‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,17 @@ packages:
8686
flutter_inappwebview_platform_interface:
8787
dependency: transitive
8888
description:
89-
name: flutter_inappwebview_platform_interface
90-
sha256: "2c99bf767900ba029d825bc6f494d30169ee83cdaa038d86e85fe70571d0a655"
91-
url: "https://pub.dev"
92-
source: hosted
93-
version: "1.4.0-beta.2"
89+
path: "../../flutter_inappwebview_platform_interface"
90+
relative: true
91+
source: path
92+
version: "1.4.0-beta.3"
9493
flutter_inappwebview_windows:
9594
dependency: "direct main"
9695
description:
9796
path: ".."
9897
relative: true
9998
source: path
100-
version: "0.7.0-beta.2"
99+
version: "0.7.0-beta.3"
101100
flutter_lints:
102101
dependency: "direct dev"
103102
description:

‎flutter_inappwebview_windows/lib/src/in_app_webview/headless_in_app_webview.dart‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,19 @@ class WindowsHeadlessInAppWebView extends PlatformHeadlessInAppWebView
376376
settings.useOnDownloadStart == null) {
377377
settings.useOnDownloadStart = true;
378378
}
379-
if (params.shouldInterceptAjaxRequest != null &&
380-
settings.useShouldInterceptAjaxRequest == null) {
381-
settings.useShouldInterceptAjaxRequest = true;
379+
if ((params.shouldInterceptAjaxRequest != null ||
380+
params.onAjaxProgress != null ||
381+
params.onAjaxReadyStateChange != null)) {
382+
if (settings.useShouldInterceptAjaxRequest == null) {
383+
settings.useShouldInterceptAjaxRequest = true;
384+
}
385+
if (params.onAjaxReadyStateChange != null &&
386+
settings.useOnAjaxReadyStateChange == null) {
387+
settings.useOnAjaxReadyStateChange = true;
388+
}
389+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
390+
settings.useOnAjaxProgress = true;
391+
}
382392
}
383393
if (params.shouldInterceptFetchRequest != null &&
384394
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_windows/lib/src/in_app_webview/in_app_webview.dart‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,18 @@ class WindowsInAppWebViewWidget extends PlatformInAppWebViewWidget {
369369
settings.useOnDownloadStart = true;
370370
}
371371
if ((params.shouldInterceptAjaxRequest != null ||
372-
params.onAjaxProgress != null ||
373-
params.onAjaxReadyStateChange != null) &&
374-
settings.useShouldInterceptAjaxRequest == null) {
375-
settings.useShouldInterceptAjaxRequest = true;
372+
params.onAjaxProgress != null ||
373+
params.onAjaxReadyStateChange != null)) {
374+
if (settings.useShouldInterceptAjaxRequest == null) {
375+
settings.useShouldInterceptAjaxRequest = true;
376+
}
377+
if (params.onAjaxReadyStateChange != null &&
378+
settings.useOnAjaxReadyStateChange == null) {
379+
settings.useOnAjaxReadyStateChange = true;
380+
}
381+
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
382+
settings.useOnAjaxProgress = true;
383+
}
376384
}
377385
if (params.shouldInterceptFetchRequest != null &&
378386
settings.useShouldInterceptFetchRequest == null) {

‎flutter_inappwebview_windows/lib/src/in_app_webview/in_app_webview_controller.dart‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,13 +1536,13 @@ class WindowsInAppWebViewController extends PlatformInAppWebViewController
15361536

15371537
if (webviewParams != null &&
15381538
webviewParams!.onAjaxReadyStateChange != null)
1539-
return (await webviewParams!.onAjaxReadyStateChange!(
1539+
return jsonEncode((await webviewParams!.onAjaxReadyStateChange!(
15401540
_controllerFromPlatform, request))
1541-
?.toNativeValue();
1541+
?.toNativeValue());
15421542
else
1543-
return (await _inAppBrowserEventHandler!
1543+
return jsonEncode((await _inAppBrowserEventHandler!
15441544
.onAjaxReadyStateChange(request))
1545-
?.toNativeValue();
1545+
?.toNativeValue());
15461546
}
15471547
return null;
15481548
case "onAjaxProgress":
@@ -1555,13 +1555,13 @@ class WindowsInAppWebViewController extends PlatformInAppWebViewController
15551555

15561556
if (webviewParams != null &&
15571557
webviewParams!.onAjaxProgress != null)
1558-
return (await webviewParams!.onAjaxProgress!(
1558+
return jsonEncode((await webviewParams!.onAjaxProgress!(
15591559
_controllerFromPlatform, request))
1560-
?.toNativeValue();
1560+
?.toNativeValue());
15611561
else
1562-
return (await _inAppBrowserEventHandler!
1562+
return jsonEncode((await _inAppBrowserEventHandler!
15631563
.onAjaxProgress(request))
1564-
?.toNativeValue();
1564+
?.toNativeValue());
15651565
}
15661566
return null;
15671567
case "shouldInterceptFetchRequest":

0 commit comments

Comments
 (0)
Please sign in to comment.