Labs/Jetpack/JEP/19

From MozillaWiki
< Labs‎ | Jetpack‎ | JEP
Jump to: navigation, search
Draft-template-image.png THIS PAGE IS A WORKING DRAFT Pencil-emoji U270F-gray.png
The page may be difficult to navigate, and some information on its subject might be incomplete and/or evolving rapidly.
If you have any questions or ideas, please add them as a new topic on the discussion page.

JEP 19 - JavaScript Injection Toolkit

  • Champion: krukid <krukid at gmail dot com>
  • Status: Draft
  • Type: API Track
  • Created: 15 July 2009
  • Reference Implementation: jetpack.future.import("injection")
  • JEP Index

Introduction and Rationale

Sometimes, when tweaking a web page to our liking, all it really takes is a function call with the sole issue being that this function is defined by that very web page and is tightly bound into its logic. It is possible, however, to inject your code into the context of the page - a variable, an assignment, an object, a prototype modification or even a whole custom library and make it all interact with existing definitions and instances.

Proposal

This JEP is meant to provide the API for JavaScript injection into document context from a Jetpack feature.

The injection API will be accessible from jetpack.injection.

Injecting Code and Libraries

The basic injection capabilities are granted by JavaScript code and library injection functions.

Injecting Code

jetpack.injection.append( options )

Arguments

options: A dictionary containing key-value pairs representing JavaScript injection parameters. All properties are mandatory.

  • document (HTMLDocument) - target document to inject the code into;
  • script (string) - JavaScript code to inject.

Injecting a Library

This allows us to link a library to the target document, in order to access its functions.

jetpack.injection.addLib( options )

Arguments

options: A dictionary containing key-value pairs representing library injection parameters. All properties are mandatory.

  • document (HTMLDocument)
  • src (string) - URL of the library.

Example

In the following example we assume that the currently focused tab is displaying a page that defines some logic for its updateContent() function.

jetpack.injection.append({
	document: jetpack.tabs.focused.contentDocument,
	script: "updateContent()"
})