Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'));
                });
            });
        }
    }
}
like image 614
Derek Avatar asked Mar 25 '11 07:03

Derek


People also ask

Can Chrome extensions write to file?

(ChromeOS only) Extends Chrome. For example, apps or extensions can allow users to upload files to a website. Allows app or extension to create, read, navigate, and write to the user's local file system at a user-selected location.

Can a Chrome extension access local files?

For security reasons, by default the Chrome browser does not allow extensions to access local files. If you want the accelerator to access local files (locations of "file:///...", instead of "http://" or "https://"), you must configure Chrome to allow the access.

Can you access file system from browser?

Web browsers (and JavaScript) can only access local files with user permission. To standardize the file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.


1 Answers

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

like image 85
HBP Avatar answered Sep 21 '22 16:09

HBP