I am having a hard time understanding how to pass information, most specifically values of an HTML element with a specific ID, in Chrome extensions I've tried going through the extension documentation and that has barely helped me.
Ultimately, the extension I'm trying to build will read a specific page which dynamically generates forms. The form has an <input id="foo">
tag; when the <input>
is generated I would like the content script to pass a message containing the text contained within another <div id="bar">
on the same page to the background page. Once at the background page, the text will be parsed and a switch statement will create a div underneath the input and color it either red or green and fill with some text.
For now, however, I'm struggling with message passing and how to determine what my content script is actually doing. At this point, can someone tell me how I can simply a send a message to the background page once a button is clicked in the user's tab and execute an alert() with the button's id?
Here is what I have so far:
manifest.json
{
"manifest_version": 2,
"name": "Authinator",
"description": "extension",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"browser_action":{
"default_icon": "icon.png"
},
"permissions":[
"tabs",
"activeTab",
"https://ajax.googleapis.com/"
],
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*"],
"js": [ "auth.js", "jquery.min.js"]
}]
}
background page (background.js):
//alerts when extension icon clicked
chrome.browserAction.onClicked.addListener(function(tab) { alert('icon clicked')});
//trying to alert when button is clicked
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
alert(response);
})
content script (auth.js):
var someButton = document.findElemendById("foo");
someButton.addEventListener("click", function(button){
chrome.runtime.sendMessage(button.id);
})
Any help or direction here would be much appreciated. Thanks guys!