0

I am using following code to get game name from the Twitch page. But it looks like 'chrome.scripting.executeScript' runs before page loading finished. Sometimes it returns 'null', sometimes it returns the game name. Looks like race condition. In case of Twitch I see browser shows that it is finished loading, but probably angular/react loads more.

How to solve the problem?

chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {

    await chrome.scripting.executeScript({
      target: { tabId: tabId },
      function: getTwitchGameName},
      (scriptResults) => { console.log('Game name: ' + scriptResults[0].result); });
  }
});

function getTwitchGameName() {
  return document.querySelector('a[data-a-target=\"stream-game-link\"] span')?.innerText;
}
AlexRUiLs
  • 11
  • 4
  • Look at this SO answer - [How to execute content script after the page is loaded completely](https://stackoverflow.com/questions/28202736/how-to-execute-content-script-after-the-page-is-loaded-completely/28203168) – arjunsiva Jan 31 '22 at 11:04
  • Add a delay before running executeScript e.g. `await new Promise(r => setTimeout(r, 1000))` – wOxxOm Jan 31 '22 at 11:08
  • Looks Good, please add more relatable Tags to reach more – Charlie Feb 09 '22 at 12:53

0 Answers0