سؤال

I have a chrome content script running on a few pages, with a console.log (and nothing else). I've noticed that on one page the script doesn't run (no console output). The only thing different about this page is that its original code runs with an error. Can this be the reason the content script does not run on this page?

Is there a way to make it run in spite of the error?

هل كانت مفيدة؟

المحلول

The script execution environment of a Content script differs from the page. Consequently, runtime errors in a web page will generally not cause any problems in content script, and vice versa.

It's however possible that the content script just stops working because the expected conditions are not met (any more). For instance, an element was expected to exist in the document, but not found. The reverse can also be true: The content script modifies the page to such extend that the page doesn't function any more.


The Content security policy of the page does affect content scripts though. This can lead to unexpected results, such as blocked cross-origin requests (example).

نصائح أخرى

The only thing different about this page is that its original code run with an error

An error can cause your JavaScript program from running. Also, the location of your script might be an issue. Just like HTML, JavaScript runs top-to-bottom. It's a little hard to tell without seeing your code.

To fix it, you can try moving your js higher up on your html file or solve your error with the help of your browser's error console.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top