Skip to content

Instantly share code, notes, and snippets.

@robozevel
Created December 11, 2014 23:06
Show Gist options
  • Save robozevel/a48757b80e1ddd784e3a to your computer and use it in GitHub Desktop.
Save robozevel/a48757b80e1ddd784e3a to your computer and use it in GitHub Desktop.
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