[英]Global variable in chrome extension's content.js
Is there a way to have a global variable inside one content.js ? 有没有一种方法可以在一个content.js中包含一个全局变量? Currently, this is my code, but it gives me 'undefined'... 当前,这是我的代码,但是给了我“未定义” ...
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;
}
For a chrome extension content script, use this code in 对于chrome扩展程序内容脚本,请在
your content script: 您的内容脚本:
chrome.runtime.onMessage.addListener(function(msg,sender){
//msg is the message, sender is where it came from
});
and in your background page: 并在您的背景页面中:
chrome.tabs.sendMessage(tabToSendMessageTo.id, {
message: {"greeting":"Hello World!"}
});
Here's a link to the google documentation for message passing. 这是指向Google文档以进行消息传递的链接。 Message Passing 讯息传递
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.