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

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

问题描述

我需要从网页按钮点击启动 Chrome应用程序。我发现了以下资源:'



建议使用 externally_connectable url_handlers ,但似乎不起作用。是否有适当的方法可以通过调用 Chrome应用扩展程序ID ?,通过按钮点击网页页来启动Chrome应用程序。

我是这个领域的新手,任何专家的帮助都将不胜感激:)



PS



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



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



这导致错误 Uncaught TypeError:无法读取属性'launchApp'的未定义

解决方案

我发现如何达到这个目的别人会遇到这个问题。 / p>


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

      //将数据从web传递到chrome应用程序
    chrome.runtime.sendMessage('your_chrome_app_i (回复){
    if(reply.version){
    //记录收到的回复从chrome应用程序
    console.log(reply.version);
    }
    }
    }
    );


  • 在您的Chrome应用程序 manifest.json 中定义 b

    $ name

    $ b $ :App name,
    description:App Desc,
    version:1,

    ...

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

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

  • p>

      chrome.runtime.onMessageExternal.addListener(
    函数(request,sender,sendResponse){
    if(request ){
    if(request.message){
    if(request.message ==version){

    //我的chrome应用程序初始加载屏幕是login.html
    window.open('login.html');

    //如果需要的话可以将回复发回到网站aswel l。我在网站上记录了回复
    sendResponse({version:'1.1.1'});
    }
    }
    }
    返回true;
    });




希望这有助于:)


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

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 :)

PS

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

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

This resulted in an error 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);
                }
            }
        }
     );
    

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

Hope this helps :)

这篇关于从网站按钮运行chrome应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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