EDIT: I found my solution here: Returning Chrome storage API value without function
I am building a chrome extension at the moment, and want to use the storage data. I managed to save it, and even the next day it's still there. So that portion of the work works. But I want to display the data I saved in the popup.
Let's say I want the user to enter their name and store that name. Then I want the popup to update and show "Welcome {name}". How should I do that?
I wrote this code, but it does nothing.
popup.js
const generateLinks = document.getElementById("generate-links");
const data = document.getElementById("data-token");
const form = document.getElementById("data-form");
const content = document.getElementById("content");
const textData = document.getElementById("saved-data-token");
let token = "";
let savedData = "";
function hasData() {
if (savedData !== "") {
textData.innerHTML = "Your data is: " + savedData;
}
}
hasData();
form.addEventListener("submit", (e) => {
e.preventDefault();
console.log(data.value);
token = data.value;
chrome.runtime.sendMessage(token, (response) => {
savedData = response;
});
content.classList.add("accepted");
});
I obviously must have done something wrong, can someone direct me to the correct solution?
I updated the code with the following and now get undefined
, but when I console.log it instead of return, I do get the data.
let savedData = chrome.storage.sync.get(["data"], (res) => {
return res.data;
});