Skip to content

Instantly share code, notes, and snippets.

@nelix
Created November 27, 2012 02:13
Show Gist options
  • Select an option

  • Save nelix/4151970 to your computer and use it in GitHub Desktop.

Select an option

Save nelix/4151970 to your computer and use it in GitHub Desktop.
Why not just make the API magically more like the way I want to write things.
var workers = [];
PageMod({
include: "*",
contentScriptWhen: 'end',
contentScriptFile: data.url("content.js"),
attachTo: ["top", "existing"],
onAttach: function(worker) {
workers.push(worker);
worker.on('detach', function () {
log('worker detach');
detachWorker(this, workers);
});
}
)};
function detachWorker(worker, workerArray) {
var index = workerArray.indexOf(worker);
if(index != -1) {
workerArray.splice(index, 1);
}
}
function getWorker(tab, workerArray) {
var _;
function isTab(worker) {
if (worker.tab === tab) _ = worker;
}
workerArray.forEach(isTab);
return _;
}
function emitMessage(worker, eventName, eventData){
var message = {
id: Math.random(),
name: eventName,
detail: eventData
};
worker.postMessage(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment