Communicating between a Chrome packaged app and a

2019-01-07 16:43发布

问题:

I need to combine functionality available only in a Chrome packaged app (access to syncFileSystem) and functionality available only in a Chrome extension (injecting a script into a 3rd party website).

It seems that neither a packaged app nor an extension can achieve both these things, so I'm now considering trying to achieve what I'm after with a separate packaged app and extension communicating.

I see that Chrome's documentation explains how two extensions can communicate via chrome.runtime.onMessageExternal.addListener and chrome.runtime.sendMessage, but nothing about packaged apps and extensions communicating.

Does anyone know if this is allowed? Is there any documentation, or a working example out there?

回答1:

Yes, that is possible. The code sample in the documentation you linked works for any combination of app and extension.

The extension documentation for chrome.runtime.sendMessage says:

Sends a single message to onMessage event listeners within the extension (or another extension/app).

Messaging works the same in both extensions and apps, and they seem to be fully compatible; simply use the ID for the destination extension or app. If you look at the docs for the app version of chrome.runtime.sendMessage, you'll see that it is identical to the extension version.