可以在chrome扩展中使用同步文件系统api [英] Can the synchronous file system api be used in a chrome extension
问题描述
我在google扩展中创建页面操作弹出窗口。当弹出窗口关闭时,我需要将内容写回文件。为此,我附加了 window.unload
事件。在试图写入文件的卸载处理程序中。但是,由于我正在使用异步api,因此在设置 getFile
函数的successHandler后,页面会关闭,并且successHandler不会被执行。我可以在这里使用同步文件API吗?我读到,同步apis只能用于webworkers。它可以用于扩展吗?还是有其他方法来处理这种情况。我粘贴我的代码以写入文件到这里
fileSystem.root.getFile(fileName,{create:true,函数(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)
。然后我的 window.unload
侦听器被调用,我的程序控制到达内容脚本并且代码正在运行。但是当我实际部署扩展时,当用户点击弹出窗口以外的地方时弹出窗口将被关闭。在这种情况下,卸载处理程序不会被调用。当弹出窗口关闭时,Google不会提供任何事件来检测。提供的唯一事件是点击事件,如果页面操作具有弹出窗口,则该事件不会被触发。我能做些什么来检测弹出窗口何时关闭?
为了找出答案,创建一个测试用例。以下代码已在后台页面的控制台中进行测试。
webkitRequestFileSystem(TEMPORARY,1024); // TypeError:没有足够的参数
以下测试用例使用网络工作者,正常工作:
//为测试目的创建一个工作者
var blob = new Blob([
'onmessage = function e){postMessage(eval(e.data))}'
],{
类型:'application / javascript'
});
var worker = new Worker((window.webkitURL || URL).createObjectURL(blob);
worker.onmessage = function(e){console.log(e.data)};
//测试:
worker.postMessage('webkitRequestFileSystem(TEMPORARY,1024)'); //没有错误
//为了比较,当出现错误时检查是否收到错误:
worker.postMessage('webkitRequestFileSystem(0)'); // E:没有足够的参数
因此,同步的FileSystem API在Chrome扩展的范围内不受支持。
而不是执行虚拟I / O在弹出窗口中,使用 chrome.extension.getBackground()
或 chrome.runtime.sendMessage
将数据传递到背景,并让高亮显示ound处理它。
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?
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.
这篇关于可以在chrome扩展中使用同步文件系统api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!