There are two ways to expand in Firefox: Extension and plugin.
Extension is an additional component of Firefox. It can change the existing functions of Firefox and add some new functions. extension is lighter than plug-in. It uses JavaScript and XUL (XML in disguise). Plug-in is an independent application. Firefox interacts with specific APIs to expand the functions of the browser.
Firefox is the Mozilla kernel and its own engine is mainly implemented through the C language, but the browser interface and a series of operations on it are all implemented through the combination of JS and XUL. Because extension uses the same principle, it can be seen as a natural extension of FF on the browser interface and small functions.
Because the extension functions in extension are implemented through JS, and in the pages loaded by browsers, we often need to call extension functions to use extension functions, however, extension is invisible in the scope of the document where the Web is located. To achieve interaction between the web page and extension, Firefox uses event listening to complete this function.
First, listen to specific user DOM events in the extension JS file.
Function mylistener (EVT) {alert ("stored ed from web page:" + evt.tar get. getattribute ("attribute1") + "/" + evt.tar get. getattribute ("attribute2");} document. addeventlistener ("myextensionevent", function (e) {mylistener (e) ;}, false, true ); // The last value is a Mozilla-specific value to indicate Untrusted content is allowed to trigger the event.
Then distribute the corresponding events in the web page.
VaR element = document. createelement ("myextensiondataelement"); element. setattribute ("attribute1", "foobar"); element. setattribute ("attribute2", "Hello World" contains multiple document.doc umentelement. appendchild (element); var EVT = document. createevent ("events"); EVT. initevent ("myextensionevent", true, false); element. dispatchevent (EVT );
In this way, the corresponding functions of trigger extension can be found in all web pages, and the active interaction between web pages on extension is completed.