My Script
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
//console.log(this.responseURL);
if(this.responseURL == "https://get.example.com/dashboard/summary")
{
// Run the function
funMain();
}
});
origOpen.apply(this, arguments);
};
function funMain() {
// My codes
}
This is my script and it's working as expected.
What this does is, this listen to XMLHttpRequests and when an XMLHttpRequest is made to 'https://get.example.com/dashboard/summary', it calls funMain
function. Since this is an event, this is not a one time event. This can be happened multiple times. This XMLHttpRequest is made by https://example.com/dashboard webpage.
Process: If an XMLHttpRequest is made to 'https://get.example.com/dashboard/summary' by https://example.com/dashboard webpage, then call funMain
function.
So now, I want to build an extension using this working script.
This is what I have tried so far...
Content Script
function funMain() {
// My codes
}
Note: I know that I have to use browser.runtime.sendMessage() to communicate between background script and content script. But at the moment, I'm trying to get the event function (in the Background Script) working.
Background Script
console.log("Msg 01 - Background script is running.");
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
console.log("Msg 02 - " + this.responseURL);
if(this.responseURL == "https://get.example.com/dashboard/summary")
{
// Run the function
// funMain();
console.log("Msg 03 - Hello! I am working as expected.");
}
});
origOpen.apply(this, arguments);
};
Manifest
{
"manifest_version": 2,
"name": "Beastify",
"version": "1.0",
"description": "des",
"icons": {
"48": "icons/beasts-48.png"
},
"permissions": [
"webRequest",
"<all_urls>"
],
"content_scripts": [{
"matches": [
"*://*.example.com/dashboard"
],
"js": [
"content-script.js"
]
}],
"background": {
"scripts": ["background.js"]
}
}
The problem is, the event does not work as expected. Therefore, funMain cannot be called. When I inspect the background page, I can only see Msg 01. I cannot see Msg 02 or Msg 03.
Output: background.js
Msg 01 - Background script is running.
What am I doing wrong and how can I fix this?