Skip to content

Commit

Permalink
fix #2197, Added useOnAjaxReadyStateChange and useOnAjaxProgress prop…
Browse files Browse the repository at this point in the history
…erties to InAppWebViewSettings, updated ajax and fetch webview events code docs
  • Loading branch information
pichillilorenzo committed Dec 1, 2024
1 parent 4e4bbb5 commit d8f7484
Show file tree
Hide file tree
Showing 33 changed files with 429 additions and 129 deletions.
2 changes: 2 additions & 0 deletions flutter_inappwebview/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
- `flutter_inappwebview_macos`: `^1.2.0-beta.2` -> `^1.2.0-beta.3`
- `flutter_inappwebview_web`: `^1.2.0-beta.2` -> `^1.2.0-beta.3`
- `flutter_inappwebview_windows`: `^0.7.0-beta.2` -> `^0.7.0-beta.3`
- Fixed "When useShouldInterceptAjaxRequest is true, some ajax requests doesn't work" [#2197](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2197)

#### Platform Interface
- Added `saveState`, `restoreState` methods to `PlatformInAppWebViewController` class
- Added `useOnAjaxReadyStateChange`, `useOnAjaxProgress` properties to `InAppWebViewSettings`

#### Android Platform
- Implemented `saveState`, `restoreState` InAppWebViewController methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class HeadlessInAppWebView {
@Deprecated('Use onNavigationResponse instead')
FutureOr<IOSNavigationResponseAction?> Function(InAppWebViewController controller, IOSWKNavigationResponse navigationResponse)? iosOnNavigationResponse,
@Deprecated('Use shouldAllowDeprecatedTLS instead') FutureOr<IOSShouldAllowDeprecatedTLSAction?> Function(InAppWebViewController controller, URLAuthenticationChallenge challenge)? iosShouldAllowDeprecatedTLS,
FutureOr<AjaxRequestAction> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxReadyStateChange,
void Function(InAppWebViewController controller, ConsoleMessage consoleMessage)? onConsoleMessage,
FutureOr<bool?> Function(InAppWebViewController controller, CreateWindowAction createWindowAction)? onCreateWindow,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class InAppWebView extends StatefulWidget {
@Deprecated('Use onNavigationResponse instead')
FutureOr<IOSNavigationResponseAction?> Function(InAppWebViewController controller, IOSWKNavigationResponse navigationResponse)? iosOnNavigationResponse,
@Deprecated('Use shouldAllowDeprecatedTLS instead') FutureOr<IOSShouldAllowDeprecatedTLSAction?> Function(InAppWebViewController controller, URLAuthenticationChallenge challenge)? iosShouldAllowDeprecatedTLS,
FutureOr<AjaxRequestAction> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxProgress,
FutureOr<AjaxRequestAction?> Function(InAppWebViewController controller, AjaxRequest ajaxRequest)? onAjaxReadyStateChange,
void Function(InAppWebViewController controller, ConsoleMessage consoleMessage)? onConsoleMessage,
FutureOr<bool?> Function(InAppWebViewController controller, CreateWindowAction createWindowAction)? onCreateWindow,
Expand Down
1 change: 1 addition & 0 deletions flutter_inappwebview_android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Updated flutter_inappwebview_platform_interface version to ^1.4.0-beta.3
- Implemented `saveState`, `restoreState` InAppWebViewController methods
- Merged "Android: implemented PlatformPrintJobController.onComplete" [#2216](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2216) (thanks to [Doflatango](https://github.com/Doflatango))
- Fixed "When useShouldInterceptAjaxRequest is true, some ajax requests doesn't work" [#2197](https://github.com/pichillilorenzo/flutter_inappwebview/issues/2197)

## 1.2.0-beta.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@
public class InterceptAjaxRequestJS {

public static final String INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME = "IN_APP_WEBVIEW_INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT";

public static String FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
return
JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._useShouldInterceptAjaxRequest";
}

public static String FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() {
return JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._useOnAjaxReadyStateChange";
}

public static String FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() {
return JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._useOnAjaxProgress";
}

public static String FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() {
return
JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME() + "._interceptOnlyAsyncAjaxRequests";
}

public static PluginScript INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(@Nullable Set<String> allowedOriginRules,
boolean forMainFrameOnly) {
boolean forMainFrameOnly,
boolean initialUseOnAjaxReadyStateChange,
boolean initialUseOnAjaxProgress) {
return
new PluginScript(
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_SOURCE(initialUseOnAjaxReadyStateChange, initialUseOnAjaxProgress),
UserScriptInjectionTime.AT_DOCUMENT_START,
null,
true,
Expand All @@ -35,7 +48,7 @@ public static PluginScript INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(@Nullable Set
public static PluginScript createInterceptOnlyAsyncAjaxRequestsPluginScript(boolean onlyAsync) {
return new PluginScript(
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
"window." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() + " = " + onlyAsync +";",
"window." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() + " = " + onlyAsync + ";",
UserScriptInjectionTime.AT_DOCUMENT_START,
null,
true,
Expand All @@ -44,11 +57,13 @@ public static PluginScript createInterceptOnlyAsyncAjaxRequestsPluginScript(bool
);
}

public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE(boolean initialUseOnAjaxReadyStateChange, boolean initialUseOnAjaxProgress) {
return
"(function(ajax) {" +
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " = true;" +
" w." + FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() + " = " + initialUseOnAjaxReadyStateChange + ";" +
" w." + FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() + " = " + initialUseOnAjaxProgress + ";" +
" var send = ajax.prototype.send;" +
" var open = ajax.prototype.open;" +
" var setRequestHeader = ajax.prototype.setRequestHeader;" +
Expand Down Expand Up @@ -98,8 +113,11 @@ public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
" setRequestHeader.call(this, header, value);" +
" };" +
" function handleEvent(e) {" +
" var self = this;" +
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" 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) {" +
" return;" +
" }" +
" var self = this;" +
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == true) {" +
" var headers = this.getAllResponseHeaders();" +
" var responseHeaders = {};" +
Expand Down Expand Up @@ -154,9 +172,9 @@ public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" var canBeIntercepted = self._flutter_inappwebview_isAsync || w." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE() + " === false;" +
" if (canBeIntercepted && (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == true)) {" +
" if (!this._flutter_inappwebview_already_onreadystatechange_wrapped) {" +
" if (w." + FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() + " === true && !this._flutter_inappwebview_already_onreadystatechange_wrapped) {" +
" this._flutter_inappwebview_already_onreadystatechange_wrapped = true;" +
" var onreadystatechange = this.onreadystatechange;" +
" var realOnreadystatechange = this.onreadystatechange;" +
" this.onreadystatechange = function() {" +
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE() + " == true) {" +
Expand Down Expand Up @@ -198,13 +216,13 @@ public static String INTERCEPT_AJAX_REQUEST_JS_SOURCE() {
" return;" +
" };" +
" }" +
" if (onreadystatechange != null) {" +
" onreadystatechange();" +
" if (realOnreadystatechange != null) {" +
" realOnreadystatechange();" +
" }" +
" });" +
" });" +
" } else if (onreadystatechange != null) {" +
" onreadystatechange();" +
" } else if (realOnreadystatechange != null) {" +
" realOnreadystatechange();" +
" }" +
" };" +
" }" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,11 @@ public void prepareAndAddUserScripts() {
interceptOnlyAsyncAjaxRequestsPluginScript = InterceptAjaxRequestJS.createInterceptOnlyAsyncAjaxRequestsPluginScript(customSettings.interceptOnlyAsyncAjaxRequests);
if (customSettings.useShouldInterceptAjaxRequest) {
userContentController.addPluginScript(interceptOnlyAsyncAjaxRequestsPluginScript);
userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(customSettings.pluginScriptsOriginAllowList,
customSettings.pluginScriptsForMainFrameOnly));
userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(
customSettings.pluginScriptsOriginAllowList,
customSettings.pluginScriptsForMainFrameOnly,
customSettings.useOnAjaxReadyStateChange,
customSettings.useOnAjaxProgress));
}
if (customSettings.useShouldInterceptFetchRequest) {
userContentController.addPluginScript(InterceptFetchRequestJS.INTERCEPT_FETCH_REQUEST_JS_PLUGIN_SCRIPT(customSettings.pluginScriptsOriginAllowList,
Expand Down Expand Up @@ -835,11 +838,22 @@ public void setSettings(InAppWebViewSettings newCustomSettings, HashMap<String,
enablePluginScriptAtRuntime(
InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE(),
newCustomSettings.useShouldInterceptAjaxRequest,
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(customSettings.pluginScriptsOriginAllowList,
customSettings.pluginScriptsForMainFrameOnly)
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT(
customSettings.pluginScriptsOriginAllowList,
customSettings.pluginScriptsForMainFrameOnly,
newCustomSettings.useOnAjaxReadyStateChange,
newCustomSettings.useOnAjaxProgress)
);
}

if (newSettingsMap.get("useOnAjaxReadyStateChange") != null && customSettings.useOnAjaxReadyStateChange != newCustomSettings.useOnAjaxReadyStateChange) {
evaluateJavascript("((window.top == null || window.top === window) ? window : window.top)." + InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_READY_STATE_CHANGE() + " = " + newCustomSettings.useOnAjaxReadyStateChange + ";", null);
}

if (newSettingsMap.get("useOnAjaxProgress") != null && customSettings.useOnAjaxProgress != newCustomSettings.useOnAjaxProgress) {
evaluateJavascript("((window.top == null || window.top === window) ? window : window.top)." + InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_ON_AJAX_PROGRESS() + " = " + newCustomSettings.useOnAjaxProgress + ";", null);
}

if (newSettingsMap.get("interceptOnlyAsyncAjaxRequests") != null && customSettings.interceptOnlyAsyncAjaxRequests != newCustomSettings.interceptOnlyAsyncAjaxRequests) {
enablePluginScriptAtRuntime(
InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE(),
Expand Down Expand Up @@ -1191,12 +1205,12 @@ public Map<String, Object> getCustomSettingsMap() {
public void enablePluginScriptAtRuntime(final String flagVariable,
final boolean enable,
final PluginScript pluginScript) {
evaluateJavascript("window." + flagVariable, null, new ValueCallback<String>() {
evaluateJavascript("((window.top == null || window.top === window) ? window : window.top)." + flagVariable, null, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
boolean alreadyLoaded = value != null && !value.equalsIgnoreCase("null");
if (alreadyLoaded) {
String enableSource = "window." + flagVariable + " = " + enable + ";";
String enableSource = "((window.top == null || window.top === window) ? window : window.top)." + flagVariable + " = " + enable + ";";
evaluateJavascript(enableSource, null, null);
if (!enable) {
userContentController.removePluginScript(pluginScript);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class InAppWebViewSettings implements ISettings<InAppWebViewInterface> {
public List<Map<String, Map<String, Object>>> contentBlockers = new ArrayList<>();
public Integer preferredContentMode = PreferredContentModeOptionType.RECOMMENDED.toValue();
public Boolean useShouldInterceptAjaxRequest = false;
public Boolean useOnAjaxReadyStateChange = false;
public Boolean useOnAjaxProgress = false;
public Boolean interceptOnlyAsyncAjaxRequests = true;
public Boolean useShouldInterceptFetchRequest = false;
public Boolean incognito = false;
Expand Down Expand Up @@ -219,6 +221,12 @@ public InAppWebViewSettings parse(@NonNull Map<String, Object> settings) {
case "useShouldInterceptAjaxRequest":
useShouldInterceptAjaxRequest = (Boolean) value;
break;
case "useOnAjaxReadyStateChange":
useOnAjaxReadyStateChange = (Boolean) value;
break;
case "useOnAjaxProgress":
useOnAjaxProgress = (Boolean) value;
break;
case "interceptOnlyAsyncAjaxRequests":
interceptOnlyAsyncAjaxRequests = (Boolean) value;
break;
Expand Down Expand Up @@ -497,6 +505,8 @@ public Map<String, Object> toMap() {
settings.put("contentBlockers", contentBlockers);
settings.put("preferredContentMode", preferredContentMode);
settings.put("useShouldInterceptAjaxRequest", useShouldInterceptAjaxRequest);
settings.put("useOnAjaxReadyStateChange", useOnAjaxReadyStateChange);
settings.put("useOnAjaxProgress", useOnAjaxProgress);
settings.put("interceptOnlyAsyncAjaxRequests", interceptOnlyAsyncAjaxRequests);
settings.put("useShouldInterceptFetchRequest", useShouldInterceptFetchRequest);
settings.put("incognito", incognito);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,19 @@ class AndroidHeadlessInAppWebView extends PlatformHeadlessInAppWebView
settings.useOnDownloadStart == null) {
settings.useOnDownloadStart = true;
}
if (params.shouldInterceptAjaxRequest != null &&
settings.useShouldInterceptAjaxRequest == null) {
settings.useShouldInterceptAjaxRequest = true;
if ((params.shouldInterceptAjaxRequest != null ||
params.onAjaxProgress != null ||
params.onAjaxReadyStateChange != null)) {
if (settings.useShouldInterceptAjaxRequest == null) {
settings.useShouldInterceptAjaxRequest = true;
}
if (params.onAjaxReadyStateChange != null &&
settings.useOnAjaxReadyStateChange == null) {
settings.useOnAjaxReadyStateChange = true;
}
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
settings.useOnAjaxProgress = true;
}
}
if (params.shouldInterceptFetchRequest != null &&
settings.useShouldInterceptFetchRequest == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,18 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {
settings.useOnDownloadStart = true;
}
if ((params.shouldInterceptAjaxRequest != null ||
params.onAjaxProgress != null ||
params.onAjaxReadyStateChange != null) &&
settings.useShouldInterceptAjaxRequest == null) {
settings.useShouldInterceptAjaxRequest = true;
params.onAjaxProgress != null ||
params.onAjaxReadyStateChange != null)) {
if (settings.useShouldInterceptAjaxRequest == null) {
settings.useShouldInterceptAjaxRequest = true;
}
if (params.onAjaxReadyStateChange != null &&
settings.useOnAjaxReadyStateChange == null) {
settings.useOnAjaxReadyStateChange = true;
}
if (params.onAjaxProgress != null && settings.useOnAjaxProgress == null) {
settings.useOnAjaxProgress = true;
}
}
if (params.shouldInterceptFetchRequest != null &&
settings.useShouldInterceptFetchRequest == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,13 +1455,13 @@ class AndroidInAppWebViewController extends PlatformInAppWebViewController

if (webviewParams != null &&
webviewParams!.onAjaxReadyStateChange != null)
return (await webviewParams!.onAjaxReadyStateChange!(
return jsonEncode((await webviewParams!.onAjaxReadyStateChange!(
_controllerFromPlatform, request))
?.toNativeValue();
?.toNativeValue());
else
return (await _inAppBrowserEventHandler!
return jsonEncode((await _inAppBrowserEventHandler!
.onAjaxReadyStateChange(request))
?.toNativeValue();
?.toNativeValue());
}
return null;
case "onAjaxProgress":
Expand All @@ -1474,13 +1474,13 @@ class AndroidInAppWebViewController extends PlatformInAppWebViewController

if (webviewParams != null &&
webviewParams!.onAjaxProgress != null)
return (await webviewParams!.onAjaxProgress!(
return jsonEncode((await webviewParams!.onAjaxProgress!(
_controllerFromPlatform, request))
?.toNativeValue();
?.toNativeValue());
else
return (await _inAppBrowserEventHandler!
return jsonEncode((await _inAppBrowserEventHandler!
.onAjaxProgress(request))
?.toNativeValue();
?.toNativeValue());
}
return null;
case "shouldInterceptFetchRequest":
Expand Down
Loading

0 comments on commit d8f7484

Please sign in to comment.