나는 크롬 확장에 popup.html과 background.js가있는 표준 사용 사례가 있습니다. 나는 사용자가 팝업에서 버튼을 클릭하면 2 일크롬에서 전달되는 메시지를 사용하는 경우
- 을 할 필요가 이제
이러한 기능을 구현하기 위해 팝업하는 배경에서 일부 데이터를 보내기에 대해 background.js 알려 크롬 확장 문서에서 설명한대로 long-lived connections을 사용하는 줄을 생각하고있었습니다. 그 측정하려면,이게 내가 그러나, 나뿐만 아니라 background.js
에 대한 sendMessage
기능을 수행하는 방법에 대한 손실에 오전,이 완벽하게 잘 작동
# popup.js
var port = chrome.extension.connect({name: "LOGGERCHANNEL"});
function sendMessage(msg) {
port.postMessage(msg);
port.onMessage.addListener(function(msg) {
console.log("message recieved"+ msg);
});
}
sendMessage("hello world");
# background_page.js
chrome.extension.onConnect.addListener(function(port){
console.log("Connected to Popup");
console.assert(port.name == "LOGGERCHANNEL");
port.onMessage.addListener(function(msg){
console.log('Background got message:', msg);
});
});
을 것입니다. 이상적으로 특정 이벤트가 발생할 때 sendMessage('hello from bg')
을하고 싶습니다. 이것이 어떻게 가능할까요?
두 번째로 this answer에 요약 된대로 변수를 공유하여 chrome.extension.getViews()
및 chrome.extension.getBackgroundPage()
을 간단하게 사용할 수 있습니다.
이런 종류의 문제를 해결하기위한 권장 방법은 무엇입니까?