Created
December 11, 2014 23:06
-
-
Save robozevel/a48757b80e1ddd784e3a 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 require = (function() { | |
var rComments = /((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/mg; | |
var rArguments = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; | |
var getArguments = function(fn) { | |
// https://gist.github.com/thebyrd/6924190 | |
var argNames = fn.toString().replace(rComments,'').match(rArguments)[1]; | |
return argNames ? argNames.split(/,/) : []; | |
}; | |
return function(dependencies, fn) { | |
if (typeof dependencies === 'function') { | |
fn = dependencies; | |
dependencies = []; | |
} else if (!Array.isArray(dependencies)) { | |
dependencies = []; | |
} | |
chrome.runtime.getBackgroundPage(function(backgroundPage) { | |
fn.apply(null, getArguments(fn).map(function(dependency, i) { | |
return backgroundPage[dependencies[i] || dependency]; | |
})); | |
}); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment