简体   繁体   中英

Writing to local file system in Chrome extension

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'));
                });
            });
        }
    }
}

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).

found at 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

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.

那么只使用localStorage呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM