Can the synchronous file system api be used in a chrome extension

廉价感情. 提交于 2019-12-08 11:09:15

问题


I'm creating page action popup in google extensions. I need to write contents back to a file when the popup gets closed. For this I have attached the window.unload event. In the unload handler I'm trying to write to the file. But since I'm using the asynchronous api, immediately after setting the successHandler for the getFile function, the page gets closed and the successHandler does not get executed. Can I use the synchronous file api here? I read that the synchronous apis can be used only with webworkers. Can it be used in extensions? Or is there some other way to deal with this situation. I'm pasting my code for the writing to the file here

fileSystem.root.getFile(fileName, {create : true, exclusive : true}, function (fileEntry) {
                file.createWriter(function() {
                    window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
                    var bb = new BlobBuilder(); 
                    bb.append(JSON.stringify(credentialsObj));
                    fileWriter.write(bb.getBlob('text/plain'));                         
                }, errorHandler);
            }, errorHandler);

Modification to the question posted initially

I moved the above piece of code to a content script. Now I'm trying to communicate with the content script when the page action closes. For this I have attached a function to window.onunload. I opened the page action popup in debugger mode and I executed location.reload(true). Then my window.unload listener is getting called and my program control is reaching the content script and the code is running. But when I actually deploy the extension, the popup will be closed when the user clicks somewhere other than within the popup. The unload handler is not getting called in this case. Google doesn't provide any event to detect when the popup closes. The only event provided is a click event which won't be triggered if the page action has a popup. What can I do to detect when the popup gets closed?


回答1:


To find out, create a test case. The following code was tested in the a background page's console.

 webkitRequestFileSystem(TEMPORARY, 1024);     // TypeError: Not enough arguments

The following test case, using a Web worker, works fine:

// Create a worker for testing purposes
var blob = new Blob([
    'onmessage=function(e){postMessage(eval(e.data))}'
], {
    type: 'application/javascript'
});
var worker = new Worker((window.webkitURL || URL).createObjectURL(blob);
worker.onmessage = function(e) {console.log(e.data)};

// Test:
worker.postMessage('webkitRequestFileSystem(TEMPORARY, 1024)'); // No error
// For comparison, check whether we receive an error when something goes wrong:
worker.postMessage('webkitRequestFileSystem(0)');     // E: Not enough arguments

So, the synchronous FileSystem API is not supported within the scope of a Chrome extension.

Instead of doing the virtual I/O in the popup, use chrome.extension.getBackground() or chrome.runtime.sendMessage to pass the data to the background, and let the background process it.



来源:https://stackoverflow.com/questions/10493337/can-the-synchronous-file-system-api-be-used-in-a-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!