Labs/Jetpack/JEP/19
Contents
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()" })