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?
addEventListenerin a separate JS file, and use message passing via your background page.chrome.tabs.executeScriptInstead of just typing outalert('test');you writechrome.tabs.executeScript(null, {code:"alert('test')"}); window.close();{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.executeScriptis how the background page gets a particular tab to run code.