Chrome extension mechanism

Source: Internet
Author: User

It is said that starting in September this year, Google will completely disable the NPAPI plugin in Chrome browser, Chrome 45 will no longer be able to load the Npapi plugin, and introduced a new mechanism: extension.

In fact, if the browser as a canvas, the NPAPI plug-in is like hanging on the canvas of a variety of small ornaments, and the extension is more like stitching on the outside of the canvas small puzzles. Both plug-ins and extensions are meant to make this picture more consistent with user expectations. Here is just a brief introduction to the extension and native message mechanism.

Extending the installation package

A complete expansion pack will eventually be a. CRX compressed file package exists, using the Zip decompression file can be opened directly.

There are two ways to expand the installation:

1) in the Google APP store, find the relevant extension and click Add directly

2) in the Chrome browser address bar, enter chrome://extensions, open the Extension management page;

Then the CRX file, dragged directly into the good.

If the extension is not able to catch a line with the browser, then the extension is really a small piece of puzzle that happens to be placed on the side of the browser.

There are two ways to extend communication between browsers:

1) Short Connection

Send message: Chrome.runtime.sendMessage

Receive event: Chrome.runtime.onMessage.addListener

2) Long Connection

Send message: var port = Chrome.runtime.connect

Port.postmessage

Receive event: Port.onMessage.addListener

At some point, only the extension does not achieve the effect we expect, this time we need to introduce the host side. Viewed from the browser, it is: Browser <-----> Extensions <------>host side. That is to say: browsers and extensions can communicate with each other, the extension and the host side can communicate with each other, but the host side and the browser cannot communicate directly. The example of Chrome gives the demo, which is also a structure.

To extend communication with the host side, you need to follow the native message mechanism. In order to implement the native message mechanism for communication, you need to be prepared in advance:

1) host side, extended

2) The host side of the JSON file, in this file defines the name of the host application, and for which the extension ID is bound. Only the binding here, is legitimate, is to be able to communicate.

3) The host side of the JSON file, since it is so important, then how can the browser find this file?

The Windows platform is written in the registry, and the Mac is placed under the specified directory.

Windows platform: hkey_local_machine\software\google\chrome\nativemessaginghosts\com.my_company.my_application or

hkey_current_user\software\google\chrome\nativemessaginghosts\com.my_company.my _application

Mac Platform:/Library/Google/Chrome/NativeMessagingHosts/com.my_company.my_application.json 或

          ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/com.my_company.my_application.json

There are two forms of communication for the Native message mechanism:

1) Short Connection

Send message: Chrome.runtime.sendNativeMessage

2) Regular Connection

Send message: var port = chrome.runtime.connectNative

Port.postmessage

Receive event: Port.onMessage.addListener

Port.onDisconnect.addListener

Regarding the native message mechanism, it is important to note

1) Extend the communication with the host side, in fact understanding can be for the standard input and output interface based process communication. In the host side, you must read and write as a binary stream.

2) from the extension to the host side, the maximum number of messages is not more than 4GB

From the host side to the extended, a single message maximum not more than 1MB

3) Each message is a 4-byte header that is used to indicate the length of the message.

Google's official notes:

Chrome starts each native messaging host in a separate process and communicates with it using standard input ( stdin) and standard output ( stdout). The same format is used to send messages in both directions: Each message is serialized using JSON, UTF-8 encoded and was preceded with 32-bit message length in native byte order .The maximum size of a single message from the native messaging host is 1 MB, mainly to protect Chrome from misbehaving native applications. The maximum size of the message sent to the native messaging host is 4 GB.

Reference URL

Introduction of NPAPI

Https://zh.wikipedia.org/wiki/NPAPI

Https://developer.chrome.com/extensions/npapi

Extended installation method given by a software

Http://honx.in/i/U7JbRYKo13vu6TsJ

Chinese documentation (slower than official update)

Http://chrome.cenchy.com/index.html

http://open.chrome.360.cn/

Http://open.chrome.360.cn/extension_dev/overview.html official website Document https://developer.chrome.com/extensions/ Devguide can automatically generate an extended web site http://extensionizr.com/ 

Chrome extension mechanism

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.