警报在Chrome扩展程序的弹出窗口中无效

时间:2013-12-02 06:19:44

标签: javascript google-chrome-extension popupwindow

任何人都可以帮助我,找出我的问题。我已将我的网站设置为chrome扩展名。当我安装扩展程序时,它会导航到一个弹出窗口,询问用户名和密码以及登录按钮。但是当我试图提醒用户在javascript中输入的用户名时,它无法正常工作。任何人请帮助我。这是什么原因? 这是我的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": "Test Extension",
    "default_icon": "calpine_not_logged_in.png"
},
"permissions": [

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

 }

以下是我在安装时创建弹出窗口的代码

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

这是我的test.html

<html>
<head>
    <script type="text/javascript">
        function log(){
            var uname=document.getElementById('name');
           alert(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()"/>
    <p id="pp"></p>
      </form>
   </body>
   </html>

2 个答案:

答案 0 :(得分:5)

它不起作用的原因是test.html作为您的扩展程序的“视图”打开,而 Content Security Policy (CSP) 会阻止内联脚本和内联事件绑定执行。

您应该将代码和事件绑定移动到外部JS文件。

test.html

<html>
    <head>
        <script type="text/javascript" src="test.js"></script>
    </head>
    <body>
        ...
        <input type="button" id="login" value="Log In" />
        ...
    </body>
</html>

test.js

window.addEventListener('DOMContentLoaded', function() {
    var login = document.querySelector('input#login');

    login.addEventListener('click', function() {
        // ...do stuff...
    });
});

答案 1 :(得分:0)

您必须获取输入字段的值:

var uname = document.getElementById('name').value;
相关问题