Can you use javascript in a page action popup?

I'm trying to build a chrome extension that uses the page action popup feature, but none of my javascript seems to be working. Here is the source:

<!DOCTYPE html>
<html>
    <body>
        <input type="button" id="button1" value="first button">
        <input type="button" id="button2" value="second button">
        <script>
           console.log("do anything!!");
        </script>
    </body>
</html>

I can't see the logger statement anywhere. I can't seem to get anything to run. Is this how popups are supposed to work? The docs make no mention of these popups being static HTML only.

Answers:

Answer

First of all, Chrome extensions are not allowed to run inline Javascript Code (aka any piece of code that is not included inside a .js file, but in the DOM).

Quoting from the Content Security Policy page:

There is no mechanism for relaxing the restriction against executing inline JavaScript. In particular, setting a script policy that includes 'unsafe-inline' will have no effect.

Therefore, you'll need to put your code inside another file and import it, like this:

<sctipt src="popup.js"></script>

Secondly, the code you run into the popup is logged in its own console, so you have to right-click on your extension's icon, and hit "Inspect popup". Then, a developer tools window will appear, and you'll be able to browse to the "Console" tab and see the log. Alternatively, you can open the popup and right click anywhere inside it, then choose "Inspect element", like on any normal page.

enter image description here

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.