0

I have this code in content script in chrome extension.

new MutationObserver(() => {
    var dataImage: any = document.getElementsByClassName('image');
    var dataName: any = document.getElementsByClassName('name');
    var dataTitle: any = document.getElementsByClassName('title');

    var listData: Array<any> = [];

    for (let x = 0; x < dataImage.length; x++) {
      const image = dataImage[x]?.firstElementChild?.src;
      const name = dataName[x]?.innerText;
      const title = dataTitle[x]?.firstElementChild?.innerText;
      const web = dataName[x]?.firstElementChild?.href;

      listData.push({
        image: image,
        name: name,
        title: title,
        web: web,
      });
    }

    const getData = (listData: any) => {
      chrome.runtime.sendMessage({
        total_elements: listData, // or whatever you want to send
      });
    };
    getData(listData);
    localStorage.setItem('taskdata', JSON.stringify(listData));
}).observe(document, { subtree: true, childList: true });

popup.js

    chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
      if (request.total_elements) localStorage.setItem('leadList', JSON.stringify(request.total_elements));
    });
     const getLeadList = localStorage.getItem('leadList');
    const initLeadList = getLeadList === null ? [] : JSON.parse(getLeadList);

initLeadList.map((x) => {
return (
<>{x.name}{x.title}
</>
)
}

How can I reload my popup in realtime when content js data is updated?

My popup only run when I reload inside, then close then re open again. How could I get the new data reflect in popup in realtime?

is it window.open the window.close?

How can I reload my popup in realtime when content js data is updated?

My popup only run when I reload inside, then close then re open again. How could I get the new data reflect in popup in realtime?

user16691768
  • 93
  • 1
  • 8
  • Use MutationObserver fully, not just to observe the URL, but to observe the added elements, there are many examples around. – wOxxOm Aug 19 '21 at 12:29
  • thank you. it worked. how about, popup will reload when content was changes? – user16691768 Aug 19 '21 at 12:50
  • Call chrome.runtime.sendMessage from the content script and use `location.reload()` in the popup's onMessage. – wOxxOm Aug 19 '21 at 15:05

0 Answers0