Chrome 扩展:使我的扩展仅在单击弹出窗口时执行并在弹出窗口中显示结果
正如它清楚地显示的那样,
我希望我的弹出窗口成为扩展程序启动的触发器。 我的扩展基本上可以完全在background.html
中执行。 Background.html
需要来自网页的一些信息,因此使用消息传递将所需内容从 contentscript.js
传递到 background.html
。在所有页面上运行这会占用大量资源,因此我希望此扩展仅在单击图标后启动,并在 popup.html
中显示信息,我可以直接从 中提取信息背景.html
。
让我知道我是否不清楚。
As it clearly reads,
I want my popup to be the trigger for the extension to start.
My extension basically can be performed completely in background.html
. Background.html
needs some info from the webpage, so use message passing for passing the required content from contentscript.js
to background.html
. This is becoming resource heavy to run on all the pages, So i want this extension to start only after the icon is clicked and display the information in the popup.html
which i can directly pull from background.html
.
Lemme know if i'm unclear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检测弹出窗口何时打开非常容易,因为每次打开弹出窗口时都会执行其中的代码。因此,您所需要做的就是在弹出代码的开头向后台页面发出请求。
第二部分是按需注入内容脚本(据我所知,注入当前选项卡)。
所以整个扩展结构应该是这样的:
popup.html
background.html
需要记住的是,弹出窗口在收到消息时可能已经关闭了数据,因此您可能需要添加一些错误处理。
第二个问题是避免将内容脚本两次注入同一页面。这可能具有挑战性,所以我建议如果您的内容脚本很小,那么只需将其注入所有页面(在清单中)。
Detecting when a popup is opened is pretty easy as the code inside it executes each time the popup is opened. So all you need to do is put a request to background page at the beginning of popup code.
Second part would be injecting content script on demand (into the current tab as I understand).
So the whole extension structure should be something like this:
popup.html
background.html
Something to keep in mind is that the popup might be already closed by the time it receives the data, so you might need to add some error handling.
Second issue would be avoiding injecting content script twice into the same page. This might be challenging, so I would suggest if your content script is small then just inject it into all pages (in manifest).