如何访问chrome扩展中新选项卡的弹出窗口的内容

时间:2013-11-29 09:12:28

标签: javascript html google-chrome-extension popupwindow

我开发了一个网站并设置为谷歌浏览器扩展程序。我需要的是,当我安装我的扩展程序时,应该在新窗口上打开弹出窗口,询问用户名和密码,并且必须将此用户名存储在应该可以在Chrome扩展程序中访问的本地存储。如何帮助我。 这是我的manifest.json

   {
"name": "Calpine Extension",
"version": "1.0",
"description": "Log on to calpinemate",
"manifest_version": 2,
"browser_action": {
    "default_icon": "icon_128.png"
},
"background": {
    "persistent": false,
    "scripts": ["background.js"]
},

"browser_action": {
    "default_title": "Calpine Extension",
    "default_icon": "calpine_not_logged_in.png" 

},
"permissions": [

   "*://blog.calpinetech.com/test/index.php",
    "alarms",
    "notifications"
 ],
  "web_accessible_resources": [
  "/icon_128.png"]

 }

这是我在background.js中弹出的代码

chrome.windows.create({url : "test.html"}); 

这是我在chrome.runtime.onInstalled.addListener()中写的。

这是我的test.html

<html>
<head>
    <script type="text/javascript">
        function log(){
            var uname=document.getElementById('name');
            alert(uname);
            //window.localStorage.setItem('uname', $("input[type='username']").val());
            //alert(localStorage.uname);
        }
    </script>
</head>
 <body>
<form name="userinfo" id="userinfo">
      username : 
      <input id="name" type="text" name="username"/><br><br>
      password :
      <input type="password" name="password"/><br><br>
       <input type="button" value="Log In" onClick="log()"/>
     </form>
  </body>
 </html>

现在我需要的是,当用户启用我的扩展时,必须在新窗口中打开一个弹出窗口,询问用户名和密码。当他输入用户名和密码时,用户名必须存储在可从以下位置访问的本地存储中镀铬扩展。我该怎么办?请帮帮我。

1 个答案:

答案 0 :(得分:0)

在您添加为脚本的任何js文件中使用此项。

//open new tab for installation after extension is added to chrome 
chrome.runtime.onInstalled.addListener(function (install) {
//code for installation
chrome.windows.create({ 'url': chrome.extension.getURL('test.html') }, function (tab) {
    //write the code to do something when tab is created on install()
});
});

在js代码中,单击登录按钮编写绑定事件。

用于设置localstorage,您可以使用。

window.localStorage.setItem('uname', $("input[type='username']").val());
window.localStorage.setItem('passwrod', $("input[type='password']").val());

为了得到它们,

window.localStorage.getItem('uname');
window.localStorage.getItem('passwrod');