无法使用chrome.management API [英] Cannot use chrome.management API

查看:153
本文介绍了无法使用chrome.management API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我想从一个chrome扩展中启动一个chrome应用程序(实际上,我想通过一个url启动chrome应用程序,但我不知道这一点)。
问题在这里。我添加了

 permissions:[
management
],
code>

在扩展名清单中。但是,当我想通过使用

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

,控制台说


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

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

解决方案

一个解决方法可以在SO问题从网站按钮运行Chrome应用程序



复制 Hasitha 的答案直接参考。




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

      / /从网络传递给Chrome应用程序的数据
    chrome.runtime.sendMessage('your_chrome_app_id',{message:version},
    function(reply){
    if(reply){
    if(reply.version){
    //记录从chrome应用程序收到的响应
    console.log(reply.version);
    }
    }
    }
    );


  • 在您的Chrome应用程序 manifest.json 中定义 externally_connectable url / url如下,

    {
    name:应用程序名称,
    description:App Desc,
    version: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加载屏幕是login.html
    window.open('login.html');

    // if requi红色也可以将回复发回给网站。我在网站上记录了回复
    sendResponse({version:'1.1.1'});
    }
    }
    }
    返回true;
    });



Now I want to start a chrome app from a chrome extension(in fact, I want to start chrome app through a url, which I have no idea to do). Here comes the question. I added

  "permissions": [
    "management"
  ],

in the manifest of extension. However, when I want to start app by using

chrome.management.launchApp("XXXX", function() {});

, the console says

Uncaught TypeError: Cannot read property 'launchApp' of undefined

So I wonder why I cannot use chrome management API. Thanks!

解决方案

A workaround for this is available at the SO question Run chrome application from a web site button

Copying Hasitha's answer here for direct reference.

  • Add the following piece of code where you want to invoke the chrome app

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

这篇关于无法使用chrome.management API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆