Google chrome extension &引用;chrome.runtime.onMessage";:在创建新选项卡之前发送响应

Google chrome extension &引用;chrome.runtime.onMessage";:在创建新选项卡之前发送响应,google-chrome-extension,response,Google Chrome Extension,Response,我在chrome.notifications.onClicked.addListener中发送响应时遇到问题 内容\u脚本 chrome.runtime.sendMessage({action:'openlink',url:'http://stackoverflow.com“},函数(响应){ 控制台日志(响应); }); 背景 chrome.runtime.onMessage.addListener(函数(请求、发送方、发送响应){ if(request&&request.action=='

我在
chrome.notifications.onClicked.addListener
中发送响应时遇到问题

内容\u脚本

chrome.runtime.sendMessage({action:'openlink',url:'http://stackoverflow.com“},函数(响应){
控制台日志(响应);
});
背景

chrome.runtime.onMessage.addListener(函数(请求、发送方、发送响应){
if(request&&request.action=='openlink'){
chrome.notifications.create('MyUniqueID'{
类型:“基本”,
标题:“开放链接”,
消息:request.url,
iconUrl:'icon.jpg'
});
chrome.notifications.onClicked.addListener(函数('MyUniqueID')){

sendResponse({action:'notification-clicked'});//必须在收到邮件后立即使用sendResponse作为回复发件人的方式:好的,我收到了您的邮件,没问题,因此您可以继续使用代码

当您需要在您的情况下发送这样的消息时,您需要再次调用该函数:

chrome.tabs.sendMessage

这意味着在后台和内容中有chrome.runtime.onMessage.addListener


有关参考信息,请参见:

不,只需在后面添加
返回真值

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
   if(request && request.action === 'openlink') {
      chrome.notifications.create('MyUniqueID',{
         type: 'basic',
         title: 'Open link',
         message: request.url,
         iconUrl: 'icon.jpg'
      });
      chrome.notifications.onClicked.addListener(function('MyUniqueID') {
         sendResponse({action:'notification-clicked'}); // <--- This
         chrome.tabs.create({url:request.url});
      });
      return true;
   }
});
chrome.runtime.onMessage.addListener(函数(请求、发送方、发送响应){
if(request&&request.action=='openlink'){
chrome.notifications.create('MyUniqueID'{
类型:“基本”,
标题:“开放链接”,
消息:request.url,
iconUrl:'icon.jpg'
});
chrome.notifications.onClicked.addListener(函数('MyUniqueID')){
sendResponse({操作:'notification-clicked'})//