我正在开发一个Firefox扩展程序,该扩展程序要求用户提供凭据,而我面临一个奇怪的问题。根据我在代码中放置.send调用的位置,代码是否执行网络请求。
这是manifest.json(主机名和域已由伪造的主机替换):
ON
COALESCE(a.empid, -1) = COALESCE(b.empid, -1)
AND COALESCE(a.ename, 'mr_noname') = COALESCE(b.ename, 'mr_noname')
AND COALESCE(a.sal, -1) = COALESCE(b.sal, -1)
AND COALESCE(a.deptno, -1) = COALESCE(b.deptno, -1)
这是HTML弹出文件:
{
"applications": {
"gecko": {
"id": "extension@example.com",
"strict_min_version": "58.0a1"
}
},
"browser_action": {
"browser_style": true,
"default_title": "Title",
"default_popup": "mowl.html"
},
"description": "Description",
"homepage_url": "https://example.com",
"manifest_version": 2,
"name": "Name",
"permissions": [
"*://www.example.com/*",
"tabs"
],
"content_security_policy": "script-src 'self' https://www.example.com; object-src 'self'",
"version": "1.0"
}
最后是javascript代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="mowl.js"></script>
</head>
<body>
Please login: <br><br>
<input type='email' placeholder='Email address:'' name='email' id='email' size='50'><br><br>
<input type='password' placeholder='Password' name='password' id='password' size='50'><br><br>
<button id="signin">Ok</button>
</body>
</html>
使用调试器在Firefox中测试该代码,表明单击按钮时根本不执行网络请求。我将console.log语句放置在不同的位置,以验证是否在需要时调用了不同的函数。
但是,以这种方式修改javascript:
function processlogin() {
console.log('in processlogin');
if(http.readyState == 4) {
console.log(http.readyState);
}
}
function buttonClicked() {
document.querySelector('#signin').addEventListener('click', dologin, false);
}
function dologin () {
console.log('in dologin');
var website = "https://www.example.com/signin.php";
var params = "email=" + document.getElementById('email').value + "&password=" + document.getElementById('password').value;
http.open("POST", website);
http.responseType = 'text';
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = processlogin;
http.send(params);
}
document.addEventListener("DOMContentLoaded", buttonClicked);
var http = new XMLHttpRequest();
进行网络调用(显然无需单击按钮),并在Firefox调试器中进行报告。我不明白发生了什么。
有人可以向我提供一些有关我对此做错了什么的指导吗?
非常感谢!
答案 0 :(得分:0)
结果表明,无论代码在函数内部还是外部,网络请求均已完成。但是无论出于什么原因,F irefox调试器都不会在函数内部显示网络调用!,或者在知道之前就清除了输出,谁知道……