警报不适用于Chrome扩展程序的弹出窗口 [英] Alert is not working in pop up window in chrome extension

查看:147
本文介绍了警报不适用于Chrome扩展程序的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我,找出我的问题。我将我的网站设置为Chrome扩展名。当我安装扩展程序时,它会导航到一个弹出窗口,询问用户名和密码以及登录按钮。但是当我试图提醒用户在javascript中输入的用户名时,它不起作用。任何人都请帮助我。那是什么原因?
这是我的manifest.json

  {
name:Calpine Extension,
version:1.0,
description:登录到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
},
权限:[

*://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>
函数log(){
var uname = document.getElementById('name');
alert(uname);
}
< / script>
< / head>
< body>
< form name =userinfoid =userinfo>
用户名:
< input id =nametype =textname =username/>< br>< br>
密码:
< input type =buttonvalue =Log Inonclick =log()/>
< p id =pp>< / p>
< / form>
< / body>
< / html>


解决方案

$ c> test.html 作为扩展的视图打开,并且 内容安全策略(CSP) 可防止执行内联脚本和内联事件绑定。



您应该将代码和事件绑定移动到外部JS文件中。
$ b test.html


< head>
< script type =text / javascriptsrc =test.js>< / script>
< / head>
< body>
...
< input type =buttonid =loginvalue =登录/>
...
< / body>
< / html>

test.js p>

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

login.addEventListener('click',function(){
// ...做东西...
});
});


Could any one help me,to find out my problem.I have set my website as chrome extension.When I install the extension,it navigates to a popup window asking user name and password along with a login button. But when I tried to alert the username entered by the user in javascript,it is not working.Anyone please help me.What would be the reason? Here is my 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"]

 }

Here is my code that creates a pop up window on installation

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

here is my 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>

解决方案

The reason it is not working is that test.html is opened as a "view" of your extension and the Content Security Policy (CSP) prevents inline scripts and inline event-binding from being executed.

You should move the code and the event-bindings to an external JS file.

In test.html:

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

In test.js:

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

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

这篇关于警报不适用于Chrome扩展程序的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆