Chrome 확장 프로그램 개발을 만들려고합니다. 팝업 페이지와 배경 페이지 사이에서 간단한 작업을 수행하려고합니다. 지금까지는 페이지가 있고 팝업은 배경 및 배경 페이지 답장으로 메시지를 보낼 수 있습니다. 나는 현재 내가 원하는 동작을 만드는 손실 오전 :크롬 확장 프로그램 setTimeout 및 메시징
- 이 팝업을 가지고
- 카운트 다운이 완료되는 수
- 의 기간 동안 배경 페이지를 실행 되세요 배경 페이지에 번호를 보내 메시지를 반환 완료 시점의 팝업.
이 시점에서 백그라운드 프로세스는 즉시 응답을 보내고 올바르게 수행하지 않을 것이라고 확신합니다. 여기에 몇 가지 코드를입니다
팝업 :
chrome.extension.sendMessage({
method: "popup",
message: "remindMe",
now: parseInt(localStorage.timeNow),
timeToRemindIn: 5
},
function(response) {
console.log("sendMessage");
console.log(response.farewell);
//alert(response.farewell);
console.log("sendMessage after");
배경 :
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
//console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.message == "remindMe") {
startTime();
if(localStorage.timeToRemindIn > now) {
sendResponse({farewell: "reminder to check"});
}
} else {
sendResponse({farewell: "hai hai"});
}
});
function startTime() {
now = parseInt(now) + 1;
if(localStorage.timeToRemindIn > now) {
console.log("Reminder to check this tab Yo! " + now);
return;
}
if (localStorage.timeToRemindIn <= now) {
timerId = window.setTimeout(startTime, pollInterval);
}
}
});
나는 '지금'이 선언되지 않았기 때문에 어떤 오류도 발생하지 않았다는 것에 놀랐습니다. 코드를 작동시키기위한 최소한의 요구 사항은 청취자의 맨 위에있는'now = 1'입니다. 이는 문제를 해결할 수 있지만 잘못된 코드 설계는 해결하지 못할 수 있습니다. 글로벌 변수로 인해 확장 프로그램은 한 번에 하나의 요청 만 처리 할 수 있습니다. 보다 나은 해결책은'startTime()'의 반환 값을 사용하고,'startTime'에 초기 시간을 전달하는 것입니다. –
고마워요. @RobW 저는 실수로 여기에 그 코드를 놓쳤습니다. 아직도 작동하지 않습니다. 나는 startTime()의 반환 valye를 사용하는 다른 제안을 변경할 것이다. –