【问题标题】:Run chrome application from a web site button从网站按钮运行 chrome 应用程序
【发布时间】:2015-08-04 04:25:49
【问题描述】:

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

这建议使用 externally_connectableurl_handlers 但似乎不起作用。有没有一种正确的方法可以通过调用 chrome 应用扩展 ID 来通过 button click on web 页面启动 chrome 应用程序?

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

附言

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

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

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

【问题讨论】:

    标签: javascript google-chrome web google-chrome-app


    【解决方案1】:

    我找到了如何实现这一点,以防其他人遇到这个问题。

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

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

    希望这会有所帮助:)

    【讨论】:

    • 这是一个很有帮助的例子,所以赞成。但请不要使用inline code formatting` 来突出显示文本。有一个 perfectly versatile array 工具可以突出显示文本。您能否编辑您的问题/答案以删除非代码文本的格式?
    【解决方案2】:

    Hasitha 的回复奏效了。但是在清单文件中,我需要进行正则表达式更改。 对于网址

    localhost:3905/manager
    

    显然externally_connectible 值应该设置为

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 2021-01-11
      • 2013-09-17
      • 2019-01-06
      • 1970-01-01
      相关资源
      最近更新 更多