Skip to content

Instantly share code, notes, and snippets.

@nicdaCosta
Created November 27, 2012 20:46
Show Gist options
  • Save nicdaCosta/4156897 to your computer and use it in GitHub Desktop.
Save nicdaCosta/4156897 to your computer and use it in GitHub Desktop.
A basic interface for Developer Tools so that developers can register useful "snippets" that will thus be available for future use.
/*
* DevToolsAPI.js
* Author : Nic da Costa ( @nic_daCosta )
* Created : 2012/11/27
* Version : 0.1
* License : MIT, GPL licenses.
*
* Overview:
* script to possibly be included in DevTools.js ( Chrome Dev Tools ) to give a
* basic "interface" for developers to register scripts / functions for repeated use.
*
* This has been based upon Basket.js (http://addyosmani.github.com/basket.js/)
* and it's main concept and methods of caching scripts, big thanks to the team for all the initial work!
*
* Brief post explaining the below - https://plus.google.com/u/0/111523616060404197347/posts/75Hw6EMjYyD
*/
var DevToolsAPI = ( function( window , document , undefined) {
"use strict";
// define local copy of localStorage
var localStorage = window.localStorage;
var _devTools = function() {
/* is there a need for anything inside the constructor??
** contemplated putting calling init but then would be called
** should any dev create a new instace of DevToolsAPI and thus
** duplicates loaded
*/
};
_devTools.register = function( key , objectToRegister ) {
// check if key exists, if so, throw error to notify dev.
// An alternative is to add current date to key and register anyway. will be hard to then remove key
if ( key && localStorage.hasOwnProperty( key ) ) {
throw 'Key (' + key + ') Already Exists, Please Register Script With A Unique Key';
}
// set item for future use
localStorage.setItem( key , objectToRegister );
// load newly set item
appendScript( localStorage[ key ] );
};
_devTools.remove = function( key ) {
if( key && localStorage.hasOwnProperty( key ) ) {
localStorage.removeItem( key );
}
};
_devTools.clear = function() {
localStorage.clear();
};
_devTools.listAllRegisteredScripts = function() {
var allRegisteredScripts = {};
// itterate through all the keys, check if exists and return object;
Object.keys( localStorage ).forEach( function( key ) {
allRegisteredScripts[ key ] = localStorage[ key ];
});
return allRegisteredScripts;
};
// private methods
function init(){
// intial load, gets all existing "scripts" and loads them
Object.keys( localStorage ).forEach( function( key ) {
// load script
appendScript( localStorage[ key ] );
} );
}
function appendScript( scriptToAppend ) {
if ( localStorage[ scriptToAppend ] ) {
var script = document.createElement( 'script' );
script.defer = script.async = true;
script.textContent = scriptToAppend;
document.head.appendChild( script );
}
}
// initialise existing scripts
init();
// return "public" facing API
return _devTools;
} ( window , document ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment