1

On my chrome extension I'm trying to listen to logs that were output from any tab.

On my content-script.js I have

import browser from 'webextension-polyfill'

console.defaultLog = console.log.bind(console)

console.log = function () {
  console.defaultLog.apply(console, arguments)

  browser.runtime.sendMessage({
    name: 'registerLog',
    payload: { log: Array.from(arguments) }
  })
}

In my background.ts I have:

import browser from 'webextension-polyfill'

browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log(request)
})

Then, I created an extremely simple page test.html where it just has a setInterval using console.log:

<html>
<script>
  setInterval(() => console.log('from page test'), 1000)
</script>
</html>

I can see the from page test log in my test.html but I can't see the log in my service work extension.

Is it possible to listen to console.logs from pages in a chrome extension? If yes, what am I doing wrong?

Bruno Francisco
  • 3,841
  • 4
  • 31
  • 61

0 Answers0