chrome.fileSystem-保留/恢复多个文件条目

chrome.fileSystem - retain/restore multiple file entries?

本文关键字:文件 恢复 fileSystem- 保留 chrome      更新时间:2023-09-26

我有两个函数:saveData()loadData()saveData()运行所有对象,并(假定)用chrome.storage.local.set设置字符串ID和值,稍后用loadData()返回。现在,我这么说是因为它不起作用。重新启动应用程序时的结果是保存的最后一个条目。

//I have an array 
//called 'fileEntries'
//which the user adds fileEntry objects to
//when they add a tab or open a file
var fileEntries = [];
function saveData(){ 
  fileEntries.forEach(function(entry){
    //the string id should just be the
    //index of the entry object within the array
    //so something like '1' or '2'
    var stringID = fileEntries.indexOf(entry),
        value = chrome.fileSystem.retainEntry(entry);
    chrome.storage.local.set({ stringID : value });
  });
}
function loadData(){
  chrome.storage.local.get({stringID : null}, function(result){
    chrome.fileSystem.restoreEntry(result.stringID, function(entry){
      //adds the resulting entry into the array
      fileEntries.push(entry);
    })
  })
}

有人知道怎么帮忙吗?这真的一直困扰着我,我尝试了很多其他方法,但都无济于事。如有任何帮助,我们将不胜感激:)

如果问题不清楚:如何保存多个文件条目,然后使用chrome fileSystem API分别恢复它们?

使用.forEach()的第二个参数;例如。;.forEach(function(_, enttry){}),其中entry是数组内迭代元素的index.forEach()回调的第一个参数是value。如果不使用value,则可以使用下划线来指示省略其使用。然后,您仍然可以在.forEach()回调中使用js的剩余部分中的现有entry变量名。