Labs/Jetpack/JEP/17

From MozillaWiki
< Labs‎ | Jetpack‎ | JEP
Jump to: navigation, search

JEP 17 - Page Mods

  • Champion: Aza Raskin <aza at mozilla dot com>
  • Implementer: David Dahl <ddahl at mozilla dot com>
  • Status: Implementing
  • Type: API Track
  • Created: 26 May 2009
  • Reference Implementation: None
  • Relevant Bugs: bug 501259
  • JEP Index

Introduction and Rationale

This JEP describes the syntactic sugar that makes it really easy to run code that modifies a page. It makes Greasemonkey-esque functionality easy to implement.

Proposal

Content scripts will live at jetpack.pageMods

Adding a new page modifier

jetpack.pageMods.add( callback, options );

Arguments:

  • callback: A function that modifies the page. It takes one argument, the document object of the page being loaded.
  • options:
    • matches: An array of match patterns describing which pages the content script should run on. If any of the match patterns matches a URL, the content script is run.

Examples

Blacklist Example
jetpack.future.import("pageMods");

var callback = function(document){
  // check the current time if it is between 9 and 5
  // 'blacklist' the sites in options.matches
  var currentTime;
  var currentHour;
  currentTime = new Date();
  currentHour = currentTime.getHours();
  if (currentHour > 8 && currentHour < 17){
    document.title = "This site is blacklisted. Get some work done!";
    $(document).find("body").css({border:"3px solid #000000"});
    $(document).find("body").children().hide();
    $(document).find("body").prepend($('<h1>Sorry this site is blacklisted until 17:00. sadface.</h1>'));
  }

};

var options = {};
options.matches = ["http://*.reddit.com/*",
                   "http://*.cnn.com/*",
                   "http://*.bbc.co.uk/*",
                   "http://*.dpreview.com/*",
                   "http://dpreview.com/*",
                   "http://*.bloglines.com/*",
                   "http://bloglines.com/*"];
jetpack.pageMods.add(callback, options);
The above code will call the callback function for each loaded page whose document.location matches any of the patterns in
options.matches

The callback checks the time and makes the page unreadable by removing all content from the page, and adding a reminder to get back to work.