将数据从background.js发送到popup.html [英] send data from background.js to popup.html
问题描述
在 background.js
中,它在加载期间匹配URL时加载了一个jax。例如,它是google.com,它会触发一个ajax并接收一些数据。但如何将这些数据发送到我的popup.html?
我试过 chrome.runtime.sendMessage
但我不没办法。该参数发送到哪个文件?
我很困惑。 当您通过 chrome.runtime.sendMessage
发送数据时,您将能够通过监听 onMessage
事件来获取它。
当您从后台发送消息时.js像这样,
chrome.runtime.sendMessage({msg:'hello there'});
你可以在由popup.html加载的js文件中获得它:
chrome.extension.onMessage.addListener(function(message,messageSender,sendResponse){
// message是您发送的消息,可能是一个对象
// messageSender是一个包含有关发送消息的上下文信息的对象
// sendResponse是一个函数,当您有响应时
});
In my background.js
, it had ajax loaded when a URL is matched during the on-load. For instance, it's google.com and it fire an ajax and recieved some data. But how to send those data to my popup.html?
I tried chrome.runtime.sendMessage
but I don't get it. The param send to which file?
I'm confused.
when you send data via chrome.runtime.sendMessage
, you will be able to get it in your popup by listening to onMessage
events.
when you send a message from background.js like this,
chrome.runtime.sendMessage({msg: 'hello there'});
you can get it in a js file loaded by popup.html:
chrome.extension.onMessage.addListener(function(message, messageSender, sendResponse) {
// message is the message you sent, probably an object
// messageSender is an object that contains info about the context that sent the message
// sendResponse is a function to run when you have a response
});
这篇关于将数据从background.js发送到popup.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!