无法使用chrome.management API

use*_*400 5 javascript google-chrome

现在我想从chrome扩展程序启动一个chrome应用程序(事实上,我想通过一个url启动chrome应用程序,我不知道这样做).这是问题所在.我补充道

  "permissions": [
    "management"
  ],
Run Code Online (Sandbox Code Playgroud)

在扩展的显而易见.但是,当我想通过使用启动应用程序时

chrome.management.launchApp("XXXX", function() {});
Run Code Online (Sandbox Code Playgroud)

,控制台说

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

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

小智 1

SO 问题Run chrome application from a web site 按钮提供了解决此问题的方法

在这里复制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);
                }
            }
        }
     );
    
    Run Code Online (Sandbox Code Playgroud)
  • 在您的 Chrome 应用程序manifest.json中定义externally_connectableurl/ urls,如下所示,

    { "name": "应用程序名称", "description": "应用程序描述", "version": "1",

          ...
    
          "externally_connectable": {
            "matches": ["*://localhost:*/"]
          }
        }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在您的应用程序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;
      });
    
    Run Code Online (Sandbox Code Playgroud)