从 Javascript 调用 Google Chrome 扩展
有一个名为 Blipshot 的出色扩展页面截图。我需要使用页面级 JavaScript 调用扩展,而不是单击其图标。这可能吗?
There is an excellent extension called Blipshot which takes page screenshots. I need to invoke the extension with page level javascript, instead of clicking its icon. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能从网页内调用扩展的任何方法。但是,可以将内容脚本注入网页,并使用
sendMessage
和onMessage
或onConnect
和connect.
要编辑扩展程序:访问
chrome://extensions
页面,然后启用开发者模式。解压扩展和/或访问扩展的目录。编辑manifest.json
文件,然后添加必要的行(请参阅此处)。在后台页面添加event事件监听器。在内容脚本中添加轮询器,例如:
另请参阅
postMessage
- 可用于页面和扩展之间的通信(当页面本身也使用message
事件时,此方法可能会导致冲突)。参考资料:
You cannot invoke any methods of an extension from within a web page. However, it's possible to inject a content script into the web page, and use
sendMessage
andonMessage
, oronConnect
andconnect
.To edit an extension: Visit
chrome://extensions
page, and enable the Developer mode. Unpack an extension and/or visit the extension's directory. Edit themanifest.json
file, and add the necessary lines (see here).Add an event event listener at the background page. Add a poller in the content script, eg:
See also
postMessage
- It can be used to communicate between a page and extension (this method may cause conflicts when the page itself is also using themessage
events).References:
只有当扩展提供一个接口来执行此操作时,这才是可能的。扩展在隔离环境中运行,因此您无法直接访问它们的任何功能。
他们得到的最接近的是内容脚本,它可以访问 DOM。因此,您可以使用事件进行通信,但显然扩展需要为它们设置事件处理程序,因此它完全取决于扩展。
It would only be possible if the extension provides an interface to do it. Extensions run in an isolated environment, so you don't have direct access to any of their functions.
The closest they get is content scripts, which have access to the DOM. Because of that, you can communicate using events, but obviously the extension would need to set up event handlers for them, so it completely depends on the extension.