【发布时间】:2019-06-06 13:30:03
【问题描述】:
我正在编写一个 Chrome 扩展弹出窗口来登录我的服务器。该扩展有一个基本形式,包括username、password 和一个submit 按钮。
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary btn-sm" id="loginButton">Log In</button>
</form>
我使用 Insomnia REST 客户端测试了我的服务器响应:
网址:https://myserver.com/login
标头:Content-Type: application/x-www-form-urlencoded
表单 URL 编码:email: email@domain.com & password: password
在我的 Chrome 扩展程序上,我编写了一个 signin.js 脚本来处理按钮单击事件并将请求发送到我的服务器。
// hardcoded for simplicity of this example
const email = email@domain.com
const pwd = password
var button = document.getElementById("loginButton");
button.addEventListener("click", function(){
const req = new XMLHttpRequest();
const baseUrl = "https://myserver.com/login";
const urlParams = `email=${email}&password=${pwd}`;
req.open("POST", baseUrl, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send(urlParams);
req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log("Got response 200!");
}
}
});
然后在我的manifest.json 文件上,我拥有以下权限:
"permissions": [
"storage",
"activeTab",
"cookies",
"*://*.myserver.com/*"
],
扩展加载并运行没有错误,但我在 DevTools 的网络选项卡上看不到请求。我可以看到所有文件都已加载,但没有请求 myserver.com
请求的网址是Request URL: chrome-extension://ahlfehecmmmgbnpbfbokojepnogmajni/sign_in.html?
【问题讨论】:
-
确保您查看的是correct devtools window。
-
哦,是的,我打开的是弹出式开发工具,而不是我当前的浏览器会话
-
假设您点击了 chrome://extensions UI 页面中的重新加载图标,尝试调试您的代码:在点击监听器中设置断点并逐行运行。
标签: javascript ajax google-chrome-extension