Я пишу расширение, чтобы позволить пользователям перехватывать любой запрос ajax и позволять им изменять его. Код ниже работает. Он будет перехватывать и открывать / отправлять xmlhttprequest и выводить диалоговое окно. Проблема в том, что я использую prompt () в xmlhttprequest.open вместо метода promptOpen (), с которым я ожидал, что он будет работать. Мне интересно, есть ли у кого-нибудь подсказка о том, что мне нужно сделать, чтобы заставить его работать.
(function(){
async function promptSend(data){
return new Promise(function(resolve){
var swalConfig = {
html: `<textarea id="swal-input1" class="" rows="10" cols="50"></textarea><br><br>`,
focusConfirm: false,
preConfirm: () => {
resolve([document.getElementById("swal-input1").value])
},
onBeforeOpen: () => {
document.getElementById("swal-input1").value = data[0];
},
showCancelButton: false,
allowOutsideClick: false
};
try{
Swal.getQueueStep();
Swal.insertQueueStep(swalConfig)
}catch(e){
Swal.queue([swalConfig])
}
});
}
async function promptOpen(data){
return new Promise(function(resolve){
var swalConfig = {
html: `<label for='swal-input1'>Method</label><input name='swal-input1' id='swal-input1' style='width:100%'><br><br>`+
`<label for='swal-input2'>Url</label><input name='swal-input2' id='swal-input2' style='width:100%'>`,
focusConfirm: false,
preConfirm: () => {
resolve([document.getElementById("swal-input1").value,document.getElementById("swal-input2").value])
},
onBeforeOpen: () => {
document.getElementById("swal-input1").value = data[0];
document.getElementById("swal-input2").value = data[1];
},
showCancelButton: false,
allowOutsideClick: false
};
try{
Swal.getQueueStep();
Swal.insertQueueStep(swalConfig)
}catch(e){
Swal.queue([swalConfig])
}
})
}
// Intercepts all XmlHttpRequest.open calls
var proxiedOpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
//let newArguments = await promptOpen(arguments)
arguments[0] = prompt("Enter Method",arguments[0])
arguments[1] = prompt("Enter Url",arguments[1])
return proxiedOpen.apply(this, [].slice.call(arguments));
}
// Intercepts all XmlHttpRequest.send calls
var proxiedSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = async function() {
let newArgument = await promptSend(arguments);
arguments[0] = newArgument[0];
return proxiedSend.apply(this, [].slice.call(arguments));
}
})()
Любая попытка преобразовать ее в promptOpen приведет к выполненному вызову ajax звонить / не определено или вообще не звонить.
Я попробовал наиболее очевидный подход, превратив его в asyn c функцию и используя await, например, как работает xmlhttprequest.send.
var proxiedOpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = async function(method, url, async, user, password) {
let newArguments = await promptOpen(arguments)
arguments[0] = newArguments[0]
arguments[1] = newArguments[1]
return proxiedOpen.apply(this, [].slice.call(arguments));
}
но, как я уже сказал, он вообще не выполняет никаких запросов ajax и даже не запрашивает окно xmlhttprequest.send.