Hello I am creating an chrome extension and I am trying to put all my timestamps that a retrieved after visiting a url into an array in local storage but I am having trouble doing so. It continues to update timestamps and i want all my timestamps to stay the same. However it doesnt seem to save anything and i keep receiving this error. Do anyone know how to fix this error or what may be causing it.
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");
})
}
"Unexpected token u at position 0" means that you're trying to parse an undefined. Replace
var data = [];
data = JSON.parse(localStorage.getItem('session')) || [];
with
var data = JSON.parse(localStorage.getItem('session') || "[]");
There is a bug in the code.
data = JSON.parse(localStorage.getItem('session')) || [];
should be:
data = JSON.parse(localStorage?.getItem('session') ?? []);
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.