Skip to content

Instantly share code, notes, and snippets.

@dascgo
Created March 21, 2011 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dascgo/880362 to your computer and use it in GitHub Desktop.
Save dascgo/880362 to your computer and use it in GitHub Desktop.
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