0

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.

Ravi Teja
  • 11
  • 1
  • onMessage doesn't support Promise. Use sendResponse({answer: "instructOK"}) instead. – wOxxOm Nov 07 '22 at 14:23
  • I want to wait for my executeAction method to complete and then want to sendResponse and send next action from background.js via messageContentScript method , but if i use sendResponse all the sendMessage are sent instantaneously to content script? – Ravi Teja Nov 07 '22 at 14:54
  • 1
    Use `return true`, [more info + example](https://stackoverflow.com/a/59915897). – wOxxOm Nov 07 '22 at 15:51
  • https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/sendMessage I see here we can return promises..Is it only for v2 and not for V3 as i see few extensions based on v2 using the promise approach? – Ravi Teja Nov 08 '22 at 06:58
  • This is not chrome extensions API. Here's the correct documentation: [link](https://developer.chrome.com/extensions/messaging). – wOxxOm Nov 08 '22 at 08:00

0 Answers0