Run chrome application from a web site button

我的未来我决定 提交于 2019-12-02 04:06:39

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 :)

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'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!