我如何存储值在数组和重用它在chrome扩展存储的每一个会话

How can I store values in an array and reuse it every session in chrome extension storage?

本文关键字:存储 chrome 扩展 会话 每一个 数组 何存储      更新时间:2023-09-26

我试图使一个数组与id(用户)的用户点击。函数是:

$('button').click(function(){
var value = $('this').parents('.username').attr('id')
// How to create array in the chrome storage?
// How can I push the value to an array and create a permanent one?
})

您可以尝试这样做:

chrome.storage.local.get("myArray", function(dataStored) {  //load your saved array, if it exists
  var workingArray = dataStored.myArray || [];  //if it doesn't exist, create an empty one
  workingArray.push(value);  //add the value just obtained
  chrome.storage.local.set({myArray:workingArray});  //save the array.
});