0
有没有办法在一个内部有一个全局变量content.js?目前,这是我的代码,但它给了我“未定义” ......铬扩展的内容中的全局变量.js
window.addEventListener("message", function(event) {
// We only accept messages from ourselves
if (event.source != window)
return;
if (event.data.type && (event.data.type == "FROM_PAGE")) {
url = event.data.text;
window.username = event.data.subject;
window.password = event.data.end;
alert(window.username);
alert(window.password);
chrome.extension.sendRequest({redirect: url});
}
}, false);
function get_passwords() {
var node_list = document.getElementsByTagName('input');
console.log(node_list);
var textboxes = [];
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
console.log(node);
if (node.getAttribute('type') == 'password') {
textboxes.push(node);
}
}
textboxes[0].value = window.password;
}
从你的代码中不清楚,但我想也许全局变量是好的,但他们得到的价值比你想象的晚。你确定他们首先得到了他们的价值,只有在以后你分配文本框[0] .value = window.password? –
您的代码不会告诉调用了哪个'get_passwords()'。确保它在事件触发后发生。另外,避免使用变量名称'username'和'password',它们在某些浏览器中具有奇怪的行为。 – Skwal