运行 chrome 来自网站按钮的申请

Run chrome application from a web site button

我需要从网页 单击按钮 启动 chrome 应用程序。我找到了以下资源,'

建议使用 externally_connectableurl_handlers 但似乎不起作用。有没有一种正确的方法可以通过调用 chrome 应用程序扩展 ID[=42 来通过 按钮单击 web 页面来启动 chrome 应用程序=]?.

我是这个领域的新手,非常感谢各位专家的帮助:)

PS

我在我的网页上点击按钮尝试了以下命令,

chrome.management.launchApp('app id');

这导致错误 Uncaught TypeError: Cannot read property 'launchApp' of undefined

我找到了如何解决这个问题,以防其他人遇到这个问题。

  • 在您的网页中点击按钮例如,包含以下代码,

                                                     //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 url/ url如下,

     {
      "name": "App 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 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;
      });
    

希望这对您有所帮助:)

Hasitha 的回复有效。但是在清单文件中我需要做一个正则表达式更改。 对于 URL

localhost:3905/manager

显然 externally_connectible 值应设置为

"externally_connectable": {
    "matches": ["*://localhost:*/*"]
}

但是由于某些原因,同一个URL无法匹配到

"externally_connectable": {
    "matches": ["*://localhost*"]
}

没用。 生成的错误 无效的匹配模式 '://localhost'