2

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!!

Howard
  • 601
  • 1
  • 11
  • 15
  • The window object represents an open window in a browser. (see https://www.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 https://developer.chrome.com/extensions/messaging). – ksridhar Jul 18 '19 at 15:39
  • You may additionally see https://stackoverflow.com/questions/22571438/access-window-object-from-a-background-chrome-extension/22599856 – ksridhar Jul 18 '19 at 15:46

0 Answers0