0

Can I use chrome.runtime port.postMessage to send messages from background script to content script?

I have seen answers of using chrome.tabs.sendMessage but I have an existing code and I wanted to make use of this code with minimal changes.

Thanks in advance.

Maninder
  • 1,261
  • 5
  • 20
  • 34
  • If you open a port you can send messages in both directions so I don't see what's the problem. – wOxxOm Oct 03 '17 at 14:11
  • Is this possible in Firefox because I want to port an existing chrome extension to Firefox addon? – Maninder Oct 03 '17 at 15:26
  • Why not? Firefox implements [most of the APIs](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities). The question is too broad so I'm not sure what answer you expect. – wOxxOm Oct 03 '17 at 15:29
  • To narrow down the question the answer given here https://stackoverflow.com/questions/14245334/chrome-extension-sendmessage-from-background-to-content-script-doesnt-work uses chrome.tabs api , I want to use port.PostMessage to send message from background script to content script. Unfortunately that doesn't seems to be working for me? Just wanted to check if chrome.tabs api is the only way around. – Maninder Oct 03 '17 at 16:00
  • 1
    The problem might be caused by something else so it's worth describing it in detail. Off the top of my head, I can only suggest reversing the process and let the content script initiate messaging either via chrome.runtime.connect or chrome.runtime.sendMessage. – wOxxOm Oct 03 '17 at 16:04
  • Which part of the [description and example of this process in the documentation](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Connection-based_messaging) was unclear? – Makyen Oct 07 '17 at 05:58
  • Your question can be interpreted in a few different ways. Please [edit] your question to clarify what you're asking. – Makyen Oct 07 '17 at 06:02

0 Answers0