Launch Chrome app using website button

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

Which suggests using externally_connectable and url_handlers , but doesn't seem to work. Can I launch the chrome app correctly using the button on the web page by clicking the Chrome x extension icon .

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

PS

I tried the following command when I clicked a button on my webpage,

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

This led to an error Uncaught TypeError: Cannot read property 'launchApp' of undefined.

+1
source share
2 answers

I found how to achieve this, someone else is facing this problem.

  • on your web page, click a button , for example, enter 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 manifest.json application, define the url externally_connectablelike this:

     {
      "name": "App name",
      "description": "App Desc",
      "version": "1",
    
      ...
    
      "externally_connectable": {
        "matches": ["*://localhost:*/"]
      }
    }
    
  • In the background.js application, configure a listener that will be called when a message is sent from a 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 :)

+6
source

Hasitha . . URL

localhost:3905/manager

, _

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

- URL-

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

. ://localhost '

+1

Source: https://habr.com/ru/post/1545809/


All Articles