0

Chrome 확장 프로그램 (popup.html)에서 선택된 탭에 삽입 된 스크립트로 메시지를 보내는 중 문제가 있습니다. 다음과 같이 popup.html의 코드는 다음과 같습니다확장 프로그램에서 탭으로 메시지 보내기

alert("sending MSG"); 
chrome.tabs.sendMessage(null, {greeting: "hello"}, function(response) { 
console.log(response.farewell); 
}); 
alert("MSG send"); 

문제는 단지 "보내는 MSG"경고가 나타납니다하지만 두 번째 경고를 표시하지 않습니다 "MSG 보내"는 것이다. 마치 코드를 막는 것과 같습니다. 여기

chrome.tabs.getSelected(null, function(tab) { 
chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) { 
console.log(response.farewell); alert("MSG sent _ in"); 
             }); 
         }); 
alert("MSG send _ out"); 

내가 같은 문제를 얻을 :

심지어 나는이 기능을 사용할 때 "에 _ MSG 보내"표시됩니다 "_ MSG 보내 밖으로"는이 있지만이 없습니다. 누군가이 문제에 관해 어떤 생각을 갖고 있다면 알려주십시오.

답변

0

팝업 관리자를 보면 팝업에 런타임 오류가 있음을 알 수 있으며 따라서 마지막 alert이 작동하지 않습니다. 당신이 chrome.tabs.sendMessage 대신 chrome.extension.sendMessage를 사용해야합니다

해결 방법은 간단하다

alert("sending MSG"); 
chrome.extension.sendMessage(null, {greeting: "hello"}, function(response) { 
    console.log(response.farewell); 
}); 
alert("MSG send"); 
관련 문제