Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console doesn't log js errors from content script

I know that to debug content script use normal web developer tools (https://developer.mozilla.org/en/docs/Mozilla/Add-ons/WebExtensions/Debugging#Debugging_content_scripts), and this works perfect. debugger keyword works as intended.

But in this exact situation things get broken:

addon.id = "123-568-485"; // I never define `addon` before this line, so this cause: ReferenceError: "addon is not defined". We aren't aware of this mistake.

// Some more code
// Some more code
// Some more code
// Some more code

debugger; // Here we want to stop execution and inspect, some other stuff. Remember that we aren't aware of earlier mistake.

What we would expect, that in console error about Reference error will appear, but it doesn't. Console get silent, and we don't know why our debugger keyword doesn't work.

This kind of silent error, happened to me when I misspell variable name. In result couldn't figure out what's wrong.

like image 518
Sonny D Avatar asked Nov 10 '18 07:11

Sonny D


People also ask

How do I fix JavaScript console error?

Go to the screen where you are experiencing the error. In Chrome, navigate to View > Developer > JavaScript Console or More Tools > JavaScript Console or press Ctrl + Shift + J. The error console will open. If you don't see any errors try reloading the page.

How do I show console errors?

Press Command+Option+J (Mac) or Control+Shift+J (Windows, Linux, Chrome OS) to jump straight into the Console panel of Chrome DevTools. Or, navigate to More Tools > Developer Tools from Chrome menu, and click Console tab. The error console will open.

Why you should not use console log?

console. log() is the method we use most often in our work, and you can use it anywhere in JavaScript. But when the amount of printed information becomes very large, the information becomes unintuitive . Because we don't know what it refers to!

How do I avoid JavaScript errors?

To avoid such syntax errors, you should spend time learning the grammatical rules of the JavaScript programming language. With extensive coding practice, you can spot the grammatical mistakes easily and avoid shipping them with your developed application.


2 Answers

The errors in the content script are not reported in the tab's Web Console due to Firefox bug 1410932, which is not fixed (as of Firefox 79, released on 2020-07-28).

I listed possible workarounds in another answer:

  • use try..catch with logging,
  • check the Browser Console (which does show errors from the content script)
  • use the Debugger's "pause on exceptions" option.
like image 138
Nickolay Avatar answered Oct 20 '22 21:10

Nickolay


Content scripts are executed in webpage, So as you know to see it's output you should open up console menu in that specific web page (ctrl+shift+e then go to console).
But if something is wrong with content script and cause it to throw exception, The error log would be shown in debug area of your extension in: about:debugging

enter image description here

I think the reason is content scripts are treated like extra frame for webpage and their error is shown there.

like image 36
Jafar Akhondali Avatar answered Oct 20 '22 20:10

Jafar Akhondali