Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

JEP Content scripts

Gozala edited this page May 24, 2012 · 4 revisions

Most of the Add-on SDK APIs are designed around content scripts which are not necessary easy to grok or work with. In addition we ended up adding set of content-script associated options to each API. In some cases it also maybe neat to have an ability to share content script instance in different APIs contexts like PageMod and ContextMenu for example.

Alternative

Instead of making each API take content script related options we probably could have an API to create a ContentScript instance instead:

let script = ContentScript({
  uri: contentScriptFile, // file to create content script from 
  // source: code, // Or an option to create from source itself.
});

// Set up listeners for `message` event for all scripts.
script.port.on('message', function(port, data) {
  // Reply back to the `port` message was received from. 
  port.emit('reply', compute(data));
});

// Use created content script in a page mod.
let mod = PageMod({
  include: "*.org",
  contentScript: script
});

// Execute content script in the context of currently active
// tab (Page.mod will use same `spawn` method).
script.spawn(tabs.activeTab);

// Execute content script in the context of a hidden page.
script.spawn(hiddenPage);

// Listen to content script spawns and get a communication
// port.
script.on('spawn', function(port) {
  port.emit('hello !!');
});

This will allow decoupling of content script logic from rest of the APIs and allow improvements in that area without changing each API individually.

Discussion

https://etherpad.mozilla.org/jetpack-content-scripts

Prior art