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;
}