1

I saved a variable in key: data Console.log(data.key), gives me out my data.

But now I send a message to the background.js from my popup.js and it doesnt read line:

sendResponse(data.key);

result = undefined, I tried to move it out of chrome.storage.local.get("key", function(data) { }) and it worked with a test variable.

Popup.js:

chrome.runtime.sendMessage({from: 'popup',subject: 'ubisoft'}, function (result) {

        window.alert(result)
        
      });

Background.js:

  chrome.runtime.onMessage.addListener((msg, sender,sendResponse) => {

    // First, validate the message's structure.
    if ((msg.from === 'popup') && (msg.subject === 'ubisoft')) {
      // Enable the page-action for the requesting tab.
      chrome.storage.local.get("key", function(data) {
        // Do something with data.key
        console.log(data.key)
        sendResponse(data.key);
      });
    }
  });
  • Code within the callback of `chrome.storage` executes asynchronously; you need to inform Chrome that you will reply asynchronously by returning `true` from the message listener. – Xan Jun 14 '22 at 13:02

0 Answers0