警报不适用于Chrome扩展程序的弹出窗口 [英] Alert is not working in pop up window in chrome extension
问题描述
这是我的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屋!