0

My content.js script example, which is only supposed to console log arguments passed to ajax requests:

console.log("this always runs, but nothing more!");
var oldxhr = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
  console.log(args);
  sendMessageToServiceworker(args); // I tested sending only strings as well. 
  oldxhr.apply(this, arguments);
};

// I also tried to send message to service-worker.js
function sendMessageToServiceworker(message) {
    chrome.runtime.sendMessage({message}, (response) =>  {
        if (response.message === 'success'){ 
            console.log(response.profile);            
        }
    });
}

service-worker.js example:

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    console.log(request);
    if (request.message) {
        console.log(request.message);
        sendResponse({message: "success"});
    }

});

Partial manifest file:

{
    "manifest_version": 3,
    "background": {
        "service_worker": "service-worker.js"
    },
    "content_scripts": [{
        "js": ["hooks.js"],
        "run_at": "document_start",
        "matches": ["https://example.org/*", "https://*.example.org/*"]
    }]
}

I tried to launch content script at document_start, document_end, document_idle, even with listening for DOMContentLoaded, it never runs beyond the first console.log. If I paste it manually into the console, then it works for the ajax requests that happen later. It also works as user script in tempermonkey extension, without sending message to the service worker of course. What am I missing? Extra permissions?

wOxxOm
  • 53,493
  • 8
  • 111
  • 119
miran80
  • 745
  • 4
  • 16

0 Answers0