Skip to content

Instantly share code, notes, and snippets.

@davepagurek
Created September 20, 2014 05:04
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 davepagurek/de5e265f4bef89fced65 to your computer and use it in GitHub Desktop.
Save davepagurek/de5e265f4bef89fced65 to your computer and use it in GitHub Desktop.
js object
//Module to communicate between background and app scripts
var Messenger = (function() {
var m = {};
var responses = {};
m.current = chrome.runtime.id;
m.extension = "hkgihfhfbmabgamjpbgkkdjcmicmheap";
m.app = "neoalcniaoimlmhmibbccponldlhnnee";
m.other = (m.current==m.app)?m.extension:m.app;
//Post a message for other apps/extensions to hear
var sendExternal = function(id, action) {
chrome.runtime.sendMessage(id, {"action": action});
};
//Generates a random string of letters and numbers of the specified length to keep track of alarms
var makeID = function(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
//Post a message for scripts to hear
m.sendAction = function(action) {
chrome.runtime.sendMessage({"action": action});
sendExternal(m.other, action);
};
//Post a message for scripts to hear after a delay
m.sendActionDelay = function(action, delay) {
//Get an id for the alarm
var id = makeID(20);
chrome.alarms.create(id, {"delayInMinutes": delay});
//When an alarm goes off and its id matches the current one, send the action and remove this listener
chrome.alarms.onAlarm.addListener(function handler(alarm) {
if (alarm.name == id) {
m.sendAction(action);
chrome.alarms.onAlarm.removeListener(handler);
}
});
}
//Save a setting to the storage
m.saveSettings = function(settings, callback) {
if (m.current == m.app) {
chrome.storage.sync.set(settings, callback);
} else {
chrome.runtime.sendMessage(m.app, {"action": "set_storage", "settings": settings}, callback);
}
};
//Share storage between extension/app
m.getStorage = function(keys, response) {
//If this is the app, get the storage
if (m.current == m.app) {
chrome.storage.sync.get(keys, response);
//If this is the extension, ask the app to get the storage
} else {
chrome.runtime.sendMessage(m.app, {"action": "get_storage", "keys": keys}, response);
}
};
//Let other modules register themselves to be notified when an event comes in
m.addListener = function(action, response) {
//If there's nothing listening for that action, make a list for it
if (!responses[action]) {
responses[action] = [response];
} else {
//Add it to the existing list if it isn't already there
for (var i=0; i<responses[action].length; i++) {
if (responses[action][i] == response) return;
}
responses[action].push(response);
}
};
m.removeListener = function(action, response) {
//Find and remove the response from the list
if (responses[action]) {
for (var i=0; i<responses[action].length; i++) {
if (responses[action][i] == response) {
responses[action].splice(i, 1);
return;
}
}
}
};
//Create a notification and play a sound
m.notify = function(text) {
//Only the app has notification permissions, so only the app can use this
if (m.current == m.app) {
chrome.notifications.create("",
{
"type": "basic",
"iconUrl": "/icons/icon-128.png",
"title": "SpurStart",
"message": text
}, function() {});
var audio = new Audio("/sounds/notification.mp3");
audio.play();
}
}.bind(this);
//Start listening for incoming messages
m.init = function() {
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
//If there are responses for this action, run them all
if (request.action && responses[request.action]) {
for (var i=0; i<responses[request.action].length; i++) {
(responses[request.action][i])(request, sender);
}
}
});
//When receiving a message from another component (app or extension)
chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) {
//If we have been asked for storage by the extension
if (m.current == m.app) {
if (request.action && request.action=="get_storage" && request.keys) {
//Get storage and return
chrome.storage.sync.get(request.keys, function(items) {
sendResponse(items);
});
return true;
} else if (request.action && request.action=="set_storage" && request.settings) {
//Set storage and return
chrome.storage.sync.set(request.settings, sendResponse({"storage_response": "done"}));
return true;
}
}
//If there are responses for this action, run them all
if (request.action && responses[request.action]) {
for (var i=0; i<responses[request.action].length; i++) {
(responses[request.action][i])(request, sender);
}
}
});
//Log errors for debug purposes
window.onerror = function(msg, url, linenumber) {
console.log("Error: " + msg + "\nurl: " + url + "\nLine number: " + linenumber);
};
};
return m;
}());
Messenger.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment