Last active
December 20, 2015 11:49
-
-
Save thom4parisot/6126549 to your computer and use it in GitHub Desktop.
Chrome Extension Channel Example
This file contains hidden or 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 process = new BackgroundProcess(); | |
process.requetChannelConfig(chrome.runtime.getURL("channel.json")); |
This file contains hidden or 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
{ | |
"channel": "dev" | |
} |
This file contains hidden or 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
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']); | |
}; |
This file contains hidden or 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
{ | |
"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 | |
} | |
} |
This file contains hidden or 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
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