文件系统的可选权限

时间:2015-05-19 12:10:46

标签: google-chrome google-chrome-extension google-chrome-app

是否可以将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"]}
  ],

1 个答案:

答案 0 :(得分:3)

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

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

在Chrome 45之前,当您第一次获得chrome.fileSystem权限后访问fileSystem API时,会出现一个错误,导致您的问题出错。在Chrome 44中,控制台上输出了一条错误消息,而早期版本导致扩展程序崩溃(https://crbug.com/489723)。 要解决此错误,请将fileSystem权限放入所需的权限集中,即在manifest.json中包含以下内容:

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

fileSystem权限不会添加任何安装警告,因此根据需要标记此权限并不是什么大问题。

相关问题