Skip to content

Instantly share code, notes, and snippets.

@brunobar79
Created March 12, 2012 15:20
Show Gist options
  • Save brunobar79/2022590 to your computer and use it in GitHub Desktop.
Save brunobar79/2022590 to your computer and use it in GitHub Desktop.
background.js extension with browser button
/************************************************************************************
This is your background code.
For more information please visit our wiki site:
http://crossrider.wiki.zoho.com/Background-Code.html
*************************************************************************************/
//Place your code here (ideal for handling toolbar button, global timers, etc.)
/*****************
This background code is showing how to interact with the browser button.
In Firefox you have two main options:
1. setting onClick event
2. change the currently displayed icon
In Chrome you have more options, that includes:
1. decide whether to work with popup window or set onClick event (you can change it programmatically but to work with popup window you must set it in advance at the "Browser" tab)
2. change the currently displayed icon
3. add badge text to the button icon + set the badge color
4. set button's tooltip
*****************/
//listen to incoming messages
appAPI.message.addListener(function(msg) {
//broadcase the message we've got to all tabs!
if(msg.action=='enable'){
enabledButton();
}else if(msg.action=='disable'){
disabledButton();
}
if(typeof(msg["event_name"]) === "string" && msg.event_name == "onTabSelection") {
// Call the user callback
//callback(msg);
if(msg["show_button"]=="yes"){
enabledButton();
}else{
disabledButton();
}
}
});
// Listen to on tab selection event
/*
onTabSelection(function(msg) {
// Don't use alert in here, since alert make the window lose focus, and get the focus after you click "OK"
if(msg.show_button){
}else{
}
});*/
function enabledButton(){
if (appAPI.platform == "CH")
{
appAPI.chrome.browserAction.setIcon(2);
appAPI.chrome.browserAction.setTitle("Click here to check-in");
}
if (appAPI.platform == "FF")
{
appAPI.firefox.browserAction.setIcon(2);
appAPI.firefox.browserAction.setTitle("Click here to check-in");
}
}
function disabledButton(){
if (appAPI.platform == "CH")
{
appAPI.chrome.browserAction.setIcon(1);
appAPI.chrome.browserAction.setTitle("Check-in not available");
}
if (appAPI.platform == "FF")
{
appAPI.firefox.browserAction.setIcon(1);
appAPI.chrome.browserAction.setTitle("Check-in not available");
}
}
function buttonClick() {
if (appAPI.platform == "CH")
{
//set the tooltip of the button
appAPI.chrome.browserAction.setTitle("chkin.at");
}
//this app changes the button icon on each click.
if (appAPI.platform == "FF")
{
appAPI.firefox.browserAction.setTitle("chkin.at");
}
appAPI.message.toActiveTab({action:"toggle"});
//NOTE: all JS and API functions are identical for all platforms with one exception of browser specific API's such as toolbar button (as each browser has different capabilities)
}
if (appAPI.platform == "FF") {
//set the browser button onClick function for Firefox
appAPI.firefox.browserAction.onClick(buttonClick);
//Firefox doesn't put the icon by default so we need to "call it" - setIcon with no param is like setIcon(1)
appAPI.firefox.browserAction.setIcon();
}
//set the browser button onClick function for Chrome
if (appAPI.platform == "CH") appAPI.chrome.browserAction.onClick(buttonClick);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment