如何在选项页面保存到localStorage并在后台页面获取?

发布于 2024-12-11 19:26:29 字数 2642 浏览 0 评论 0原文

我仍在尝试将 options.html 中输入的用户电子邮件保存到 background.html 中的 extension_user 变量,如建议的在此答案中。我希望有人能提供帮助。

在选项页面中,我有一个文本区域 id="getEmail",它获取用户电子邮件并将输入的电子邮件保存到 localStorage

//save entered gmail address
document.getElementById("save").addEventListener(
    "click", function ()
    {
        var user = document.getElementById("getEmail").value;
        localStorage.setItem("user", user);
        console.log("gmail id saved as " + user);
    } , false)

现在我想在 中获取此电子邮件>background.html 并将其保存为“extension_user”。在 background.html 中,我

if (localStorage.getItem("user"))
{
    user = localStorage.getItem("user")
    //localStorage.clear()
    console.log("if(!user): " + user)
    //...    
    // run normal program
    //...
}
else
{
    chrome.tabs.create({url: "/options.html"});
    console.log("options page opened as new tab");
}

发生的情况是,仅当我在选项页面中保存电子邮件后重新加载扩展程序时,后台页面才会看到保存的“用户”。

我缺少什么?

使用消息传递将选项中保存的用户电子邮件直接发送到后台页面是不是更好?我感谢任何有关将此用户电子邮件永久保存在后台页面(无论是否使用本地存储)的建议。谢谢!

更新由serg的回答

background.html

function refreshUser ()
{
    user = localStorage.getItem("user");
    console.log("user inside of refreshUser function " + user);
}

user = localStorage.getItem("user");
if (!user)
{
    refreshUser(); 
    //user is not defined outside refreshUser()
    console.log("user outside of refreshUser function" + user);
    //run the rest of code here
    //...
}
else
{
    user = localStorage.getItem("user"); 
    console.log("this is the else close")
    //run the rest of code here
    //...

}

更新

@serg 我更新了代码如下,但我仍然看到newUser在函数 refreshUser() 内部更新,但在函数外部为 null。我做错了什么?

newUser = localStorage.getItem("user");
console.log("first newUser " + newUser);
//newUser=null

function refreshUser() 
{
    newUser = localStorage.getItem("user");
    console.log("newUser inside of refreshUser function " + newUser);
    //newUser is updated with saved email in localStorage
}

console.log("newUser after refreshUser() " + newUser);
//newUser=null

更新

按照serg的回答,这有效:

function refreshUser () 
{
    newUser = localStorage.getItem("user");
}

...

formData.append("extension_user", newUser);

I am still trying to save a user email entered in options.html to the extension_user variable in background.html as suggested in this answer. I hope someone can help.

In options page I have a textarea id="getEmail" that takes the user email and saves the entered email to localStorage:

//save entered gmail address
document.getElementById("save").addEventListener(
    "click", function ()
    {
        var user = document.getElementById("getEmail").value;
        localStorage.setItem("user", user);
        console.log("gmail id saved as " + user);
    } , false)

Now I want to pick up this email in background.html and save it as "extension_user". In background.html I have

if (localStorage.getItem("user"))
{
    user = localStorage.getItem("user")
    //localStorage.clear()
    console.log("if(!user): " + user)
    //...    
    // run normal program
    //...
}
else
{
    chrome.tabs.create({url: "/options.html"});
    console.log("options page opened as new tab");
}

What happens is that background page sees the saved "user" only if I reload the extension after I saved the email in options page.

What am I missing?

And is it better to use message passing and send the user email saved in options directly to background page? I appreciate any suggestions to save this user email in background page permanently with or without localStorage. Thanks!

UPDATE by serg's answer

background.html

function refreshUser ()
{
    user = localStorage.getItem("user");
    console.log("user inside of refreshUser function " + user);
}

user = localStorage.getItem("user");
if (!user)
{
    refreshUser(); 
    //user is not defined outside refreshUser()
    console.log("user outside of refreshUser function" + user);
    //run the rest of code here
    //...
}
else
{
    user = localStorage.getItem("user"); 
    console.log("this is the else close")
    //run the rest of code here
    //...

}

UPDATE

@serg I updated the code as follows but I still see that newUser is updated inside the function refreshUser() but it is null outside the function. What am I doing wrong?

newUser = localStorage.getItem("user");
console.log("first newUser " + newUser);
//newUser=null

function refreshUser() 
{
    newUser = localStorage.getItem("user");
    console.log("newUser inside of refreshUser function " + newUser);
    //newUser is updated with saved email in localStorage
}

console.log("newUser after refreshUser() " + newUser);
//newUser=null

Update

Following serg's answer, this works:

function refreshUser () 
{
    newUser = localStorage.getItem("user");
}

...

formData.append("extension_user", newUser);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-12-18 19:26:29

浏览器启动时加载后台页面,并始终保持加载状态。当您在选项页面中保存用户时,后台页面不知道本地存储值已更改,因此您需要以某种方式通知它。

最简单的方法是在用户更新后调用后台页面内的某些方法:

//options
localStorage.setItem("user", user);
chrome.extension.getBackgroundPage().refreshUser();

//bg page
function refreshUser() {
    //user in localStorage is updated, read it
}

Background page gets loaded when browser starts and remains loaded all the time. When you saved user inside options page, background page doesn't know that localstorage value has changed, so you need to notify it somehow.

The easiest approach would be calling some method inside background page once user is updated:

//options
localStorage.setItem("user", user);
chrome.extension.getBackgroundPage().refreshUser();

//bg page
function refreshUser() {
    //user in localStorage is updated, read it
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文