Skip to content

Instantly share code, notes, and snippets.

@thom4parisot
Last active December 20, 2015 11:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thom4parisot/6126549 to your computer and use it in GitHub Desktop.
Save thom4parisot/6126549 to your computer and use it in GitHub Desktop.
Chrome Extension Channel Example
var process = new BackgroundProcess();
process.requetChannelConfig(chrome.runtime.getURL("channel.json"));
{
"channel": "dev"
}
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
manifest: grunt.file.readJSON('src/manifest.json'),
zip: {
extension: {
cwd: 'src/',
src: [
'src/**/*',
'!src/channel.json'
],
dest: "dist/<%= pkg.name %>-<%= manifest.version %>.zip",
dot: false
}
}
});
grunt.loadNpmTasks('grunt-zip');
grunt.registerTask('default', ['zip']);
};
{
"name": "Channel Extension",
"version": "0.0.1",
"manifest_version": 2,
"description": "Sample example of a Chrome Extension channel implementation",
"background": {
"scripts": [
"process.js",
"background.js"
],
"persistent": true
}
}
function BackgroundProcess(){
this.channel = "production";
}
BackgroundProcess.prototype.requetChannelConfig = function requetChannelConfig(url){
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.addEventListener("load", this.channelResponseHandler.bind(this));
xhr.send();
};
BackgroundProcess.prototype.channelResponseHandler = function channelResponseHandler(progressEvent){
var channelConfig = JSON.parse(progressEvent.target.responseText);
this.channel = channelConfig.channel;
if (typeof this[this.channel+'Setup'] === "function"){
this[this.channel+'Setup'](channelConfig);
}
};
BackgroundProcess.prototype.devSetup = function devSetup(config){
chrome.browserAction.setIcon({
"path": chrome.extension.getURL("src/resources/icon-dev.png")
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment