最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - send data from background.js to popup.html - Stack Overflow

programmeradmin0浏览0评论

In my background.js, it had ajax loaded when a URL is matched during the on-load. For instance, it's google 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.

In my background.js, it had ajax loaded when a URL is matched during the on-load. For instance, it's google. 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.

Share Improve this question asked Sep 1, 2014 at 15:40 Adam91Adam91 831 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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
});

See here and here

Can you edit your question to include your manifest.json? That's often very helpful when making suggestions.

Assuming that your popup.html is part of a content script, you need to use chrome.tabs.sendMessage to send messages from the background page to content scripts.

Also consider using chromeps for simple pubsub style message propagation throughout your extension without having to worry about when to use chrome.runtime vs chrome.tabs.

发布评论

评论列表(0)

  1. 暂无评论