I want to send some data from my content.js to background script in my chrome extension. I tried chrome.runtime.sendMessage but it is undefined. It happens because of my manifest file where world:MAIN is set for content script. Here is the code for content.js
listenerFn = (event) => {
const url = event.target.responseURL;
if (url.startsWith("https://example.com/api/v2/home/allfeed")) {
chrome.runtime.sendMessage({
type: "response",
data: event.target.responseText,
});
}
};
(() => {
let XHR = XMLHttpRequest.prototype;
let send = XHR.send;
XHR.send = function () {
this.addEventListener("load", listenerFn);
return send.apply(this, arguments);
};
})();
manifest.json
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start",
"world": "MAIN"
}
]
Is there a way of sending data from content script to background script.