Created
September 24, 2014 04:01
-
-
Save RichardLitt/cc88bbc5a6dd8d44900d to your computer and use it in GitHub Desktop.
This file contains 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 fs = require('fs'); | |
var _ = require('underscore'); | |
// Init | |
var sidebarOpen = false; | |
// TODO Enable Static Assets to go to other Views besides SideBar | |
function buildStaticAssets(modules){ | |
// Init | |
var sidebar = document.createElement('div'); | |
sidebar.id = "beagle-sidebar"; | |
console.log(modules); | |
// Read required modules | |
// These are stored in ... local storage? | |
// Grab CSS and HTML files from required modules | |
// TODO Concat CSS files | |
// Add in CSS | |
sidebar.innerHTML = '<style>' + | |
fs.readFileSync(__dirname + '/main.css', 'utf8') + '</style>'; | |
// TODO Concat HTML files | |
// Add in HTML | |
sidebar.innerHTML += fs.readFileSync(__dirname + '/sidebar.html', 'utf8'); | |
return sidebar; | |
} | |
// This function will write a manifest to local storage | |
function setManifest(modules) { | |
} | |
// Handle requests from background.html | |
function handleRequest( | |
// The object data with the request params | |
request, | |
// These last two ones isn't important for this example. | |
// If you want know more about it visit: | |
// http://code.google.com/chrome/extensions/messaging.html | |
sender, sendResponse | |
) { | |
if (request.callFunction == "toggleSidebar") { | |
// If the extension has specified new modules to load | |
var newModules = request.modules ? request.modules : null ; | |
//Get the current list of used modules | |
var modules = chrome.storage.sync.get('modules', function(obj){ | |
console.log('obj', obj); | |
return obj; | |
}); | |
console.log('modules', modules); | |
// If there are new modules, append them | |
if (newModules) { | |
_.each(newModules, function(module) { | |
modules[module] = true; | |
}); | |
// If there is a change, set it. | |
chrome.storage.sync.set(modules); | |
} | |
// Continue building out the view | |
buildView(modules); | |
} | |
} | |
chrome.extension.onRequest.addListener(handleRequest); | |
function buildView(modules) { | |
if (sidebarOpen) { | |
var el = document.getElementById('beagle-sidebar'); | |
el.parentNode.removeChild(el); | |
sidebarOpen = false; | |
} else { | |
var sidebar = buildStaticAssets(modules); | |
document.body.appendChild(sidebar); | |
sidebarOpen = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment