【问题标题】:Chrome extension: Schema "file://" not allowed in "permissions"Chrome 扩展:“权限”中不允许架构“文件://”
【发布时间】:2015-08-09 15:45:44
【问题描述】:

我的 Chrome 扩展程序有问题,就是清单文件。

{
  "manifest_version": 2,

  "permissions": [
     "file://*/*",
     "activeTab",
     "http://www.pdfzorro.com/"
   ],

  "name": "PDFzorro - PDF Editor",
  "version": "0.0.0.11",
  "short_name": "PDF-Dateien bearbeiten - edit PDF files",
  "description": "edit PDF files online, direct from GoogleDrive",
  "icons": { "16": "logo16.png",
          "128": "logo.png" },  

  "container": ["GOOGLE_DRIVE"],
  "api_console_project_id": "000000000000",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }  
}

在 Dev-Mod 中,扩展在 Chrome 中工作。但我无法将压缩后的扩展程序上传到 Chrome 网上应用店。

错误:“权限”中不允许架构“file://”

我也试过了:

file:///
file://
file:///*
file://*/*

..但没有任何效果。

应用程序正常工作和上传工作的唯一方法是将<all_urls> 添加到权限。但由于安装应用程序时出现警告,我不希望这样做。

【问题讨论】:

  • 有趣。在 optional 权限中声明 "<all_urls>" 仍会触发警告。
  • 没关系,因为我已经默许了。

标签: javascript google-chrome-extension permissions manifest file-uri


【解决方案1】:

您可以将"<all_urls>" 声明为optional permission 并在运行时请求来源。

仅将其设置为可选权限会显示用户无论如何都需要勾选的“允许访问文件:URL”复选框。

您可以找出该标志的状态,因为除非设置原点,否则原点将被视为无效。所以:

chrome.permissions.contains(
  {origins: ["file://*"]},
  function(granted){
    if(chrome.runtime.lastError) {
      // The flag is not set
      // You need to explain it to the user and then show the page
      // You can open the page scrolled where you need it with the following:
      // chrome.tabs.create({ url: "chrome://extensions?id=" + chrome.runtime.id });
      // Note that just a link won't work
    } else if(!granted) {
      // The flag is set, but the permissions are not yet granted
      // You need to call the next snippet FROM A USER GESTURE (e.g. click)
    } else {
      // Everything is peachy, you have the permissions
    }
  }
);

选中该标志后,您可以从用户手势调用以下命令来获取权限:

chrome.permissions.request(
  {origins: ["file://*"]},
  function(granted) {
    if(!granted) {
      // Should never happen (see below)
    } else {
      // Everything is peachy now
    }
  }
);

只要设置了标志,这将总是成功而不向用户显示对话框,但必须从用户手势调用一次。权限授予在整个安装生命周期中保持不变。

这不会在安装时产生警告,而 CWS 应该通过。

【讨论】:

  • 请注意,这对用户来说不是很方便:他们需要阅读说明,勾选另一个您无法交互的页面中的复选框,然后在您的 UI 中再次单击某些内容。但这就是非一揽子许可的代价。
  • 是的,我知道 :-) 我想了想,也许我会设置权限 all_urls ;-)
猜你喜欢
  • 2021-04-08
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
相关资源
最近更新 更多