javascript - 如何将时间戳放在本地存储中的数组中?
问题描述
您好,我正在创建一个 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");
})
}
解决方案
“位置 0 处的意外标记 u”表示您正在尝试解析未定义的。代替
var data = [];
data = JSON.parse(localStorage.getItem('session')) || [];
和
var data = JSON.parse(localStorage.getItem('session') || "[]");
推荐阅读
- java - 为什么我的 Scanner 不能使用 2 个不同的 if 语句?
- php - 查询解决方案
- c# - 带有 SkiaSharp 的 UWP ScrollViewer.TopHeader 不绘图
- python - 如何从列表中打印出每 50 个值?
- node.js - 在一次 api 调用中保存到多个数据库时返回状态
- javascript - 动态加载 JavaScript 库并调用
- javascript - 未找到 aws 配置凭据
- ubuntu - Puppeteer:使用 WebGL 的页面在 Ubuntu 服务器上需要 70 秒才能完成,而在 Windows 桌面上需要 1 秒
- python - 在 Termux 安卓终端模拟器上安装 python3.6
- sql - 两个日期之间的差异,一个日期作为没有时区的时间戳