Created
March 21, 2011 22:25
-
-
Save dascgo/880362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var btn_id = "my_addon_btn"; | |
// create a button and attach to DOM: | |
// but only if it isnt there already... | |
if(!document.getElementById(btn_id)){ | |
var btn = document.createElement("button"); | |
btn.id = btn_id; | |
btn.innerHTML = "Click Me"; | |
// add an event listener that will call the btnClick func | |
btn.addEventListener("click", btnClick, false); | |
// add to the tab's DOM | |
document.body.appendChild(btn); | |
} | |
// handler for the button's click event | |
function btnClick(){ | |
// The button was click so tell main.js | |
postMessage({ | |
"type": "click", | |
"msg": "You click on the button!" | |
}); | |
} | |
// handle messages from main.js to here.... | |
onMessage = function onMessage(data) { | |
if(data.type == "click_complete"){ | |
console.log("you clicked a button in the dom"); | |
console.log("we told main.js about it"); | |
console.log("and this is main.js's response:"); | |
console.log(" - " + data.msg); | |
}else{ | |
console.log("unknown message type: " + data.toSource()); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment