admin管理员组

文章数量:1418592

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.

本文标签: javascriptsend data from backgroundjs to popuphtmlStack Overflow