Chrome Extension: How to pass data?

Viewed 14

I am trying to create a chrome extension but have encountered a very annoying but rudimental problem. How to pass data between background.js and popup.js?

I currently have this code in background.js that listens to URL updates and prints it to the console. I basically want to pass the URL to popup.js everytime it updates.

This is my code:

// background.js
let activeTabId, lastUrl, lastTitle;

function getTabInfo(tabId) {
  if (db) {
    return new Promise((resolve, reject) => {
      chrome.tabs.get(tabId, function (tab) {
        if (lastUrl != tab.url || lastTitle != tab.title)
          console.log((lastUrl = tab.url), (lastTitle = tab.title));
        lastUrl = tab.url;
        resolve(true);
      });
    });
  }
}

chrome.tabs.onActivated.addListener(function (activeInfo) {
  getTabInfo((activeTabId = activeInfo.tabId));
});

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
  if (activeTabId == tabId) {
    getTabInfo(tabId);
  }
});
0 Answers
Related