文件系统的可选权限

Optional permission for filesystem

是否可以提及 filesystem 作为 chrome.permissions.request API 请求的可选权限(对于 Chrome 个应用程序)?

我的 JS 代码包括:

document.getElementById('savebtn').addEventListener('click',
    function () {
        chrome.permissions.request({ permissions: ["fileSystem"] },
            function (granted) {
                if (granted) {
                    chrome.fileSystem.chooseEntry({ type: 'openDirectory' },
                        function (entry) {
                            ...
                        });
                }
            });
    });

但是我在上面的代码中 chrome.fileSystem 周围出现了一个错误:

... extensions::fileSystem:11: Uncaught TypeError: Cannot read property 'getFileBindingsForApi' of undefined{TypeError: Cannot read property 'getFileBindingsForApi' of undefined

我的 manifest.json 文件包括:

  "optional_permissions": [
    {"fileSystem": ["write", "retainEntries", "directory"]}
  ],

如果您想使用类型为 openDirectorychooseEntry,您也应该请求 fileSystem.directory 权限。这可以按如下方式完成:

chrome.permissions.request({
    permissions: [
        'fileSystem',
        'fileSystem.write',
        'fileSystem.retainEntries',
        'fileSystem.directory'
    ]
}, function(granted) {
    if (granted) { /* use chrome.fileSystem API */ }
});

在Chrome45之前,第一次获得fileSystem权限后访问chrome.fileSystemAPI时出现了一个bug导致你的问题出现错误时间。在 Chrome 44 中,错误消息被打印到控制台,而早期版本导致扩展程序崩溃 (https://crbug.com/489723)。
要解决此错误,请将 fileSystem 权限放入所需的权限集中,即在 manifest.json 中具有以下内容:

"optional_permissions": [
    {"fileSystem": ["write", "retainEntries", "directory"]}
],
"permissions": [
    "fileSystem"
],

文件系统权限不会添加任何安装警告,因此将此权限标记为必需没什么大不了的。