MyNick
MyNick

Reputation: 556

Chrome extension - on click event

I started to learn today how to make chrome extension. All i want it to do is alert when i press the extension button.

My manifest.json file:

{
"name": "BrowserActionExtension",
"version": "0.0.1",
"manifest_version": 2,
"background": { "scripts": ["background.js"] },
"browser_action": {
    "default_icon": {
        "19": "icons/19x19.png",
        "38": "icons/38x38.png"
    },
    "default_title": "That's the tool tip",
    "default_popup": "popup.html"
}
}

i tied couple of things. The first was to run a script in the popup.html. but i read that i can't use inline scripts. My popup.html file:

<html>
<head>
    <script src='script.js' type='text/javascript'></script>
</head>

<body>
    Hello World!
</body>
</html>

and my script file:

<script>
console.log("clicked");
alert("hey");
</script>

none of them works, no alert and it doesn't write into the console.

last thing i tried was to use: chrome.browserAction.onClicked.addListener I used in background script, i figured out it works once when the extension starts to work (didn't understand if it run for every tab or once i open the browser).

function dostuff() {
  console.log("HEY")
  alert("FDS");
}

chrome.browserAction.onClicked.addListener(dostuff);

How can i alert when the extension is pressed?

Thank you for your help and time

Upvotes: 0

Views: 673

Answers (1)

Xan
Xan

Reputation: 77523

1) You should not wrap the contents of an included JavaScript file in <script> tag.

Remove it, and your example should work.

Oh, and by the way, that console.log("clicked"); will output into popup's own console. You can examine (and see the error about your code) by right-clicking the button and selecting "Inspect popup".

2) Your code in the background does not work, because the event does not fire while you're using a popup page. See here for more information.

Upvotes: 3

Related Questions