我的目标很简单:我想要一个面板,用户可以在其中指定用户名和密码并单击登录。然后我想做一个 webrequest 来验证凭据,并向用户显示响应。
我发现我必须在 main.js 中执行请求并将其传回。因此,当提交表单时,我构建了我的 url,向我执行请求的 main.js 发送一条消息。效果很好,响应也是正确的。但是,当我想将响应发送回面板时,出现此错误:
Error: The page is currently hidden and can no longer be used until it is visible again.
resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/content/worker.js 404
Traceback (most recent call last):
File "resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/net/xhr.js", line 126, in
self._orsc.apply(self, arguments);
File "resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/request.js", line 91, in onreadystatechange
emit(target, 'complete', response);
File "resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/event/core.js", line 83, in emit
for each (let item in emit.lazy.apply(emit.lazy, arguments)) {
File "resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/event/core.js", line 101, in lazy
yield listeners.shift().apply(target, args);
File "resource://jid1-aaautmtqen7pta-at-jetpack/domain/lib/main.js", line 21, in hosters<.onComplete
popup.port.emit("handleloginresponse",response);
File "resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/content/worker.js", line 369, in
emit: function () self._emitEventToContent(Array.slice(arguments))
File "resource://jid1-aaautmtqen7pta-at-jetpack/addon-sdk/lib/sdk/content/worker.js", line 404, in _emitEventToContent
throw new Error(ERR_FROZEN);
我还尝试再次打开面板或使用 postMessage() 代替。似乎没有任何效果,我无法将内容传输回我的面板!任何帮助都会得到帮助!
这是我的 main.js
var data = require("self").data;
var popup = require("panel").Panel({
width: 212,
height: 200,
contentURL: data.url("popup.html"),
contentScriptFile: [data.url("jquery.js"),data.url("popup.js")]
});
var Request = require("request").Request;
popup.port.on("handlelogin", function (url) {
console.log("requesting " + url);
var hosters = Request({
url: url,
onComplete: function (response) {
console.log(response.status);
console.log("emmiting handleloginresponse");
popup.port.emit("handleloginresponse",response);
}
}).get();
});
require("widget").Widget({
label: "my label",
id: "pop-up",
contentURL: data.url("16.png"),
panel: popup
});
popup.html
<html>
<head>
</head>
<body>
<form id="loginform">
<input name="user" placeholder="User" id="userfield" />
<input type="password" name="pass" placeholder="Password" id="passfield"/>
<input type="submit" id="loginbutton" value="Log in " />
</form>
</body>
</html>
popup.js
$('#loginform').submit(function (event) {
var url = 'https://mydomain.com?user=' + encodeURIComponent($('#loginform')[0].user.value) + '&pass=' + encodeURIComponent($('#loginform')[0].pass.value) + '&something=something;
self.port.emit("handlelogin",url);
});
self.on("handleloginresponse", function(data) {
console.log("got a handleloginresponse!");
console.log(data);
});