Iam trying to create a extension of manifest v3 where iam sending message to a particular tab from background script and returning Promise.resolve from the listener in content script,but the repsonse in then in undefined every time. Heres the code:
Background.js
async function processRunData() {
if(runData.params.length>0){
let action = runData.params.splice(0,1)[0];
try {
await actionExecution(action.target,action.command,action,action.value);
}
catch(e) {
console.log("error",e);
}
await processRunData();
}
}
async function actionExecution(path,action,actionSet,value="")
{
switch (action) {
case "sleep": {
await timeout(Number(path));
break;
}
case "open": {
let window = await chrome.windows.create({focused: true,state:"maximized", url:path });
await chrome.storage.local.set({'activeWindow': window.id});
await chrome.storage.local.set({'runWindow': window.id});
runningTabID = window.tabs[0].id;
break;
}
default: {
await messageContentScript(actionSet);
break;
}
}
}
async function messageContentScript(instruction)
{
const message = {"action": "executeAction" ,instruction};
let tab = await chrome.tabs.get(runningTabID);
if(tab.status !="complete")
{
await waitForUpdate(runningTabID);
}
await chrome.tabs.sendMessage(runningTabID, message).then(playResponse);
}
async function playResponse(response) {
//Here iam always getting response as undefined
if(response == null) {
await timeout(1000);
}
else if(response.answer == "instructOK") {
console.log("instruct ok")
}
else if(response.answer == "error") {
console.log(response.error);
}
}
function errorResponse(e){
console.log("error from content script",e);
}
Content.js
chrome.runtime.onMessage.addListener((request, sender,sendResponse) => {
if(request.action == "executeAction") {
try {
executeAction(request.instruction, request);
return Promise.resolve({answer: "instructOK"});
}
catch(e) {
return Promise.resolve({answer: "error"});
}
}
});
I want response to contain object with key answer but getting undefined.