Skip to content

Commit

Permalink
updated AjaxRequest class, fixed 'InvalidStateError' ajax error #189,…
Browse files Browse the repository at this point in the history
… No need to listen to window.addEventListener(flutterInAppBrowserPlatformReady, fuction(){ }) javascript event anymore to use JaaScript message handlers
  • Loading branch information
pichillilorenzo committed Nov 10, 2019
1 parent 44c17d1 commit f89610a
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 249 deletions.
181 changes: 98 additions & 83 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- Renamed `injectScriptCode` to `evaluateJavascript`
- Renamed `injectStyleCode` to `injectCSSCode`
- Renamed `injectStyleFile` to `injectCSSFileFromUrl`
- No need to listen to `window.addEventListener("flutterInAppBrowserPlatformReady", fuction(){ })` javascript event anymore to call `window.flutter_inappbrowser.callHandler(handlerName <String>, ...args)` to use the JavaScript message handlers

## 1.2.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ final public class InAppWebView extends InputAwareWebView {
" }" +
"})(window.console);";

static final String platformReadyJS = "window.dispatchEvent(new Event('flutterInAppBrowserPlatformReady'));";

static final String variableForOnLoadResourceJS = "window._flutter_inappbrowser_useOnLoadResource";
static final String enableVariableForOnLoadResourceJS = variableForOnLoadResourceJS + " = $PLACEHOLDER_VALUE;";

Expand Down Expand Up @@ -116,6 +114,29 @@ final public class InAppWebView extends InputAwareWebView {
" ajax.prototype._flutter_inappbrowser_password = null;" +
" ajax.prototype._flutter_inappbrowser_already_onreadystatechange_wrapped = false;" +
" ajax.prototype._flutter_inappbrowser_request_headers = {};" +
" function convertRequestResponse(request, callback) {" +
" if (request.response != null && request.responseType != null) {" +
" switch (request.responseType) {" +
" case 'arraybuffer':" +
" callback(new Uint8Array(request.response));" +
" return;" +
" case 'blob':" +
" const reader = new FileReader();" +
" reader.addEventListener('loadend', function() { " +
" callback(new Uint8Array(reader.result));" +
" });" +
" reader.readAsArrayBuffer(blob);" +
" return;" +
" case 'document':" +
" callback(request.response.documentElement.outerHTML);" +
" return;" +
" case 'json':" +
" callback(request.response);" +
" return;" +
" };" +
" }" +
" callback(null);" +
" };" +
" ajax.prototype.open = function(method, url, isAsync, user, password) {" +
" isAsync = (isAsync != null) ? isAsync : true;" +
" this._flutter_inappbrowser_url = url;" +
Expand Down Expand Up @@ -143,36 +164,40 @@ final public class InAppWebView extends InputAwareWebView {
" responseHeaders[header] = value;" +
" });" +
" }" +
" var ajaxRequest = {" +
" method: this._flutter_inappbrowser_method," +
" url: this._flutter_inappbrowser_url," +
" isAsync: this._flutter_inappbrowser_isAsync," +
" user: this._flutter_inappbrowser_user," +
" password: this._flutter_inappbrowser_password," +
" withCredentials: this.withCredentials," +
" headers: this._flutter_inappbrowser_request_headers," +
" readyState: this.readyState," +
" status: this.status," +
" responseURL: this.responseURL," +
" responseType: this.responseType," +
" responseText: this.responseText," +
" statusText: this.statusText," +
" responseHeaders, responseHeaders," +
" event: {" +
" type: e.type," +
" loaded: e.loaded," +
" lengthComputable: e.lengthComputable," +
" total: e.total" +
" }" +
" };" +
" window." + JavaScriptBridgeInterface.name + ".callHandler('onAjaxProgress', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
" switch (result) {" +
" case 0:" +
" self.abort();" +
" return;" +
" };" +
" }" +
" convertRequestResponse(this, function(response) {" +
" var ajaxRequest = {" +
" method: self._flutter_inappbrowser_method," +
" url: self._flutter_inappbrowser_url," +
" isAsync: self._flutter_inappbrowser_isAsync," +
" user: self._flutter_inappbrowser_user," +
" password: self._flutter_inappbrowser_password," +
" withCredentials: self.withCredentials," +
" headers: self._flutter_inappbrowser_request_headers," +
" readyState: self.readyState," +
" status: self.status," +
" responseURL: self.responseURL," +
" responseType: self.responseType," +
" response: response," +
" responseText: (self.responseType == 'text' || self.responseType == '') ? self.responseText : null," +
" responseXML: (self.responseType == 'document' && self.responseXML != null) ? self.responseXML.documentElement.outerHTML : null," +
" statusText: self.statusText," +
" responseHeaders, responseHeaders," +
" event: {" +
" type: e.type," +
" loaded: e.loaded," +
" lengthComputable: e.lengthComputable," +
" total: e.total" +
" }" +
" };" +
" window." + JavaScriptBridgeInterface.name + ".callHandler('onAjaxProgress', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
" switch (result) {" +
" case 0:" +
" self.abort();" +
" return;" +
" };" +
" }" +
" });" +
" });" +
" }" +
" };" +
Expand All @@ -195,33 +220,37 @@ final public class InAppWebView extends InputAwareWebView {
" responseHeaders[header] = value;" +
" });" +
" }" +
" var ajaxRequest = {" +
" method: this._flutter_inappbrowser_method," +
" url: this._flutter_inappbrowser_url," +
" isAsync: this._flutter_inappbrowser_isAsync," +
" user: this._flutter_inappbrowser_user," +
" password: this._flutter_inappbrowser_password," +
" withCredentials: this.withCredentials," +
" headers: this._flutter_inappbrowser_request_headers," +
" readyState: this.readyState," +
" status: this.status," +
" responseURL: this.responseURL," +
" responseType: this.responseType," +
" responseText: this.responseText," +
" statusText: this.statusText," +
" responseHeaders: responseHeaders" +
" };" +
" window." + JavaScriptBridgeInterface.name + ".callHandler('onAjaxReadyStateChange', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
" switch (result) {" +
" case 0:" +
" self.abort();" +
" return;" +
" };" +
" }" +
" if (onreadystatechange != null) {" +
" onreadystatechange();" +
" }" +
" convertRequestResponse(this, function(response) {" +
" var ajaxRequest = {" +
" method: self._flutter_inappbrowser_method," +
" url: self._flutter_inappbrowser_url," +
" isAsync: self._flutter_inappbrowser_isAsync," +
" user: self._flutter_inappbrowser_user," +
" password: self._flutter_inappbrowser_password," +
" withCredentials: self.withCredentials," +
" headers: self._flutter_inappbrowser_request_headers," +
" readyState: self.readyState," +
" status: self.status," +
" responseURL: self.responseURL," +
" responseType: self.responseType," +
" response: response," +
" responseText: (self.responseType == 'text' || self.responseType == '') ? self.responseText : null," +
" responseXML: (self.responseType == 'document' && self.responseXML != null) ? self.responseXML.documentElement.outerHTML : null," +
" statusText: self.statusText," +
" responseHeaders: responseHeaders" +
" };" +
" window." + JavaScriptBridgeInterface.name + ".callHandler('onAjaxReadyStateChange', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
" switch (result) {" +
" case 0:" +
" self.abort();" +
" return;" +
" };" +
" }" +
" if (onreadystatechange != null) {" +
" onreadystatechange();" +
" }" +
" });" +
" });" +
" } else if (onreadystatechange != null) {" +
" onreadystatechange();" +
Expand All @@ -243,7 +272,8 @@ final public class InAppWebView extends InputAwareWebView {
" user: this._flutter_inappbrowser_user," +
" password: this._flutter_inappbrowser_password," +
" withCredentials: this.withCredentials," +
" headers: this._flutter_inappbrowser_request_headers" +
" headers: this._flutter_inappbrowser_request_headers," +
" responseType: this.responseType" +
" };" +
" window." + JavaScriptBridgeInterface.name + ".callHandler('shouldInterceptAjaxRequest', ajaxRequest).then(function(result) {" +
" if (result != null) {" +
Expand All @@ -254,6 +284,9 @@ final public class InAppWebView extends InputAwareWebView {
" };" +
" data = result.data;" +
" self.withCredentials = result.withCredentials;" +
" if (result.responseType != null) {" +
" self.responseType = result.responseType;" +
" };" +
" for (var header in result.headers) {" +
" var value = result.headers[header];" +
" self.setRequestHeader(header, value);" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,6 @@ public void onPageFinished(WebView view, String url) {
view.clearFocus();
view.requestFocus();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript(InAppWebView.platformReadyJS, (ValueCallback<String>) null);
} else {
webView.loadUrl("javascript:" + InAppWebView.platformReadyJS.replaceAll("[\r\n]+", ""));
}

Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
Expand Down
21 changes: 9 additions & 12 deletions example/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,23 @@ <h1 class="cover-heading">Inline WebView</h1>
window.location = "#foo-" + randomNumber;
}

window.addEventListener("flutterInAppBrowserPlatformReady", function(event) {
console.log("ready");

window.flutter_inappbrowser.callHandler('handlerFoo').then(function(result) {
//console.log(result, typeof result);
//console.log(JSON.stringify(result), result.bar);
});
window.flutter_inappbrowser.callHandler('handlerFoo').then(function(result) {
console.log(result, typeof result);
console.log(JSON.stringify(result), result.bar);
});

window.flutter_inappbrowser.callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}).then(function(result) {
//console.log(result, typeof result);
//console.log(JSON.stringify(result));
});
window.flutter_inappbrowser.callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}).then(function(result) {
console.log(result, typeof result);
console.log(JSON.stringify(result));
});

$(document).ready(function() {

console.log("jQuery ready");

var xhttp = new XMLHttpRequest();
xhttp.addEventListener("load", function() {
console.log(this.responseText);
console.log(this.response);
});
xhttp.open("POST", "http://192.168.1.20:8082/test-ajax-post");
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Expand Down
7 changes: 5 additions & 2 deletions example/lib/inline_example.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,19 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
},
shouldInterceptAjaxRequest: (InAppWebViewController controller, AjaxRequest ajaxRequest) async {
print("AJAX REQUEST: ${ajaxRequest.method} - ${ajaxRequest.url}, DATA: ${ajaxRequest.data}");
if (ajaxRequest.url == "http://192.168.1.20:8082/test-ajax-post") {
ajaxRequest.responseType = 'json';
}
// ajaxRequest.method = "GET";
// ajaxRequest.url = "http://192.168.1.20:8082/test-download-file";
// ajaxRequest.headers = {
// "Custom-Header": "Custom-Value"
// };
// return ajaxRequest;
return null;
return ajaxRequest;
},
onAjaxReadyStateChange: (InAppWebViewController controller, AjaxRequest ajaxRequest) async {
print("AJAX READY STATE CHANGE: ${ajaxRequest.method} - ${ajaxRequest.url}, ${ajaxRequest.status}, ${ajaxRequest.readyState}, ${ajaxRequest.responseType}, ${ajaxRequest.responseText}, ${ajaxRequest.responseHeaders}");
print("AJAX READY STATE CHANGE: ${ajaxRequest.method} - ${ajaxRequest.url}, ${ajaxRequest.status}, ${ajaxRequest.readyState}, ${ajaxRequest.responseType}, ${ajaxRequest.responseText}, ${ajaxRequest.response}, ${ajaxRequest.responseHeaders}");
return AjaxRequestAction.PROCEED;
},
onAjaxProgress: (InAppWebViewController controller, AjaxRequest ajaxRequest) async {
Expand Down
Loading

0 comments on commit f89610a

Please sign in to comment.