Home > Web Front-end > JS Tutorial > body text

How to Determine Chrome Extension Installation Status from an External Script?

Susan Sarandon
Release: 2024-11-17 01:29:03
Original
651 people have browsed it

How to Determine Chrome Extension Installation Status from an External Script?

Determining Chrome Extension Installation Status from External Script

In developing a Chrome extension that interacts with external JavaScript scripts, you may need to ascertain whether your extension is installed on a user's browser. This capability allows you to tailor your script's behavior accordingly.

Chrome has introduced a feature that enables external websites to communicate with extensions. By incorporating the following code in your extension's background script (background.js), you can establish a communication channel:

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        if (request) {
            if (request.message) {
                if (request.message == "version") {
                    sendResponse({version: 1.0});
                }
            }
        }
        return true;
    });
Copy after login

From the external website, you can initiate a message to the extension as follows:

var hasExtension = false;

chrome.runtime.sendMessage(extensionId, { message: "version" },
    function (reply) {
        if (reply) {
            if (reply.version) {
                if (reply.version >= requiredVersion) {
                    hasExtension = true;
                }
            }
        }
        else {
          hasExtension = false;
        }
    });
Copy after login

This script will check for the presence of your extension and assign a value to the hasExtension variable. The only limitation is the asynchronous nature of the request, which you'll need to accommodate in your implementation.

Remember to add an entry to the manifest.json file of your extension to specify the domains that can communicate with it, using the following syntax:

"externally_connectable": {
    "matches": ["*://localhost/*", "*://your.domain.com/*"]
},
Copy after login

2021 Update:

When invoking chrome.runtime.sendMessage, an exception will be thrown if the extension is not installed or disabled. To handle this situation, add the following validation within the sendMessage callback:

if (chrome.runtime.lastError) {
    // handle error
}
Copy after login

The above is the detailed content of How to Determine Chrome Extension Installation Status from an External Script?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template