javascript - 无法使用 chrome.management API

标签 javascript google-chrome

现在我想从 chrome 扩展程序启动 chrome 应用程序(事实上,我想通过 url 启动 chrome 应用程序,我不知道该怎么做)。 问题来了。我加了

  "permissions": [
    "management"
  ],

在扩展 list 中。但是,当我想使用

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

,控制台显示

Uncaught TypeError: Cannot read property 'launchApp' of undefined

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

最佳答案

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 应用程序 ma​​nifest.json 中定义 externally_connectable url/urls 如下,

    { "name": "应用名称", "description": "应用描述", “版本”:“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;
      });
    

关于javascript - 无法使用 chrome.management API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382305/

相关文章:

javascript - 浏览器后退按钮的痛苦

javascript - JavaScript 是否有可能阻止人们右键单击以保存图像?

javascript - 在 Google Chrome 中调试 CSP 违规

javascript - 为什么我的 jQuery 脚本只能在 Firefox 中运行?

javascript - 为什么这个脚本在 Chrome 中不起作用?

javascript - 是否可以在类位于 Angular 6 组件内部的应用程序组件上使用 [ngClass]?

javascript - 使用按钮将 d3 词云导出为 png

javascript - angularjs ng-repeat cascading Dropdown不更新

html - 在 Chrome 中正确显示,但在 Internet explorer 11 中不正确

linux - WebDriver异常: no chrome binary at/usr/bin/google-chrome-stable or chrome binary not found