从网站按钮运行 Chrome 应用程序 [英] Run chrome application from a web site button

查看:50
本文介绍了从网站按钮运行 Chrome 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a requirement to launch a chrome application from a web page button click. I have found the following resources,'

建议使用 externally_connectableurl_handlers 但似乎不起作用.是否有一种正确的方法可以通过调用 chrome 应用扩展程序 ID,通过 网页上的按钮点击 页面启动 chrome 应用程序?.

Which suggests to use externally_connectable and url_handlers but doesn't seem to work. Is there a proper way I could launch a chrome application via button click on web page by calling the chrome app extension id?.

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

I am new to this field and any help from you experts would be greatly appreciated :)

附注

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

I tried the following command on a button click on my web page,

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

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

推荐答案

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

I found how to achieve this incase someone else comes across this issue.

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

  • in your web page on button click for example, include the following code,

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

  • 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:*/"]
      }
    }
    

  • 在您的应用程序 background.js 中设置一个侦听器,以便在从网页发送消息时调用,如下所示,

  • 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 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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