javascript - Run chrome application from a web site button - Stack Overflow

I have a requirement to launch a chrome application from a web page button click. I have found the foll

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

  • Run Google Chrome application from url
  • Activate chrome app from web page?
  • How can I launch a Chrome Packaged App through javascript?

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 mand 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.

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

  • Run Google Chrome application from url
  • Activate chrome app from web page?
  • How can I launch a Chrome Packaged App through javascript?

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 mand 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.

Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Aug 4, 2015 at 4:25 Hasitha ShanHasitha Shan 2,9806 gold badges46 silver badges85 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

I found how to achieve this incase someone else es 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'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744580356a4581997.html

相关推荐

  • javascript - Run chrome application from a web site button - Stack Overflow

    I have a requirement to launch a chrome application from a web page button click. I have found the foll

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信