Intercepting AJAX in Flutter webview works on iOS but not Android

Viewed 1232

I am trying to intercept the ajax requests in webview using XMLHttpRequest. In iOS it is always working fine with flutter webview plugin but the same script injection code is not working on Android. Strange thing is it is not throwing any particular error even.

Some help on this will be really appreciable as this seems to be an elementary functional issue for webview.

Minimal example:

I am using the following Javascript code for intercepting Ajax request:

var open = window.XMLHttpRequest.prototype.open,
    send = window.XMLHttpRequest.prototype.send,
    onReadyStateChange;

function openReplacement(method, url, async, user, password) {
console.log('new data22');
    var syncMode = async !== false ? 'async' : 'sync';
    if (url === '/api/fakeCall') {
        console.log('Preparing ' + syncMode + ' HTTP request : ' + method + ' ' + url);
    }
    return open.apply(this, arguments);
}

function sendReplacement(data) {
    console.log('Sending HTTP request data : ', data);

    if(this.onreadystatechange) {
        this._onreadystatechange = this.onreadystatechange;
    }
    this.onreadystatechange = onReadyStateChangeReplacement;
    return send.apply(this, arguments);
}

function onReadyStateChangeReplacement() {
    console.log('HTTP request ready state changed : ' + this.readyState + ' ' + this.readyState + ' ' + XMLHttpRequest.DONE);
    if (this.readyState === XMLHttpRequest.DONE) {
        if (this.responseText !== "" && this.responseText !== null) {
            if (this.responseText.indexOf('fareSessionUUID') !== -1) {
                console.log('________________response____________');
                var oData = JSON.stringify({'data': this.responseText});
                    console.log('new data');
                    console.log(oData);
                window.flutter_inappbrowser.callHandler('myHandler', {foo: oData}).then(function(result) {
                    console.log(result, typeof result);
                    console.log(JSON.stringify(result));
                })
            }
        }
     }
    if (this._onreadystatechange) {
        return this._onreadystatechange.apply(this, arguments);
    }
}
console.log(openReplacement.toString());
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
0 Answers
Related