Run chrome application from a web site button

前端 未结 2 1322
执念已碎
执念已碎 2021-01-22 08:28

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

  • Run Goo
相关标签:
2条回答
  • 2021-01-22 08:55

    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'

    0 讨论(0)
  • 2021-01-22 09:10

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

    0 讨论(0)
提交回复
热议问题