Chrome 打包应用程序和 Chrome 扩展程序之间的通信? [英] Communicating between a Chrome packaged app and a Chrome extension?
问题描述
我需要结合仅在 Chrome 打包应用程序中可用的功能(访问 syncFileSystem)和仅在 Chrome 扩展程序中可用的功能(将脚本注入第 3 方网站).
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.
我看到Chrome的文档解释了两个扩展如何通过chrome.runtime.onMessageExternal.addListener
和 chrome.runtime.sendMessage
,但与打包应用程序和扩展程序通信无关.
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?
推荐答案
是的,这是可能的.您链接的文档中的代码示例适用于应用和扩展程序的任意组合.
Yes, that is possible. The code sample in the documentation you linked works for any combination of app and extension.
chrome.runtime.sendMessage
的扩展文档说:
The extension documentation for chrome.runtime.sendMessage
says:
向扩展程序(或其他扩展程序/应用)内的 onMessage
事件侦听器发送一条消息.
Sends a single message to
onMessage
event listeners within the extension (or another extension/app).
消息在扩展程序和应用程序中的工作方式相同,并且它们似乎完全兼容;只需使用目标扩展程序或应用程序的 ID.如果您查看 chrome.runtime.sendMessage
应用程序版本的文档,您会看到它与扩展版本相同.
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.
这篇关于Chrome 打包应用程序和 Chrome 扩展程序之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!