Chrome扩展程序:架构"文件://"不允许"权限"

时间:2015-05-27 13:49:08

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

我的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>。但由于安装应用程序时出现警告,我不希望这样做。

1 个答案:

答案 0 :(得分:0)

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

只需将其置于可选权限中即可显示“允许访问文件:网址”复选框,用户无论如何都需要勾选。

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

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 通过。