简体   繁体   English

请求扩展程序的可选权限时,“ chrome.permissions不可用”

[英]“chrome.permissions is not available” when requesting optional permissions for my extension

I am building an extension that requires access to history to provide one of the features. 我正在构建一个扩展该扩展需要访问历史记录才能提供功能之一。

After publishing a version which contained the permission as mandatory and consequently losing a part of my users because they got scared away by the big alert saying that the extension might be able to snoop into their history (I really didn't plan on doing that), I decided to publish a version with the offending part removed and the permission disabled as a temporary fix. 在发布包含强制性许可的版本后,由于他们被大警报吓到了,他们说扩展名可能可以窥探他们的历史,因此我失去了部分用户,(我真的不打算这样做) ,我决定发布一个版本,其中删除了有问题的部分,并且禁用了权限作为临时修订。

I'd like to implement this feature back using optional permissions . 我想使用可选权限来实现此功能。
First of all, I added the new optional permission to my manifest file: 首先,我向清单文件添加了新的可选权限:

...

"permissions": [
  "https://news.ycombinator.com/",
  "http://news.ycombinator.com/"
],

"optional_permissions": [ "history" ],

...

Then, I built a function to request permissions into the script which handles the extension's settings: 然后,我构建了一个函数来请求权限到处理扩展设置的脚本中:

Settings.prototype.applyPermissions = function (permissions, map) {
  Object.keys(permissions).forEach(function (key) {
    if (map[key]) {
      var checkbox = map[key].getElementsByTagName("input")[0];
      checkbox.addEventListener("change", function (e) {
        if (this.checked) {
          chrome.permissions.request(permissions[key], function(granted) {
            if (granted) {
              // Permission has been granted
            } else {
              // Not granted
            }
          });
        }
      });
    }
  });
};

The key part here is this: 这里的关键部分是:

checkbox.addEventListener("change", function (e) {
  if (this.checked) {
    chrome.permissions.request(permissions[key], function(granted) {
      if (granted) {
        // Permission has been granted
      } else {
        // Not granted
      }
    });
  }
});

I perform the request on an event caused by user interaction (the guide states that it won't work otherwise), and pass permissions[key] , an object specified in my extension's settings which looks like this: 我对由用户互动引起的事件执行请求(指南指出,该事件将无法正常工作),并传递permissions[key] (扩展名设置中指定的对象),如下所示:

"permissions": {
    "mark_as_read": {
        "permissions": ["history"]
    }
}

When accessing it as permissions[key] , I get this part: 当以permissions[key]身份访问它时,我得到了这一部分:

{
    "permissions": ["history"]
}

Which is basically the format that the documentation shows for this kind of requests. 这基本上是文档针对此类请求显示的格式。


If I run this code and toggle the checkbox that should enable the feature, and look at the error log, I see this error: 如果我运行此代码并切换应启用该功能的复选框,并查看错误日志,则会看到此错误:

chrome.permissions is not available: You do not have permission to access this API. chrome.permissions不可用:您无权访问此API。 Ensure that the required permission or manifest property is included in your manifest.json. 确保所需的权限或清单属性包含在manifest.json中。

I also tried accessing this API from a background page, where it was actually available but I was not allowed to use because Chrome requires that you access it from a user interaction, and such interaction is lost if you send a message to the background page from your content script to request activation. 我还尝试从实际可用的后台页面访问此API,但是我不允许使用它,因为Chrome要求您通过用户互动来访问它,并且如果您从您的内容脚本以请求激活。

Am I missing something obvious here? 我在这里错过明显的东西吗? Maybe I need to add something to the manifest, but I can't find any explicit documentation about it. 也许我需要在清单中添加一些内容,但是找不到关于它的任何显式文档。

I assume you're trying to do this from a content script. 我假设您正在尝试通过内容脚本执行此操作。 You can't access most chrome.* APIs from content scripts, including chrome.permissions . 您无法从内容脚本(包括chrome.permissions访问大多数chrome.* API。 However, you've correctly pointed out that a background page is also unsuitable, because you a permission change requires a direct user action. 但是,您已经正确指出,后台页面也不适合,因为更改权限需要直接的用户操作。

Luckily, we have hardly exhausted our options. 幸运的是,我们几乎没有穷尽所有选择。 You could set the permission in: 您可以在以下位置设置权限:

In the last case, get the URL using chrome.extension.getURL . 在最后一种情况下,请使用chrome.extension.getURL获取URL。 You could possibly use an iframe to inject it directly into the page, if you don't want the permission-requesting interface to be separate from the current page. 如果您不希望将权限请求界面与当前页面分开,则可以使用iframe将其直接注入到页面中。

So, in fact, content scripts and background pages are the only two extension contexts where you can't use chrome.permissions . 因此,实际上,内容脚本和背景页面是您不能使用chrome.permissions的仅有的两个扩展上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM