Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.0 kB
2
Indexable
Never

const handleScrape = async () => {
  try {
    const tabs = await queryTabs({ active: true, currentWindow: true });
    if (tabs.length > 0 && tabs[0].id !== undefined) {
      const response = await sendMessageToTab(tabs[0].id, { action: "getProfileData" });
      console.log(response);
      if (response) {
        if (response === "invalid address") return;

        setScrapingResult(response);
      }
    }
  } catch (error) {
    console.error("Error during scraping:", error);
  }
};

const queryTabs = (queryInfo) => {
  return new Promise((resolve, reject) => {
    chrome.tabs.query(queryInfo, (tabs) => {
      if (chrome.runtime.lastError) {
        return reject(chrome.runtime.lastError);
      }
      resolve(tabs);
    });
  });
};

const sendMessageToTab = (tabId, message) => {
  return new Promise((resolve, reject) => {
    chrome.tabs.sendMessage(tabId, message, (response) => {
      if (chrome.runtime.lastError) {
        return reject(chrome.runtime.lastError);
      }
      resolve(response);
    });
  });
};
Leave a Comment