如何检查一个Chrome应用程序是否已安装在另一个Chrome应用程序中?

4
我想知道如何从不同的Chrome应用程序中检查某个应用程序是否已安装。例如,我制作了App1和App2,现在我想知道当用户打开App2时,他/她是否已安装App1。是否可以通过某些Chrome API实现这一点,或者这是不可能的?
如果无法检查用户是否已安装App1,则是否有某种解决方法?
如果需要,我可以访问Chrome Web Store。
我的目标是为那些安装了我的其他应用程序的用户提供一些忠诚度福利。

为什么您会需要这个?如果不知道您的目标,我们无法提供任何解决方案。 - John Dvorak
因此,我可以为那些安装了我的其他应用程序的用户提供一种忠诚度福利。 - dragonloverlord
1个回答

6

由于您编写了这两个应用程序,因此可以使用外部消息传递来实现:

在app1后台脚本中:

var app2id = "abcdefghijklmnoabcdefhijklmnoab2";
chrome.runtime.onMessageExternal.addListener(
  // This should fire even if the app is not running, as long as it is
  //   included in the event page (background script)
  function(request, sender, sendResponse) {
    if(sender.id == app2id && request.areYouThere) sendResponse(true);
  }
);

在app2的某个地方:

var app1id = "abcdefghijklmnoabcdefhijklmnoab1";
chrome.runtime.sendMessage(app1id, {areYouThere: true},
  function(response) {
    if(response) {
      // Installed and responded
    } else {
      // Could not connect; not installed
    }
  }
);

我喜欢这个答案,但是这两个应用程序是否需要同时运行才能实现这个功能? - dragonloverlord
嗯,我不是100%确定,但据我所知,一旦在后台脚本中注册了此侦听器,它将会触发。 - Xan
好的,谢谢你的回答,已经被接受了。感谢你的帮助,非常感激。 - dragonloverlord
1
测试过了;我可以确认它能唤醒一个不活跃的应用程序。 - Xan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接