0

I have this code in content.js, wherein I want to pass the listData to popup.js using chrome.runtime. how can I possibly do that?

var dataImage: any = document.getElementsByClassName("image");
var dataName: any = document.getElementsByClassName("name");
var dataJobTitle: any = document.getElementsByClassName("title");

var listData: Array<any> = [];

for (let x in [dataImage, dataName]) {
  const image = dataImage[x]?.firstElementChild?.src;
  const name = dataName[x]?.innerText;
  const job = dataJobTitle[x]?.firstElementChild?.innerText;

  listData.push({ image: image, name: name, job: job });
}
console.log(listData);

These listData will be map as html <li> in popup

user16691768
  • 93
  • 1
  • 8

1 Answers1

0
**in contentScript.js:**
 chrome.runtime.sendMessage({
     total_elements: totalElements // or whatever you want to send
 });

 **in eventPage.js (your background page):**
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse){
   localStorage["total_elements"] = request.total_elements;
});

reference : Chrome Extension how to send data from content script to popup.html

Joel Wembo
  • 814
  • 6
  • 10
  • 1
    is there a way to directly do the addListener in popup.js? i tried to put the addListener in popup, but I receiving undefined per element in total_elements – user16691768 Aug 18 '21 at 03:32