1

我有一個chrome擴展,在內容和後臺腳本之間來回發送和接收消息。chrome.tabs.sendMessage當標籤不集中

一切工作正常與chrome.runtime,但我使用chrome.tabs.sendMessage使用這樣的事情將消息發送到內容腳本:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 
    chrome.tabs.sendMessage(tabs[0].id, {method: "stuff"}, function(response) { }); 
}); 

延長只有在少數特定的URL運行,當代碼執行時即時通訊在與擴展程序運行不同的選項卡上,或在後臺檢查視圖中,它不起作用。

即使在代碼運行時選項卡處於活動狀態/集中狀態,我如何才能使其工作?

+0

你想將消息發送給所有選項卡的工作??? –

回答

0
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { 
console.log(sender.tab.id); 
}); 

使用sender.tab.id而不是tabs[0].id似乎做