Hasitha Shan
Hasitha Shan

Reputation: 2980

Run chrome application from a web site button

I have a requirement to launch a chrome application from a web page button click. I have found the following resources,'

Which suggests to use externally_connectable and url_handlers but doesn't seem to work. Is there a proper way I could launch a chrome application via button click on web page by calling the chrome app extension id?.

I am new to this field and any help from you experts would be greatly appreciated :)

PS

I tried the following command on a button click on my web page,

chrome.management.launchApp('app id');

This resulted in an error Uncaught TypeError: Cannot read property 'launchApp' of undefined.

Upvotes: 2

Views: 2877

Answers (2)

janindu
janindu

Reputation: 39

Hasitha's reply worked. However in the manifest file I needed to do a regex change. For the URL

localhost:3905/manager

obviously the externally_connectible value should be set as

"externally_connectable": {
    "matches": ["*://localhost:*/*"]
}

But for some reason, the same URL cannot be matched with

"externally_connectable": {
    "matches": ["*://localhost*"]
}

did not work. Generated error Invalid match pattern '://localhost'

Upvotes: 1

Hasitha Shan
Hasitha Shan

Reputation: 2980

I found how to achieve this incase someone else comes across this issue.

  • in your web page on button click for example, include the following code,

                                                     //data passed from web to chrome app
    chrome.runtime.sendMessage('your_chrome_app_id', { message: "version" },
        function (reply) {
            if (reply) {
                if (reply.version) {
                    //log the response received from the chrome application
                    console.log(reply.version);
                }
            }
        }
     );
    
  • in your chrome application manifest.json define the externally_connectable url/ urls as follows,

     {
      "name": "App name",
      "description": "App Desc",
      "version": "1",
    
      ...
    
      "externally_connectable": {
        "matches": ["*://localhost:*/"]
      }
    }
    
  • In your application background.js setup a listener to be invoked when a message is sent from the web page as follows,

    chrome.runtime.onMessageExternal.addListener(
      function(request, sender, sendResponse) {
        if (request) {
          if (request.message) {
            if (request.message == "version") {
    
              //my chrome application initial load screen is login.html
              window.open('login.html');
    
              //if required can send a response back to the web site aswell. I have logged the reply on the web site
              sendResponse({version: '1.1.1'});
            }
          }
        }
        return true;
      });
    

Hope this helps :)

Upvotes: 8

Related Questions