Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 669
priestc Avatar asked Jan 11 '15 15:01

priestc


1 Answers

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

like image 111
Marco Bonelli Avatar answered Oct 03 '22 13:10

Marco Bonelli