阅读本文(create a file using javascript in chrome on client side)之后,我想将此功能合并到用javascript编写的chrome扩展中。
扩展非常简单。它会查找一个按钮,如果找到该按钮,则会单击该按钮。单击按钮后,我希望扩展程序创建一个文件,所以为了测试这个,我只是在点击功能后从上面的链接复制到代码。添加代码以创建文件后,扩展程序将不再单击该按钮。也许它与代码的排序方式有关?任何帮助将不胜感激。
if (document.getElementsByName("submit")[0] != null) {
document.getElementsByName("submit")[0].click();
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
fs.root.getFile('test.bin', {create: true}, function(fileEntry) { // test.bin is filename
fileEntry.createWriter(function(fileWriter) {
var arr = new Uint8Array(3); // data length
arr[0] = 97; // byte data; these are codes for 'abc'
arr[1] = 98;
arr[2] = 99;
var blob = new Blob([arr]);
fileWriter.addEventListener("writeend", function() {
// navigate to file, will download
location.href = fileEntry.toURL();
}, false);
fileWriter.write(blob);
}, function() {});
}, function() {});
}, function() {});
}