Я пытаюсь перехватить запросы ajax в веб-просмотре, используя XMLHttpRequest
. В iOS он всегда работает нормально с плавающим веб-представлением plugin , но тот же код внедрения скрипта не работает на Android. Странно то, что она не выдает даже какую-то конкретную ошибку.
Некоторая помощь по этому вопросу будет действительно заметной, так как это кажется элементарной функциональной проблемой для веб-просмотра.
Минимальный пример:
Я использую следующий код Javascript для перехвата Ajax-запроса:
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;