简体   繁体   中英

Chrome extension to save data to local file

I'm making chrome extension, which will save every action in browser like new_tab, new_window, switch_tab, close_tab... But I want to store information locally in csv file. I tried many guides and tutorials, but still not working. For start I just want to save test string into local file, will be this file visible? I am planning getting this data from about 20 people.

In manifest:

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "This is testing description",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_title": "Click here!"
  },
  "permissions": [
    "activeTab",
    "tabs",
    ]
}

In popup.html is imported javascript <script type="text/javascript" src="popup.js"></script>

and in popup.js is this:

window.onload = function() {
var filesystem = null;
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;

window.requestFileSystem(window.PERSISTENT, 5*1024*1024 , onInitFs, errorHandler);

  function onInitFs() {
  filesystem = fs;

  filesystem.root.getFile('treehouse.txt', {create: true}, function(fileEntry) {

  fileEntry.createWriter(function(fileWriter) {

  fileWriter.onwriteend = function(e) {
   console.log("write done");
  };

  fileWriter.onerror = function(e) {
    console.log("write error");
  };

  var contentBlob = new Blob(['JUST OWN TEXT FOR TESTING!'], {type: 'text/plain'});

  fileWriter.write(contentBlob);

}, errorHandler);
}, errorHandler);

}


function errorHandler(e) {
  var msg = '';

  switch (e.message) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };
  console.log('Error: ' + msg);
}
}

Giving error from console Uncaught ReferenceError: fs is not defined on line filesystem = fs; No file was created or I didnt find it. I assume root is somewhere C:/ or C:/Windows

Can anybody help with code repair? or some useful advice?

你需要将onInitFs()改为onInitFs(fs)因为window.requestFileSystem函数会将你的新文件系统句柄作为回调的参数传递给你。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM