I make a firefox extension that get all the request url's and displays them. But the code only works if I paste it in the console.
when the extension loads it doesn't show any error, it seems like it just won't run
here is the full code
xhrScript.js
(function(){
const proxiedOpen = XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function ( _, url) {
this.__URL = url;
return proxiedOpen.apply(this, arguments);
};
const proxiedSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.send = function () {
const { protocol, host } = window.location;
// showing only when it paste in console
console.log("full request url ", `${protocol}//${host}${this.__URL}`);
return proxiedSend.apply(this, [].slice.call(arguments));
};
})();
// this works all times
document.body.style.border = "7px solid blue";
manifest.json
{
"manifest_version": 2,
"name": "XHR request urls",
"version": "1.0",
"description": "get all the request url's",
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["xhrScript.js"]
}
]
}
As you can see, in the last line is document.body.style.border = "7px solid blue";
, this works fine every time. But the XMLHttpRequest open
and send
methods don't work. only works if I paste the code in the console.
if you want see an example, you can try copy and paste the xhrScript.js
code in https://reactjs.org (it's a SPA, so it's easy to check what I want) in the devTools console, and see all the request.
I don't know why this code only runs when it is pasted in console
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…