Javascript 在 Chrome 扩展程序中写入本地文件系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5429513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:09:55  来源:igfitidea点击:

Writing to local file system in Chrome extension

javascriptgoogle-chromegoogle-chrome-extension

提问by Derek

chrome.tabs.onUpdated.addListener(checkForValidUrl);
function checkForValidUrl(tabId, changeInfo, tab) {
    if (tab.url.indexOf('https') > -1) {
        var tabURL = tab.url;
        console.log("\n<TimeStamp>" + getTimestamp() + "</TimeStamp><Browser>Chrome</Browser><URL>" + tabURL + "</URL>\n");
        window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024, initFs);

        function initFs(fs) {
            fs.root.getFile
            ('log.txt', { create: true, exclusive: true }, function (fileEntry) {
                fileEntry.isFile = true;
                fileEntry.name = 'log.txt';
                fileEntry.fullPath = '/log.txt';
                fileEntry.createWriter(function (fileWriter) {
                    fileWriter.seek(fileWriter.length);
                    var bb = new BlobBuilder();
                    bb.append("\n<TimeStamp>" + getTimestamp() + "</TimeStamp><Browser>Chrome</Browser><URL>" + tabURL + "</URL>\n");
                    fileWriter.write(bb.getBlob('text/plain'));
                });
            });
        }
    }
}

回答by HBP

Based on this:

基于此:

At the time of writing this article, Google Chrome 9+ has the only working implementation of the FileSystem API. Since a dedicated browser UI does not yet exist for file/quota management, the API cannot be used without running Chrome with the --unlimited-quota-for-files flag (Note: if you're building an app or extension for the Chrome Web Store, the unlimitedStorage manifest file permission will suffice).

在撰写本文时,Google Chrome 9+ 拥有 FileSystem API 的唯一有效实现。由于尚不存在用于文件/配额管理的专用浏览器 UI,因此如果不运行带有 --unlimited-quota-for-files 标志的 Chrome,则无法使用该 API(注意:如果您正在为 Chrome 构建应用程序或扩展程序Web Store,unlimitedStorage 清单文件权限就足够了)。

found at http://www.html5rocks.com/tutorials/file/filesystem/#toc-support

http://www.html5rocks.com/tutorials/file/filesystem/#toc-support找到

I assume you are using Chrome and that you have not set the --unlimited-quota-for-files flag

我假设您正在使用 Chrome 并且您没有设置 --unlimited-quota-for-files 标志

回答by Michael

This filesystem API does not appear to actually write a true "file" to your hard disk. It seems to store a file within a sandboxed safe zone in the browser. You'll have to write a quick and dirty little file manager (or find one out there) to manage the files for a given web app. You can also try visiting filesystem://<your URL here>/temporary/to see all the files that your app has created.

此文件系统 API 似乎并未实际将真正的“文件”写入您的硬盘。它似乎将文件存储在浏览器的沙盒安全区中。您必须编写一个快速而肮脏的小文件管理器(或在那里找到一个)来管理给定 Web 应用程序的文件。您还可以尝试访问filesystem://<your URL here>/temporary/以查看您的应用程序创建的所有文件。

回答by Marcel

What about just using localStorage?

只使用localStorage怎么样?