Chrome extension mechanism and chrome Extension Mechanism

Source: Internet
Author: User

Chrome extension mechanism and chrome Extension Mechanism

It is said that Google will completely disable the NPAPI plug-in the Chrome browser starting in May, and Chrome will no longer be able to load the NPAPI plug-in after 45, and a new mechanism is introduced: expansion.

In fact, if you think of a browser as a canvas, the NPAPI plug-in is like a variety of Ornaments hanging on the canvas, and the extension is more like splicing a small puzzle outside the canvas. Both plug-ins and extensions are used to make the painting more in line with user expectations. The following is a brief introduction of the extension and Native Message mechanism.

Extended installation package

A complete extension package will eventually contain a. crx compressed file package, which can be opened directly by using a zip decompressed file.

There are two methods for extension installation:

1) In Google APP Store, find the related extensions and click Add.

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

Drag the crx file directly.

If the extension cannot be launched with the browser, the extension will be a small piece of puzzle placed on the side of the browser.

There are two forms of extended Communication with browsers:

1) transient connection

Send message: chrome. runtime. sendMessage

Receive events: chrome. runtime. onMessage. addListener

2) persistent connection

Send message: var port = chrome. runtime. connect

Port. postMessage

Receive event: port. onMessage. addListener

In some cases, expansion alone cannot achieve our expected results. In this case, we need to introduce the Host. From the perspective of the browser, It is the browser <-----> extension <------> Host. That is to say, the browser and the extension can communicate with each other, and the extension and the Host can communicate with each other, but the Host and the browser cannot communicate directly. The Demo provided by the Chrome example is also such a structure.

To expand communication with the Host, you must follow the Native Message mechanism. To implement Native Message communication, you need to prepare in advance:

1) Host and extension

2) The Host JSON file defines the name of the Host application and binds the extension ID to it. Only those bound here are valid and can communicate with each other.

3) How can the browser find the JSON file on the Host end?

On Windows, it is written in the registry; on Mac, it is stored in the specified directory.

Windows: 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 or

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

The Native Message mechanism can communicate in two ways:

1) transient connection

Send message: chrome. runtime. sendNativeMessage

2) persistent connections

Send message: var port = chrome. runtime. connectNative

Port. postMessage

Receive event: port. onMessage. addListener

Port. onDisconnect. addListener

Note the Native Message mechanism

1) extended communication with the Host can be understood as process communication based on the standard input/output interface. On the Host side, the read/write operations must be performed in the form of a binary stream.

2) from the extended Host end, a single message cannot exceed 4 GB

From the Host end to the extended, a single message cannot exceed 1 MB

3) Each message has a 4-byte header to indicate the message length.

Google official instructions:

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 ctions: each message is serialized using JSON, UTF-8 encoded and is 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 to NPAPI

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

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

Extended installation method provided by a software

Http://honx.in/ I /U7JbRYKo13vu6TsJ

Chinese documents (slower than official updates)

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

Http://open.chrome.360.cn/

Bytes/

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.