Javascript 拦截和修改XMLHttpRequests
我正在编写一个扩展,使用户可以拦截任何ajax请求并对其进行修改。下面的代码可以工作。它将截获xmlhttprequest打开/发送,并用对话框提示我。问题是,我在xmlhttprequest.open中使用了prompt(),而不是我希望它能够使用的promptOpen()方法。我想知道是否有人知道我需要做什么才能让它工作Javascript 拦截和修改XMLHttpRequests,javascript,ajax,asynchronous,Javascript,Ajax,Asynchronous,我正在编写一个扩展,使用户可以拦截任何ajax请求并对其进行修改。下面的代码可以工作。它将截获xmlhttprequest打开/发送,并用对话框提示我。问题是,我在xmlhttprequest.open中使用了prompt(),而不是我希望它能够使用的promptOpen()方法。我想知道是否有人知道我需要做什么才能让它工作 (function(){ async function promptSend(data){ return new Promise(function
(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));
}
})()
但正如我所说,它根本不做任何ajax请求,甚至不提示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));
}