最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Writing to local file system in Chrome extension - Stack Overflow

programmeradmin4浏览0评论
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'));
                });
            });
        }
    }
}
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'));
                });
            });
        }
    }
}
Share Improve this question edited Mar 26, 2011 at 14:36 Bill the Lizard 406k211 gold badges572 silver badges889 bronze badges asked Mar 25, 2011 at 7:06 DerekDerek 2,2054 gold badges21 silver badges25 bronze badges 5
  • 1 Are you writing a Chrome extension? If so, please specify that (in a tag and/or in the question). – Martijn Commented Mar 25, 2011 at 7:56
  • Try adding an error callback to the requestFileSystem call to display any errors. – HBP Commented Mar 25, 2011 at 12:14
  • I have edited changes to my question,previous problem have been solved & I have been faced with a new problem. Do guide me along,thank you in advance. – Derek Commented Mar 26, 2011 at 7:37
  • If your question was answered you should close it and open a new one. In any case I see your code, what is the question. – HBP Commented Mar 26, 2011 at 9:58
  • Is there any way to access this file from a endows application not running in the browser? – rollsch Commented Sep 30, 2023 at 20:50
Add a comment  | 

3 Answers 3

Reset to default 10

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.

What about just using localStorage?

发布评论

评论列表(0)

  1. 暂无评论