Search code examples
javascripthtmlgoogle-chrome-extension

Need to open a chrome extension from button click + HTML + JS


Need to open chrome extension from html button click.

I tried to use chrome.runtime to open an extension but there is no use.

The program executes but the extension is not opening.

<!DOCTYPE html>
<html>

<head>
    <script> 
        // This function will trigger the Chrome extension with the specified ID
        function runChromeExtension(extensionId) {
            console.log(extensionId)
            // Check if the extension is already installed 
            if (typeof (chrome) !== "undefined" && chrome.runtime) {
                // If the extension is installed, send a message to it
                chrome.runtime.sendMessage(extensionId, { action: "run" }, function (response) {
                    console.log(response);
                });
            } else {// If the extension is not installed, show an error message
                console.error("Chrome extension not found");
            }
        }</script>
    
</head>

<body>
    <button onclick="runChromeExtension('anflghppebdhjipndogapfagemgnlblh')">Run Chrome Extension</button>
</body>

</html>

Any suggestions or am doing anything wrong?

Kindly Note: Security settings can be modified to run the above program.


Solution

  • If you own chrome extension then
    Add your website url in externally_connectable key in manifest.json

    "externally_connectable": {
            "matches": ["http://mywebsite.com/*"]
        },
    

    If your domain not exist in any installed extension then
    chrome make chrome.runtime is undefined