[英]Writing to local file system in Chrome extension
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'));
});
});
}
}
}
基於此:
在撰寫本文時,Google Chrome 9+是FileSystem API唯一可用的實現。 由於文件/配額管理尚不存在專用的瀏覽器用戶界面,因此如果不使用--unlimited-quota-for-files標記運行Chrome,則無法使用該API(注意:如果您要為Chrome構建應用或擴展程序) Web Store,unlimitedStorage清單文件權限就足夠了)。
見http://www.html5rocks.com/tutorials/file/filesystem/#toc-support
我假設您使用的是Chrome,而您尚未設置--unlimited-quota-for-files標志
此文件系統API似乎並未實際將真正的“文件”寫入您的硬盤。 它似乎將文件存儲在瀏覽器的沙盒安全區域中。 您將不得不編寫一個快速而骯臟的小文件管理器(或在那里找到一個)來管理給定Web應用程序的文件。 您還可以嘗試訪問filesystem://<your URL here>/temporary/
以查看您的應用創建的所有文件。
那么只使用localStorage呢?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.