Labs/Jetpack/Reboot/JEP/108: Difference between revisions

From MozillaWiki
< Labs‎ | Jetpack‎ | Reboot‎ | JEP
Jump to navigation Jump to search
Line 32: Line 32:
* Functional context for the function passed to the background page can be passed as an option
* Functional context for the function passed to the background page can be passed as an option


==== Page Mods <i>Initialization</i> ====
==== Background Page Global: <i>$dom</i> ====




<pre class="brush:js;">
==== Background Page Method: <i>run</i> ====
var myMods = new pageMod({
  'matches': ['*.google.com', 'jetpack.mozillalabs.com'],
  'includes': {
      'script': ['http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools.js'],
      'styles': ['http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css']
  },
  'styles': {
      'body': {
          'background': '#ffffff',
          'font-family': 'Trebuchet MS'
      }
  },
  'script': function(){
      $('container').addEvents({
          'click:relay(ul li)': function(){ this.setStyles('background','#000') },
          'mousenter:relay(ul li)': function(){ this.tween('background','#000') },
          'mouseleave:relay(ul li)': function(){ this.tween('background','#fff') }
      });
  }
});
</pre>
 
==== Page Mods Method: <i>add</i> ====


<b>Arguments:</b>
<b>Arguments:</b>


#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'matches', 'includes', 'styles', or 'script'. If the only argument passed in is an object, Page Mods assumes multiple modifiers will be passed in.
#<b>options</b> - (<i>object</i>)
#<b>data</b> -
#* matches: (<i>array</i>) an array of web site resource strings
#* matches: (<i>array</i>) an array of web site resource strings
#* includes: (<i>object</i>) 'script' or 'style' keyed nodes whose values are arrays of asset resource strings
#* includes: (<i>object</i>) 'script' or 'style' keyed nodes whose values are arrays of asset resource strings
#* styles: (<i>object</i>) an object whose keys are CSS selectors and whose values are objects composed of CSS property keys and values
#* styles: (<i>object</i>) an object whose keys are CSS selectors and whose values are objects composed of CSS property keys and values
#* script: (<i>function</i>) a function that will be injected into the page and called. By default, the function is bound with the target document's window object
#* script: (<i>function</i>) a function that will be injected into the page and called. By default, the function is bound with the target document's window object
#<b>executable</b> - (<i>array</i>) an array of web site resource strings


<b>Returns:</b>
<b>Returns:</b>

Revision as of 21:13, 5 February 2010

JEP 108 - Background Page

  • Champion: Daniel Buchner - daniel@mozilla.com
  • Status: Accepted/In-Queue
  • Bug Ticket:
  • Type: API


Proposal

Background Pages are like a more open and free sandbox for doing most anything in a traditional web page with a DOM context, but with a key difference: the page is augmented with escalated, waterfall chrome privileges. It is essentially along the same line of a Web Worker, just more open and accessible.

Key Issues

Dependencies & Requirements

  • We must be able to give the code in the top window of this page chrome privileges that are strictly one-way/descending in nature.
  • Dynamically generated code sent to the Background Page should be injected and executed in the Background Page's window context.


Internal Methods

  • TBD

API Methods

$dom.run({ interval: 10000, bind:$tabs.get('index', 0) }, function(){
    return window.location;
});
  • Background Page work units are bound with the Background Page's window object by default
  • If interval option is present, the function is wrapped with setInterval(); and the $Moz.background method returns the interval timer so that it may be cleared outside of the Background Page.
  • Functional context for the function passed to the background page can be passed as an option

Background Page Global: $dom

Background Page Method: run

Arguments:

  1. options - (object)
    • matches: (array) an array of web site resource strings
    • includes: (object) 'script' or 'style' keyed nodes whose values are arrays of asset resource strings
    • styles: (object) an object whose keys are CSS selectors and whose values are objects composed of CSS property keys and values
    • script: (function) a function that will be injected into the page and called. By default, the function is bound with the target document's window object
  2. executable - (array) an array of web site resource strings

Returns:

The Page Mods instance

Notes:

Modifications passed to the Page Mods instance with this method will be added, and persist, on open and future documents matching all of the URL(s) currently on the 'matches' white-list.

Examples:

myMods.add('matches', ['*.digg.com']);

myMods.add('includes', { 
  'script': ['http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools.js'],
  'styles': ['http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css']
});

myMods.add('styles', {
  'body': {
      'background': '#ffffff',
      'font-family': 'Trebuchet MS'
  },
  'span.details': {
      'font-size': '7px'
  }
});

// Creating a function reference

var pageLog = function(){
  console.log('I just logged a message with Page Mods!');
}

// Using a function reference enables removal of the script later, see
// the <i>remove</i> method below for more info.

myMods.add('script', pageLog);

Page Mods Method: remove

Arguments:

  1. type - (string) The type of modifier being added. Can be 'matches', 'includes', 'styles', or 'script'. NOTE: If the only argument is an object, Page Mods assumes multiple modifiers will be passed in.
  2. data -
    • matches: (array) an array of web site resource strings
    • includes: (object) 'script' or 'style' keyed nodes whose values are arrays of asset resource strings. Passing string of 'all' for the value of either 'include' key of 'styles' or 'script' will remove all includes of that type.
    • styles: (object) expects object nodes keyed with CSS selector strings, whose values are arrays of the CSS property strings you would like to remove
    • script: (function) removes the script associated with a function reference

Returns:

The Page Mods instance

Notes:

NEEDS FEEDBACK: Removal of any 'matches', 'script', or 'includes' of the keyed node type 'script' will reload any documents that contain, or are affected by, the modification's removal.

Examples:


myMods.remove('matches', ['*.google.com']);

myMods.remove('includes', {
  'script': 'all',
  'styles': ['http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css']
});

myMods.remove('styles', {
  'body': ['background', 'font-family'],
  'span.details': ['font-size']
});

myMods.remove('script', pageLog);

Page Mods Method: empty

Arguments:

  1. type - (string) The type of modifier being added. Can be 'matches', 'includes', 'styles', or 'script'. NOTE: If the only argument is an object, Page Mods assumes multiple modifiers will be passed in.

Returns:

The Page Mods instance

Notes:

NEEDS FEEDBACK: Emptying of 'matches', 'script', or 'includes' will reload any documents that contain, or are affected by, the modification's removal.

Examples:

myMods.empty('includes');