Interaction between Firefox extension and page

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.