Google chrome 如何在后台页面中获取本地存储值?

Google chrome 如何在后台页面中获取本地存储值?,google-chrome,google-chrome-extension,local-storage,Google Chrome,Google Chrome Extension,Local Storage,我是开发chrome扩展的新手 我正在测试一个扩展。在这里,我有一个背景页面和选项页面。在选项页面中,我将用户输入的密码存储在本地存储器中,我希望在单击浏览器图标的背景页面中检索该密码 我的选项页是: <html> <head> <title>Options for Password</title> <script> function savePassword() { window.localStorage.setItem('p

我是开发chrome扩展的新手

我正在测试一个扩展。在这里,我有一个背景页面和选项页面。在选项页面中,我将用户输入的密码存储在本地存储器中,我希望在单击浏览器图标的背景页面中检索该密码

我的选项页是:

<html>
<head>
<title>Options for Password</title>
<script>
 function savePassword()
  {
 window.localStorage.setItem('password', txtPassword.value);


  }
</script>   
</head>
<body >
<h1>Enter Password</h1>
<input type='password' name='txtPassword' id='txtPassword'/>
<br />
<button onclick='savePassword();'>Save</button>
<br />
</body>
</html>

密码选项
函数savePassword()
{
window.localStorage.setItem('password',txtPassword.value);
}
输入密码

拯救
我的背景页是:

<html>
<head>
<script>
 // Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {code:"alert('password from tab:'+window.localStorage.getItem('password'));getfeedback_dialog_submitButton.setAttribute('onclick', 'if(gettext.value==\'"+window.localStorage.getItem(password)'+"\'){document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);}');"});
});


</script>
</head>
</html>

//当用户单击浏览器操作时调用。
chrome.browserAction.onClicked.addListener(函数(选项卡){
chrome.tabs.executeScript(null,{code:'警报('来自选项卡的密码:'+window.localStorage.getItem('password'));getfeedback_对话框_submitButton.setAttribute('onclick','if(gettext.value==\'”+window.localStorage.getItem(password)+“\”){document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);“);”;
});
当我通过“选项”页面保存密码,然后单击“浏览器操作”时,我将从tab:null获取警报密码


我不知道如何检索存储在本地存储器中的值

您的代码从单击浏览器操作的页面获取localStorage值,该值为
null
。更改您的报价以在背景页获取:

chrome.tabs.executeScript(null, {code:"alert('password from tab:"+window.localStorage.getItem('password')+"');"});
注意,这可能允许代码注入

编辑:执行此操作时,只要变量定义正确并且
window.localStorage.getItem('password')
应为字符串,它就可以工作:

chrome.tabs.executeScript(null, {code:"alert('password from tab:"+window.localStorage.getItem('password')+"');"
+"getfeedback_dialog_submitButton.addEventListener('click', function(event){if(gettext.value=='"+window.localStorage.getItem('password')+"')"
+"{document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);}},false);"});

为了便于阅读,我将您的代码分为多行。

谢谢您的回复。你能帮我一下吗?我想测试存储的值是否等于某个文本框输入的值?如果(gettext.value==window.localStorage.getItem(密码)){document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);}我必须测试以下条件:我如何在这个块的代码中写入它?@Pankaj Khurana如果你也在注入代码,只要做
如果(gettext.value=\'“+window.localStorage.getItem(密码)“+”\”{…
但我建议使用一个外部脚本。我使用了下面的代码,但是没有发生任何事情(没有错误):getfeedback\u dialog\u submitButton.setAttribute('onclick','if(gettext.value=\'”+window.localStorage.getItem(password)+“\”{document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);}');“}”);
onclick
在页面的上下文中运行,而不是在内容脚本的上下文中运行,您的引用可能不起作用。但是您可以在另一个问题中问这个问题还是编辑您的问题?我已经编辑了我的问题,并添加了检查用户输入是否等于存储值的代码