如何将消息从content-script发送到不活动的弹出窗口,扩展名为chrome [英] Howto send a message from content-script to inactive popup, chrome extension
问题描述
我想将任何网页上的文本(使用content-script处理选择)记录到弹出式资源的数据库中,以便在一个位置收集文本。
我在尝试
我在弹出页面创建一个数据库并尝试通过使用content-script来管理它,尽管弹出窗口并未激活(未打开) Chrome消息传递,但无法让弹出窗口收到来自content-script的任何消息。
我不确定如何使用消息传递来解决此问题。
有没有更好的解决方案?
内容脚本不能发送消息给不可见的弹出窗口,上下文在隐藏时处于非活动状态(关闭状态)。
有几种解决方案可以解决您的问题。
选项1 :没有消息传递,使用存储事件
如果您的数据库实际上是一个简单的键值存储,请切换到 chrome.storage
API。此API可用于内容脚本和弹出窗口,并附带一个事件以通知您值更改。
$ b 示例:
//获取变化的通知(在弹出窗口中)
chrome.storage.onChanged.addListener(function(changes, areaName){
//做任何你想要的改变。
});
//初始化弹出(打印初始信息?)
chrome.storage.local.get({keyName:'defaultValue'},函数(items){
//做某事items.keyName
});
//内容脚本,存储(记得文档标题?)
chrome.storage.local.set({keyName:document.title});
选项2:将消息传递到背景/活动页面
弹出窗口和背景 / event 页面共享相同的过程。绑定到弹出窗口的任何数据库也可用于后台页面,反之亦然。对此方法的高级概述:
- 内容脚本将消息发送到后台页面。
- 后台页面存储数据库中的值
- 如果弹出窗口打开,更新弹出窗口的视图。
- 如果弹出窗口打开< sup>(因此之前关闭),它应该读取数据库(直接或通过使用
chrome.runtime.getBackgroundPage
)并处理结果。
我在这个答案。
I want to log text on any webpage (using content-script to handle selection) into a database that is popup's resource in order to collect text in one place.
what i am trying I create a database in popup page and try to manage it from content-script though popup is not active (not opened) by using chrome messaging but cannot make the popup receives any message from content-script.
I'm not sure about using messaging to solve this problem. Is there any better solution?
A content script cannot send a message to an invisible popup, because the popup's context is inactive (closed) when it's hidden.
There are several solutions to your problem.
Option 1: No message passing, use storage events
If your "database" is in fact a simple key-value store, switch to the chrome.storage
API. This API is available to the Content script and the popup, and comes with an event to notify you of value changes.
Example:
// Get notified of changes (in the popup?)
chrome.storage.onChanged.addListener(function(changes, areaName) {
// Do whatever you want with the changes.
});
// Initialization of the popup (print initial information?)
chrome.storage.local.get({keyName: 'defaultValue'}, function(items) {
// Do something with items.keyName
});
// Content script, storage (remember document title?)
chrome.storage.local.set({keyName: document.title});
Option 2: Pass messages to the background/event page
The popup and the background / event page share the same process. Any database tied to the popup is also available to the background page, and vice versa. A high-level overview of this method:
- Content script sends a message to the background page.
- The background page stores the value in the database
- If the popup is open, update the popup's view.
- If the popup is opened (so it was closed before), it should read the database (either directly, or by reading data from the background page using
chrome.runtime.getBackgroundPage
) and handle the results.
I've provided the code corresponding to this flow in this answer.
这篇关于如何将消息从content-script发送到不活动的弹出窗口,扩展名为chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!