Definitions: Please note from the outset that by 'injected script', 'extension code' and 'content script' I will be using the definitions provided in the excellent first answer to this question.
Assumption: Handling confidential information is less secure if I do it directly within my injected script (in the web zone) than if I do it within the chrome:// zone of content scripts and extension code. I therefore should use message passing to send confidential information from the web zone to the chrome:// zone for it to be handled.
Question: I'm building a Google Chrome extension where I need to run some operations on sensitive user data derived from my injected script. The data in question is confidential and I must do all I can to ensure that it can't be seen by anyone but the user of the extension until I've operated on it. Of the 3 techniques (defined below) that can be used to pass messages between an injected script and extension code/content script which would be best for this purpose?
My understanding of the 3 different techniques that can be used for passing data between an injected script and extension code/content script:
For messaging passing between an injected script and extension code (e.g. a background page), one can use the chrome.runtime API.
For messaging passing between an injected script and a content script one can use window.postMessage.
Another way of passing messages between an injected script and a content script is via document.dispatchEvent(CustomEvent).
My understanding is that method 1. cannot be used for message passing between an injected script and a content script while methods 2. and 3. cannot be used for message passing between an injected script and extension code (unless the message is forwarded by the content script to, for example, a background page).