我一直在研究chrome擴展,它需要在某些時候傳遞消息來觸發當前網頁上的一些事件。發送一個時間消息從背景頁面到devtool頁面
我一直在使用chrome.runtime.sendMessage將消息從devtool頁面發送到背景頁面。並在背景頁面上,我使用chrome.runtime.onMessage.addListener獲取這些消息,它正在工作。
但我不能從背景頁面發送消息到devtools頁面使用此! 我怎樣才能從背景頁面發送一次性消息到devtool頁面?
devtool.js
chrome.runtime.onMessage.addListener(function(request){
if(request.name=="xyz")
{
alert("HI");
}
});
background.js
chrome.runtime.sendMessage({
name : "xyz"
});
有人能幫助?
根據此[論壇](https://groups.google.com/forum/#!msg/google-chrome-developer-tools/Q3DbtWRlgLo/kEYPaW8h0sgJ),您不能使用chrome.extension.sendMessage ()'將消息發送到在DevTools環境中運行的擴展頁面。從背景頁面到DevTools的最佳溝通方式是使用'chrome.extension.connect'在DevTools頁面中打開一個端口,稍後在後臺頁面中使用port.postMessage()。 – abielita
@abielita _請將該評論寫入一個答案(但請注意'extension'應該由'runtime'替換) – Xan