From aff7c50f94c0041e30b5d9373dc03a7db7e7b953 Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 4 Apr 2022 11:58:50 +0700 Subject: [PATCH] fix backgound.js listener callback, take 3 --- extension/background.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/extension/background.js b/extension/background.js index 893d0b7..7345256 100644 --- a/extension/background.js +++ b/extension/background.js @@ -116,21 +116,25 @@ async function downloadLink(toDownload) { } -// event listener for messages from script.js and popup.js -browserType.runtime.onMessage.addListener( - (data, sender) => { - console.log("message background.js listener: " + JSON.stringify(data)) - if (data.verify === true) { - let response = verifyConnection() - return Promise.resolve(response); - } else if (data.youtube) { - setYoutubeLink(data) - } else if (data.download) { - let response = downloadLink(data.download.url) - return Promise.resolve(response); - } +// process and return message if needed +function handleMessage(request, sender, sendResponse) { + console.log("message background.js listener: " + JSON.stringify(request)); - return false; - + if (request.verify === true) { + let response = verifyConnection(); + response.then(message => { + sendResponse(message); + }) + } else if (request.youtube) { + setYoutubeLink(request) + } else if (request.download) { + let response = downloadLink(request.download.url); + response.then(message => { + sendResponse(message) + }) } -); + + return true; +} + +browserType.runtime.onMessage.addListener(handleMessage);