Chrome浏览器扩展程序chrome.app.isInstalled总是返回false? [英] chrome.app.isInstalled always returns false for Google Chrome extensions?

查看:564
本文介绍了Chrome浏览器扩展程序chrome.app.isInstalled总是返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么chrome.app.isInstalled总是会为Google Chrome扩展程序返回false?



动态地在页面加载时添加一个链接元素:

 << ; link type =text / css =chrome-webstore-itemhref =https://chrome.google.com/webstore/detail/itemID> 

以下是点击按钮时执行的一些Javascript代码:

  if(!chrome.app.isInstalled){
alert('extension is about to be installed!');
install_extension();
} else {
alert('extension is installed already。');
}

第一次点击按钮时,Google Chrome问我是否想要安装扩展。我同意并且扩展已正确安装。当我刷新页面时,我再次点击该按钮,Google CHrome要求我再次安装扩展程序,即使在2分钟前安装了该扩展程序。换句话说,即使安装了扩展程序,chrome.app.isInstalled也总是返回false。为什么?

解决方案

请参阅 chrome.app.isInstalled始终返回为false


chrome.app.isInstalled 适用于托管应用程序 strong>(它定义了一组包含应用程序的URL)。扩展可以反而表明它们已经通过在页面中注入DOM节点来安装(参见 https://developers.google.com/chrome/web-store/docs/inline_installation#already-installed )。

该链接描述了测试您的扩展是否已安装的策略: 有一个内容脚本注入一个DOM节点到每个页面。这个节点应该有一个非常具体的ID,例如< div id ='my-extension-installed-with-id-sdgdthsdfgdtyjufwknsdkos'>

li>
  • 如果该节点存在,则内容脚本正在运行;因此,扩展已安装。如果它不存在,则假定没有安装该扩展。

  • 注入一个DOM节点不会影响状态 app.isInstalled 。相反,您会检查DOM节点的存在以证明扩展的存在。


    why does chrome.app.isInstalled always return false for Google Chrome extensions?

    Dynamically I add a link element on page load:

    <link type="text/css" rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/itemID">
    

    The following is some Javascript that executes when a button has been clicked:

    if (!chrome.app.isInstalled) {
     alert('extension is about to be installed!');
     install_extension();
    }else{
      alert('extension is installed already.');
    }
    

    The first time I clicked the button, Google Chrome asked me if I wanted to install the extension. I agree and the extension was installed correctly. When I refreshed the page, I clicked the button once again and Google CHrome asked me to install the extension once again even when I had installed it 2 minutes ago. In other words, chrome.app.isInstalled always return false even when the extension is installed. Why?

    解决方案

    See chrome.app.isInstalled Always Returns as "false":

    chrome.app.isInstalled is meant for use by hosted apps (which define a set of URLs that encompass the app). Extensions can instead indicate that they're installed already by injecting a DOM node into the page (see second half of https://developers.google.com/chrome/web-store/docs/inline_installation#already-installed).

    That link describes a strategy for testing if your extension is installed:

    1. Have a content script inject a DOM node into every page. This node should have a very specific ID, e.g., <div id='my-extension-installed-with-id-sdgdthsdfgdtyjufwknsdkos'>

    2. On button press, have your page test if that node exists.

    3. If the node exists, the content script is running; therefore, the extension is installed. If it does not exist, assume the extension is not installed.

    Injecting a DOM node won't affect the state of app.isInstalled. Instead, you check for the existence of the DOM node as proof of the extensions presence.

    这篇关于Chrome浏览器扩展程序chrome.app.isInstalled总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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