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

From MozillaWiki
< Labs‎ | Jetpack‎ | Reboot‎ | JEP
Jump to navigation Jump to search
Line 29: Line 29:
<pre class="brush:js;">
<pre class="brush:js;">
var myMods = new pageMod({
var myMods = new pageMod({
   'includes': ['*.google.com', 'jetpack.mozillalabs.com'],
   'include': ['*.google.com', 'jetpack.mozillalabs.com'],
   'excludes': ['https://mail.google.com/*'],
   'exclude': ['https://mail.google.com/*'],
   'files': {
   'style': [
      'javascript': ['http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools.js'],
    'http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css',
      'stylesheet': ['http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css']
     'body { background: #ffffff; font-family: Trebuchet MS; } span.details { font-size: 7px; }'
  },
  'styles': [
     'body { background: #ffffff; font-family: Trebuchet MS; }',
    'span.details { font-size: 7px; }'
   ],
   ],
   'scripts': [function(){
   'script': [
    'http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools.js',
    function(){
       $('container').addEvents({
       $('container').addEvents({
           'click:relay(ul li)': function(){ this.setStyles('background','#000') },
           'click:relay(ul li)': function(){ this.setStyles('background','#000') },
Line 54: Line 52:
<b>Arguments:</b>
<b>Arguments:</b>


#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'includes', 'excludes', 'files', 'styles', or 'scripts'. If the only argument passed in is an object, Page Mods assumes multiple modifiers will be passed in.
#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'include', 'exclude', 'style', or 'script'
#<b>data</b> -
#<b>data</b> -
#* includes: (<i>array</i>) an array of URL strings to apply mods to
#* include: (<i>array</i>) an array of URL strings to apply mods to
#* excludes: (<i>array</i>) an array of URL strings to skip when modding
#* exclude: (<i>array</i>) an array of URL strings to skip when modding
#* files: (<i>object</i>) 'javascript' or 'stylesheet' keyed nodes whose values are arrays of asset resource strings
#* style: (<i>array</i>) an array of style or resource strings
#* styles: (<i>array</i>) an array of styles, wherein each array item is a selector and contained within are properties and values.
#* script: (<i>function</i>) an array of functions or resource strings
#* scripts: (<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>Returns:</b>
<b>Returns:</b>
Line 68: Line 65:
<b>Notes:</b>
<b>Notes:</b>


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.
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 'include' white-list.


<b>Examples:</b>
<b>Examples:</b>


<pre class="brush:js;">
<pre class="brush:js;">
myMods.add('includes', ['*.digg.com']);
myMods.add('include', ['*.digg.com']);


myMods.add('excludes', ['http://labs.digg.com/*']);
myMods.add('exclude', ['http://labs.digg.com/*']);


myMods.add('files', {
myMods.add('style', ['body: { background: #ffffff; font-family: Trebuchet MS; } span.details { font-size: 7px; }']);
  'javascript': ['http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools.js'],
  'stylesheet': ['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
// Creating a function reference
Line 90: Line 82:
}
}


// Using a function reference enables removal of the script later, see
myMods.add('script', pageLog);
// the remove method below for more info.
 
myMods.add('scripts', pageLog);
</pre>
</pre>


==== Page Mods Method: <i>remove</i> ====
==== FUTURE ADDITION: Page Mods Method: <i>remove</i> ====


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


#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'includes', 'excludes', 'files', 'styles', or 'script'. NOTE: If the only argument is an object, Page Mods assumes multiple modifiers will be passed in.
#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'include', 'exclude', 'style', or 'script'
#<b>data</b> -
#<b>data</b> -
#* includes: (<i>array</i>) an array of URL strings (removal reloads modded docs)
#* include: (<i>array</i>) an array of URL strings to apply mods to
#* excludes: (<i>array</i>) an array of URL strings (removal applies mods to docs if any are open)
#* exclude: (<i>array</i>) an array of URL strings to skip when modding
#* files: (<i>object</i>) 'javascript' or 'stylesheet' keyed-nodes, whose values are arrays of asset resource strings. Passing string of 'all' for the value of either will remove all files of that type.
#* style: (<i>array</i>) an array of style or resource strings
#* styles: (<i>array</i>) an array of styles, wherein each array item can be a selector name only (to remove all styles related to the selector) or additionally, can include specific property names (removing only styles of that property while leaving unmentioned selector styles intact).
#* script: (<i>function</i>) an array of functions or resource strings
#* scripts: (<i>function</i>) removes the script associated with a function reference


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


The Page Mods instance
The Page Mods instance
<b>Notes:</b>
NEEDS FEEDBACK: Removal of any 'includes', 'excludes', 'scripts', or 'files' of the keyed node-type 'javascript' will prompt the user asking whether or not to perform a reload for pages that contain, or are affected by, the modification's removal.


<b>Examples:</b>
<b>Examples:</b>
Line 120: Line 104:
<pre class="brush:js;">
<pre class="brush:js;">


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


myMods.remove('excludes', ['http://labs.digg.com/*']);
myMods.remove('exclude', 'http://labs.digg.com/*');


myMods.remove('files', {
myMods.remove('style', [
  'javascript': 'all',
  'stylesheet': ['http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css']
});
 
myMods.remove('styles', [
   'body { background; font-family; }',
   'body { background; font-family; }',
   'span.details'  //This would remove all styles for the selector
   'span.details'  //This would remove all styles for the selector
Line 138: Line 117:
</pre>
</pre>


==== Page Mods Method: <i>empty</i> ====
==== FUTURE ADDITION: Page Mods Method: <i>empty</i> ====


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


#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'includes', 'excludes', 'files', 'styles', or 'scripts'. NOTE: If the only argument is an object, Page Mods assumes multiple modifiers will be passed in.
#<b>type</b> - (<i>string</i>) The type of modifier being added. Can be 'include', 'exclude', 'style', or 'script'


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


The Page Mods instance
The Page Mods instance
<b>Notes:</b>
NEEDS FEEDBACK: Emptying of 'includes', 'excludes', 'scripts', or 'files' of the node-type 'javascript' will prompt the user asking whether or not to perform a reload for pages that contain, or are affected by, the modification's removal.


<b>Examples:</b>
<b>Examples:</b>


<pre class="brush:js;">
<pre class="brush:js;">
myMods.empty('includes');
myMods.empty('include');
</pre>
</pre>



Revision as of 01:24, 23 February 2010

JEP 107 - Page Mods

  • Champion: Daniel Buchner - daniel@mozilla.com
  • Status: Accepted/Pre-Production
  • Bug Ticket: 546739
  • Type: API
  • Difficulty: 4

Proposal

Page Mods is chiefly aimed at modifying and manipulating content documents within Firefox. Pages Mods should perform this functionality in a seamless fashion where the end result is the only change visible to the user.

Key Issues

When documents load that are represented in the "matches" white-list, we must ensure that script and styles injected into the page are evaluated by the parser in the normal load cycle. Perhaps we achieve this by dynamically creating resource (or custom protocol) URIs that contain the style and script blocks entered similar to the Background Page mechanisms. These URIs would be dynamically created CSS/JS files that would then be injected in the appropriate places withing the matched documents (CSS files in the doc head, JS files just after the close of the body tag).

Dependencies & Requirements

  • Requires JEP 104 - Simple Storage
  • Ability to dynamically create a resource, such a method would require JEP 106 - Registered Jetpack URLs
  • Capturing the page pre-render and injecting resources, for example: linked style sheets in the header and script tags after the body
  • Ability to access extension specific resources

Internal Methods

API Methods

Page Mods Initialization

var myMods = new pageMod({
  'include': ['*.google.com', 'jetpack.mozillalabs.com'],
  'exclude': ['https://mail.google.com/*'],
  'style': [
    'http://yui.yahooapis.com/3.0.0/build/cssbase/base-min.css',
    'body { background: #ffffff; font-family: Trebuchet MS; } span.details { font-size: 7px; }'
  ],
  'script': [
    'http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools.js',
    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') }
      });
    }
  ]
});

Page Mods Method: add

Arguments:

  1. type - (string) The type of modifier being added. Can be 'include', 'exclude', 'style', or 'script'
  2. data -
    • include: (array) an array of URL strings to apply mods to
    • exclude: (array) an array of URL strings to skip when modding
    • style: (array) an array of style or resource strings
    • script: (function) an array of functions or 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 'include' white-list.

Examples:

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

myMods.add('exclude', ['http://labs.digg.com/*']);

myMods.add('style', ['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!');
}

myMods.add('script', pageLog);

FUTURE ADDITION: Page Mods Method: remove

Arguments:

  1. type - (string) The type of modifier being added. Can be 'include', 'exclude', 'style', or 'script'
  2. data -
    • include: (array) an array of URL strings to apply mods to
    • exclude: (array) an array of URL strings to skip when modding
    • style: (array) an array of style or resource strings
    • script: (function) an array of functions or resource strings

Returns:

The Page Mods instance

Examples:


myMods.remove('include', '*.google.com');

myMods.remove('exclude', 'http://labs.digg.com/*');

myMods.remove('style', [
  'body { background; font-family; }',
  'span.details'  //This would remove all styles for the selector
]);

myMods.remove('script', pageLog);

FUTURE ADDITION: Page Mods Method: empty

Arguments:

  1. type - (string) The type of modifier being added. Can be 'include', 'exclude', 'style', or 'script'

Returns:

The Page Mods instance

Examples:

myMods.empty('include');

Use Cases

  1. Creation of CSS-based add-ons like Stylish, EditCSS, etc...
  2. Creation of JS-based add-ons like Execute JS, JS Exec etc...
  3. In General: Any Greasemonky-style add-on, with the advantage that this API would allow for far greater flexibility - turning on and off only certain parts of a mod, automatically flashing a new url/web-page with the active parts of a mod by using the add method to include a new match to the matches white-list

Common Actions

The API, if done in this fashion, give the developer the ability to dramatically simplify application actions such as:

  • Creating an instance of Page Mods that adds script or styles to a set of matched urls
  • Further extending and existing instance of Page Mods with additional styles and script
  • Toggling on and off specific styles or script within a Page Mods instance
  • Adding new matches to a Page Mods instance, which in turn instantly applies active styles and script within that instance to the newly added matches.
  • Multiple instances of Page Mods can be instantiated, which enables a whole cadre of functionality that the object-bound 'singleton' implementation neglects.