I have a content script (running in world MAIN) that is listening to changes in console.log
and it should send a message to background.ts
when a console.log
happens.
manifest.json
{
"content_scripts": [
{
"world": "MAIN",
"js": ["src/contentScripts/registerConsoleLogs.ts"],
"matches": [
"http://*/*",
"https://*/*"
],
"run_at": "document_start"
}
],
}
registerConsoleLogs.ts
console.stdlog = console.log.bind(console)
console.log = function () {
console.stdlog.apply(console, arguments)
const payload = {
name: 'registerLog',
payload: { severity: 'warning', log: arguments }
}
}
export default {}
background.ts
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.name === 'registerLog') {
console.log('log received: ')
console.log(request.payload)
}
})
My doubt is: how can I send a message from registerConsoLogs.ts
to background.ts
?
The examples (1, 2) do not use the world: "MAIN"
. I'm fairly new to developing web extension and would love some guidance on how to pass the messages from content script with world: "MAIN"
to background