在Chrome扩展中写入本地文件系统 [英] Writing to local file system in Chrome extension

查看:1592
本文介绍了在Chrome扩展中写入本地文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  chrome.tabs.onUpdated.addListener(checkForValidUrl); 
函数checkForValidUrl(tabId,changeInfo,tab){
if(tab.url.indexOf('https')> -1){
var tabURL = tab.url;
console.log(\\\
< TimeStamp>+ getTimestamp()+< / TimeStamp><浏览器> Chrome< /浏览器><网址>+ tabURL +< / URL> \\\
);
window.requestFileSystem(window.PERSISTENT,5 * 1024 * 1024,initFs);

函数initFs(fs){
fs.root.getFile
('log.txt',{create:true,exclusive:true},function(fileEntry){
fileEntry.isFile = true;
fileEntry.name ='log.txt';
fileEntry.fullPath ='/log.txt';
fileEntry.createWriter(function(fileWriter) {
fileWriter.seek(fileWriter.length);
var bb = new BlobBuilder();
bb.append(\\\
< TimeStamp>+ getTimestamp()+< / TimeStamp>< Browser> Chrome< / Browser>< URL>+ tabURL +< / URL> \\\
);
fileWriter.write(bb.getBlob('text / plain') );
});
});




解决方案


在撰写本文时,
Google Chrome 9+是唯一可以工作的
实现FileSystem API。
由于专用的浏览器用户界面不存在用于文件/配额管理的
,因此
无法在没有运行
的情况下使用API​​,因为
- unlimited-配额(注意:如果您为Chrome网上应用店构建应用程序或
扩展程序,
unlimitedStorage清单文件
权限就足够了)。


http:// www.html5rocks.com/tutorials/file/filesystem/#toc-support



我假设您使用的是Chrome,但您尚未设置 - 无限配额文件标志


chrome.tabs.onUpdated.addListener(checkForValidUrl);
function checkForValidUrl(tabId, changeInfo, tab) {
    if (tab.url.indexOf('https') > -1) {
        var tabURL = tab.url;
        console.log("\n<TimeStamp>" + getTimestamp() + "</TimeStamp><Browser>Chrome</Browser><URL>" + tabURL + "</URL>\n");
        window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024, initFs);

        function initFs(fs) {
            fs.root.getFile
            ('log.txt', { create: true, exclusive: true }, function (fileEntry) {
                fileEntry.isFile = true;
                fileEntry.name = 'log.txt';
                fileEntry.fullPath = '/log.txt';
                fileEntry.createWriter(function (fileWriter) {
                    fileWriter.seek(fileWriter.length);
                    var bb = new BlobBuilder();
                    bb.append("\n<TimeStamp>" + getTimestamp() + "</TimeStamp><Browser>Chrome</Browser><URL>" + tabURL + "</URL>\n");
                    fileWriter.write(bb.getBlob('text/plain'));
                });
            });
        }
    }
}

解决方案

Based on this:

At the time of writing this article, Google Chrome 9+ has the only working implementation of the FileSystem API. Since a dedicated browser UI does not yet exist for file/quota management, the API cannot be used without running Chrome with the --unlimited-quota-for-files flag (Note: if you're building an app or extension for the Chrome Web Store, the unlimitedStorage manifest file permission will suffice).

found at http://www.html5rocks.com/tutorials/file/filesystem/#toc-support

I assume you are using Chrome and that you have not set the --unlimited-quota-for-files flag

这篇关于在Chrome扩展中写入本地文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆