2

I'm trying to build an extension for Chrome, but I'm a newbie and I'm having trouble understanding the Docs provided by Google. I want the extension to have a popup that shows a few buttons, and when a button is clicked, I want to run a script.

This is my setup:

popup.html

<button id="test1" onclick="getSite();">button 1</button>
<button id="test2" onclick="getSite();">button 2</button>

content_script.js

function getSite(){alert('getSite works!');}

I'm having trouble understanding how to use the chrome javascript api, as I see others saying use chrome.tabs.executeScript, but I can't figure out where that line goes. Can anyone help me? I'll give you a cookie! or just an upvote.. or maybe both?

3
  • Both answers below are correct. You cannot use inline event attributes, and you cannot reference variables in your content script from your browser action popup. Use addEventListener in a separate JS file, and use message passing via your background page. Commented May 8, 2012 at 15:42
  • Ohhh.. I think I just figured it out, but I'll test more before confirming. This is how you use chrome.tabs.executeScript Instead of just typing out alert('test'); you write chrome.tabs.executeScript(null, {code:"alert('test')"}); window.close(); Commented May 8, 2012 at 19:30
  • That is correct; you can also use {file:"some_file.js"} to inject a whole file. Each tab has an isolated execution evironment for extension scripts, and those are all separate from the background paage. executeScript is how the background page gets a particular tab to run code. Commented May 8, 2012 at 19:44

2 Answers 2

2

You haven't mentioned on which page you want your scripts to run onclick, in Popup.html page or the page on which user is currently working on the browser. If it is just the popup.html page in which you want to execute your script, include them in popup.html page itself.

If however you want to execute them on the user's browser page, You will have to pass a message to your background page, which in turn will execute chrome.tabs.executeScript with current tab's id and {file: 'yourjsfile.js'} as arguments.

Sign up to request clarification or add additional context in comments.

3 Comments

Trying this in popup.html: <script> function getSiteThree() { alert('test3'); } document.addEventListener('DOMContentLoaded', function () { document.getElementById('button3').addEventListener('click', getSiteThree); }); </script> in body: <button id="button3">button3</button> It's not running the function. But when I reference the same script in content_script.js, the pop up shows, albeit briefly. I'm sure I"m doing something wrong, so I'll keep trying this a little more, but if you see something obvious, please do let me know!
I've never used the event DOMContentLoaded. Can you try window.onload instead and check if it works for you?
Jophin Joseph has mentioned correctly with the link that you cannot use inline javascript in your popup.html. You will have to put all your js in a file and load it through <script> tag.
1

I think you are having this problem because of restrictions imposed by the Google Content Security Policy. It mentions that iniline javascript like the one that you have mentioned in you code will not be executed. Try removing the onclick="getSite()" from your HTML markup to content_script.js. Use addEventListener function to attach the event to the button.

1 Comment

I did it this way, and it seems to working, somewhat. The alert shows, but it fades away right away. I'm guessing I'll have to try it by passing the message to the background page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.