使用多个页面的加载项,我在其中调用内容脚本和附加脚本中的相应HTML页面。当我尝试在HTML页面中获取文本框的值时,为什么我会得到空。
HTML:
<html>
<head>
<title>
Dummy Page
</title>
</head>
<body>
<input type="text" class="login-field" placeholder="username" id="usermail" value="heelo">
<label class="login-field-icon fui-user" for="login-name"></label>
<input type="password" class="login-field" placeholder="password" id="password" value="heells">
<label class="login-field-icon fui-lock" for="login-pass"></label>
</body>
</html>
ContentJS
self.port.on("get-first-para", getFirstPara);
function getFirstPara() {
var userId = document.getElementById("usermail").value;
var pass = document.getElementById("password").value;
if (userId.length > 0 && pass.length > 0) {
var firstPara = userId + " ** " + pass;
self.port.emit("first-para", firstPara);
}
}
Addon Js
var data = require("sdk/self").data;
var pageWorkers = require("sdk/page-worker");
var self = require("sdk/self");
require("sdk/ui/button/action").ActionButton({
id: "Mailer",
label: "Click to start",
icon: {
"16": "./icon-16.png",
"32": "./icon-32.png",
"64": "./icon-64.png"
},
onClick: handleClick
});
var textChk = require("sdk/panel").Panel({
position: {
top: 0,
right: 0
},
hight: 100,
contentURL: data.url("textChk.html"),
contentScriptFile: data.url("content.js")
});
function handleClick() {
textChk.show();
pageWorker = require("sdk/page-worker").Page({
contentScriptFile: self.data.url("content.js")
});
pageWorker.port.on("first-para", function(firstPara) {
console.log(firstPara);
});
pageWorker.port.emit("get-first-para");
}
需要知道我怎么能摆脱null错误?
答案 0 :(得分:1)
您向pageWorker
发出“get-first-para” - 在您的代码页中,工作人员无内容,但 SAME 脚本为< strong>小组(textChk)...另一方面,textChk 有(html)内容和相同的content.js - 您是否意味着要做以下?
function handleClick() {
textChk.show();
textChk.port.on("first-para", function(firstPara) {
console.log(firstPara);
});
textChk.port.emit("get-first-para");
}