无法使用chrome.management API

时间:2014-06-24 08:56:06

标签: javascript google-chrome

现在我想从chrome扩展程序启动Chrome应用程序(事实上,我想通过url启动chrome应用程序,我不知道这样做)。 这是问题所在。我添加了

  "permissions": [
    "management"
  ],

在扩展的清单中。但是,当我想使用

启动应用程序时
chrome.management.launchApp("XXXX", function() {});

,控制台说

  

未捕获的TypeError:无法读取未定义的属性“launchApp”

所以我想知道为什么我不能使用chrome management API。谢谢!

1 个答案:

答案 0 :(得分:1)

可以在SO问题Run chrome application from a web site button

中找到解决方法

在此处复制Hasitha的答案以供直接参考。

  • 在您要调用Chrome应用的位置添加以下代码

                                                     //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);
                }
            }
        }
     );
    
  • Chrome应用程序中的
  • manifest.json 定义externally_connectable网址/网址,如下所示,

    {           “名称”:“应用名称”,           “description”:“App Desc”,           “版本”:“1”,

          ...
    
          "externally_connectable": {
            "matches": ["*://localhost:*/"]
          }
        }
    
  • 在您的应用程序 background.js 中设置一个侦听器,当从网页发送消息时调用该侦听器,如下所示,

    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;
      });