4

I use the following code in content.js, and it works fine.

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var fs = null;

function initFS(grantedBytes) {
    window.requestFileSystem(window.PERSISTENT, grantedBytes, function(filesystem) {
        fs = filesystem;
    }, errorHandler);
}
function askStorage(){
    console.log("askStorage");
    window.webkitStorageInfo.requestQuota(PERSISTENT, 500*1024*1024, function(grantedBytes) {
        initFS(grantedBytes);
    }, function(e) {
        console.log('Error', e);
    });
}

function errorHandler(e) {
}

function saveFileSystem(files){
    for (var i = 0, file; file = files[i]; ++i) {
        console.log("file.name:"+file.name);
        if (!fs) alertFileError();

        (function(f) {
            fs.root.getFile((file.name), {create: true, exclusive: true}, function(fileEntry) {

                fileEntry.createWriter(function(fileWriter) {
                    fileWriter.write(f); 
                    console.log("toURL:"+fileEntry.toURL());
                }, errorHandler);

            }, errorHandler);
        })(file);

    }


}
askStorage();

However, when I use those codes in background.js, I get a error "Uncaught Error: TYPE_MISMATCH_ERR: DOM File Exception 11". Any idea? Thanks!!

2
  • 1
    The window object represents an open window in a browser. (see w3schools.com/jsref/obj_window.asp). Only content scripts can access the page content and hence the window object. Background scripts cannot access page content directly and thus have no context of the window object through which the requestFileSystem has been accessed. Background and content scripts can communicate through messaging though (see developer.chrome.com/extensions/messaging). Commented Jul 18, 2019 at 15:39
  • You may additionally see stackoverflow.com/questions/22571438/… Commented Jul 18, 2019 at 15:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.