KartikTalwar/gmail-chrome-extension-boilerplate

Communication with background script

brandonhilkert opened this issue · 2 comments

This isn't an issue with the library itself, but just hoping to get you feedback. If the content script was just a normal script and didn't embed js links in the dom, you could communicate with a background script using:

    chrome.runtime.sendMessage data, (response) ->

However, if you embed the main.js in to the and I define some behavior on the read_email event, I need to send some messages to a background script. Because I'm now in the actual tab, not in the content script, I haven't been able to use chrome.runtime.sendMessage because it seems like it's out of context of the extension at that point.

Do you have suggestions on getting back in to the extension?

This link demonstrates what I think you are trying to achieve. This is devtools specific, but the concept is the same for injected scripts. You can add window.postMessage to main.js, and listen for the message in the content.js

diagram adapted from that link:

main.js
     |
     | window.postMessage()
     V
content.js
     |
     | chrome.runtime.sendMessage()
     V
background script