Ask Your Question
3

What is the method to transfer information from a popup to the main content in a Chrome extension?

asked 2023-02-23 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-01-21 17:00:00 +0000

david gravatar image

One method to transfer information from a popup to the main content in a Chrome extension is through message passing using the chrome.runtime.sendMessage and chrome.runtime.onMessage API.

  1. In the popup script, send a message with the information using chrome.runtime.sendMessage:
chrome.runtime.sendMessage({data: info});
  1. In the main content script, listen for the message using chrome.runtime.onMessage:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if (message.data) {
    // use the information from the message
  }
});
  1. Make sure both scripts have access to the chrome.runtime API by including it in the permissions section of the extension's manifest file:
"permissions": [
  "tabs",
  "activeTab",
  "storage",
  "http://*/",
  "https://*/",
  "chrome://favicon/",
  "chrome://extensions/",
  "chrome://resources/"
]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-23 11:00:00 +0000

Seen: 12 times

Last updated: Jan 21 '23