Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome Chrome扩展-消息从后台传递到内容脚本_Google Chrome_Google Chrome Extension - Fatal编程技术网

Google chrome Chrome扩展-消息从后台传递到内容脚本

Google chrome Chrome扩展-消息从后台传递到内容脚本,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,我发现了很多方法可以主动将内容中的数据发送到background.js。 但是,是否有任何主动方式可以通知background.js或将消息从background.js发送到contents脚本? 似乎实现消息传递的常用方法是在后台添加Listener,并从内容中传递消息并获取返回值作为回调。 由于端口在我打开新页面时不断变化,因此打开端口似乎不是一个选项。 我需要解决的办法是:从后台发送消息,内容已被listner删除。 我的用例的详细信息是.. 我在background.js(chrome

我发现了很多方法可以主动将内容中的数据发送到background.js。
但是,是否有任何主动方式可以通知background.js或将消息从background.js发送到contents脚本?
似乎实现消息传递的常用方法是在后台添加Listener,并从内容中传递消息并获取返回值作为回调。
由于端口在我打开新页面时不断变化,因此打开端口似乎不是一个选项。
我需要解决的办法是:从后台发送消息,内容已被listner删除。

我的用例的详细信息是..
我在background.js(chrome.cookies.onChanged.addListener)中有Cookie更改侦听器
每次cookies发生更改/更新时,我都会通知内容脚本。

在background.js中

 function cookieUpdate(info) {
 // need to  notify to contents script
 };

 chrome.cookies.onChanged.addListener(cookieUpdate);

尝试chrome.tabs.sendRequest(tabId,{},callback)

最后,我发现我需要chrome.tabs.sendMessage