检测是否安装了其他Chrome扩展程序

问题描述:

我正在开发Chrome扩展程序,但是我希望我的扩展程序仅在禁用/删除另一个扩展程序后才能工作.

I'm developing an extension for Chrome, but I want my extension to work only if another is disabled/removed.

这意味着当我的用户安装我的扩展程序时,我想告诉他们如果安装我的扩展程序,则必须接受以禁用此其他扩展程序".

It means that when my users install my extension, I would like to tell them "if you install my extension, you have to accept to disable this other extension".

我不希望我的扩展程序正常工作.

I don't want my extension to work if the other one is active.

你知道我该怎么做吗?

要检测是否安装了另一个扩展程序,

To detect if another extension is installed:

1)更新 manifest.json 以包括必要的 management 权限:

1) Update the manifest.json to include the necessary management permission:

{
    "name": "My extension",
    ...
    "permissions": [
      "management"
    ],
    ...
}

2)要检查扩展是否已安装,有2个选项:

2) To check if the extension is installed exist 2 options:

a)如果您知道扩展名ID,请使用 chrome.management.get(extensionId,函数回调)方法:

a) If you know the extension id, use the chrome.management.get(extensionId, function callback) method:

 var extensionId = '<the_extension_id>';
 chrome.management.get(extensionId, function(extensionInfo) {
    var isInstalled;
    if (chrome.runtime.lastError) {
      //When the extension does not exist, an error is generated
      isInstalled = false;
    } else {
      //The extension is installed. Use "extensionInfo" to get more details
      isInstalled = true;
    }
  });

b)如果您知道扩展名(或任何其他参数),则可以使用chrome.management.getAll(函数回调):

b) If you know the extension name (or any other parameter), you can use chrome.management.getAll(function callback):

var extensionName = '<the_extension_name>';

chrome.management.getAll(function(extensions) {
    var isInstalled = extensions.some(function(extensionInfo) {
       return extensionInfo.name === extensionName;       
    });
});