Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 同步文件系统api可以在chrome扩展中使用吗_Html_Google Chrome_Google Chrome Extension_Html5 Filesystem - Fatal编程技术网

Html 同步文件系统api可以在chrome扩展中使用吗

Html 同步文件系统api可以在chrome扩展中使用吗,html,google-chrome,google-chrome-extension,html5-filesystem,Html,Google Chrome,Google Chrome Extension,Html5 Filesystem,我正在谷歌扩展中创建页面操作弹出窗口。当弹出窗口关闭时,我需要将内容写回文件。为此,我附加了窗口.unload事件。在卸载处理程序中,我试图写入文件。但是由于我使用的是异步api,在为getFile函数设置successHandler之后,页面立即关闭,successHandler不会执行。我可以在这里使用同步文件api吗?我了解到同步API只能用于webworkers。它可以用于扩展吗?还是有其他方法来处理这种情况。我正在将写入文件的代码粘贴到这里 fileSystem.root.getFil

我正在谷歌扩展中创建页面操作弹出窗口。当弹出窗口关闭时,我需要将内容写回文件。为此,我附加了
窗口.unload
事件。在卸载处理程序中,我试图写入文件。但是由于我使用的是异步api,在为
getFile
函数设置successHandler之后,页面立即关闭,successHandler不会执行。我可以在这里使用同步文件api吗?我了解到同步API只能用于webworkers。它可以用于扩展吗?还是有其他方法来处理这种情况。我正在将写入文件的代码粘贴到这里

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);
修改最初发布的问题


我将上面的代码移到了内容脚本中。现在,我试图在页面操作关闭时与内容脚本通信。为此,我在
window.onunload
中附加了一个函数。我在调试器模式下打开了页面操作弹出窗口,并执行了
location.reload(true)
。然后调用我的
窗口.unload
侦听器,我的程序控件到达内容脚本,代码正在运行。但是,当我实际部署扩展时,当用户单击弹出窗口以外的其他位置时,弹出窗口将关闭。在这种情况下,未调用卸载处理程序。谷歌没有提供任何事件来检测弹出窗口何时关闭。提供的唯一事件是单击事件,如果页面操作有弹出窗口,则不会触发该事件。我能做些什么来检测弹出窗口何时关闭?

要找到答案,请创建一个测试用例。下面的代码在a中进行了测试

以下测试用例,工作正常

// 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
因此,Chrome扩展范围内不支持同步文件系统API


不要在弹出窗口中执行虚拟I/O,而是使用或将数据传递到后台,并让后台处理。

我在Chrome的扩展API页面中找不到您所引用的API:。另外,Chrome扩展无法直接访问文件系统。唯一与文件相关的API是chrome.fileBrowserHandler,它只适用于chrome操作系统。请忽略我之前的评论。看起来您正在使用HTML5的文件系统API,而不是Chrome扩展的文件系统API。是的,我正在使用HTML5文件系统API。对不起,我没有特别提到
// 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