简体   繁体   English

Chrome扩展程序的content.js中的全局变量

[英]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.

相关问题 chrome扩展中的popup和content.js无法通信 - popup and content.js not communicating in chrome extension 在Chrome扩展程序的content.js中注入Google Recaptcha的JS文件 - Injecting Google recaptcha's JS file in a chrome extension's content.js Chrome扩展程序:无法将消息传递给content.js - Chrome Extension: Unable to pass message to content.js 在popup.html和content.js之间的Chrome扩展消息传递 - Chrome extension messaging between popup.html and content.js Chrome扩展程序-select2库可在控制台中使用,但不能在content.js中使用 - Chrome extension - select2 library working into console but not in content.js Chrome 扩展消息传递不起作用(background.js 到 content.js) - Chrome extension message passing not working (background.js to content.js) Chrome-扩展程序:将消息从background.js发送到content.js时出错 - Chrome - Extension: Error when sending messages from background.js to content.js 如何知道当前环境是chrome扩展中的content.js或background.js - how to know current environment is content.js or background.js in chrome extension 在Chrome扩展程序中将(对象的)数组从popup.js传递到content.js - Passing array (of objects) from popup.js to content.js in chrome extension 从background.js到content.js的chrome扩展消息,带有响应,TypeError无法读取属性 - chrome extension message from background.js to content.js with response, TypeError cannot read property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM