I am creating a Firefox extension, where I want to get the stack trace each time a network request is sent. Currently, I inject a content script to access the stack but cannot get the content script to send the stack trace back to background script. Specifically, I listen for a message in content script (from background script) with browser.runtime.onMessage.addListener
and call my code in the content script to get me the stack trace but it seems that when I am inside the browser.runtime.onMessage.addListener
I cannot call JS methods that I injected with the content script. Any ideas on how I can get the stack trace when a network request is initiated?
background.js:
browser.webRequest.onBeforeSendHeaders.addListener(
function (details) {
if (details.tabId == -1) {
return;
}
browser.tabs.sendMessage(details.tabId, { content: details.url });
},
{
urls: [
"http://*/*",
"https://*/*",
],
},
["requestHeaders", "blocking"]
);
content_script.js
function getPageScript() {
return "function codeToInject() {console.trace();} codeToInject();";
}
function insertScript(text) {
var parent = document.documentElement,
script = document.createElement('script');
script.text = text;
script.async = false;
parent.insertBefore(script, parent.firstChild);
}
browser.runtime.onMessage.addListener(returnStack);
function returnStack() {
insertScript(getPageScript());
}
manifest.json
{
"manifest_version": 2,
"name": "call-stack-retrieval",
"version": "0.0",
"background": {
"persistent": true,
"scripts": [
"background.js"
]
},
"content_scripts": [{
"all_frames": true,
"js": [
"content_script.js"
],
"match_about_blank": false,
"matches": [
"<all_urls>"
],
"run_at": "document_start"
}],
"permissions": [
"tabs",
"<all_urls>",
"contextMenus",
"webRequest",
"webRequestBlocking",
"webNavigation",
"storage",
"unlimitedStorage",
"notifications"
]
}