2013-09-08 5 views
0

Chrome 앱에서 콘텐츠의 백그라운드 스크립트로 메시지를 보내려고합니다. 난 (상기 코드에 기술 된 바와 같이) 지정된 콜백 콘텐츠 sendMessage 호출하면Chrome 앱에서 callback이 포함 된 sendMessage가 오류를 발생시킵니다.

// background.js 
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { 
    console.log(message) 
    sendResponse("hey man!"); 
}); 

// content.js 
chrome.runtime.sendMessage("hello world!", function(response) { 
    console.log(response); 
}); 

,이 에러로 끝날 :

리스너가 움직이게되지
Port: Could not establish connection. Receiving end does not exist. 

하지만 콜백은 (정의되지 않은 응답). sendMessage 인수에서 콜백을 생략하면 (메시지 만 전달되므로) listener가 예상대로 실행됩니다.

콜백 함수를 설정하는 방법을 알 수 없습니다. 누군가?

+0

샘플을 보았습니까? https://github.com/GoogleChrome/chrome-app-samples/blob/master/messaging/app1/index.js. 작동하는지 확인한 다음 원하는대로 수행하도록 수정하십시오. – sowbug

답변

0

이 두 기능을 window-state 샘플 Chrome 30.0.1599.22에 추가하면 문제의 코드가 올바르게 작동합니다.