[英]How to put timestamps within array in local storage?
您好,我正在创建一个 chrome 扩展,并且我正在尝试将访问 url 后检索到的所有时间戳放入本地存储中的数组中,但我在这样做时遇到了麻烦。 它继续更新时间戳,我希望我所有的时间戳保持不变。 但是它似乎没有保存任何东西,我一直收到这个错误。 有谁知道如何解决此错误或可能导致它的原因。
function addRow() {
const bg = chrome.extension.getBackgroundPage()
Object.keys(bg.get).forEach(function (url) {
// Append product to the table
var data = [];
data = JSON.parse(localStorage.getItem('session')) || [];
// Push the new data (whether it be an object or anything else) onto the array
data.push( Date.now() );
localStorage.setItem('session', JSON.stringify(data));
myDate = data;
//protocol
var arr = url.split("/");
var protocoll = arr[0] + "//" + arr[2];
//inputed data --
browser= "Google Chrome";
protocol = protocoll;
downloads = "example";
description = "example";
//use data or myDate as time
time = myDate;
//will put dates in array and replace it and have var as myDate
// add new row to the table
//1 = in the top
//table.rows.length = the end
//table.rows.length/2+1 = the center
var newRow = table.insertRow(0);
//console.log(table.rows.length) gives length of table
// add cells to the row
var browserCell = newRow.insertCell(0);
var timeCell = newRow.insertCell(1);
var urlCell = newRow.insertCell(2);
var protocolCell = newRow.insertCell(3);
var downloadsCell = newRow.insertCell(4);
var descCell = newRow.insertCell(5);
// add the data to the cells
urlCell.innerHTML = `${url}`;
timeCell.innerHTML = time;
browserCell.innerHTML = browser;
descCell.innerHTML = description;
protocolCell.innerHTML = protocol;
downloadsCell.innerHTML = downloads;
console.log("add row works");
})
}
“position 0 处的意外令牌 u”表示您正在尝试解析未定义的。 代替
var data = [];
data = JSON.parse(localStorage.getItem('session')) || [];
和
var data = JSON.parse(localStorage.getItem('session') || "[]");
代码中有一个错误。
data = JSON.parse(localStorage.getItem('session')) || [];
应该:
data = JSON.parse(localStorage?.getItem('session') ?? []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.