From 1c4c4d5b6ca411ceb44ef444132fedb0a06f622d Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 4 Oct 2024 13:49:19 +0300 Subject: [PATCH] Run global steamdb script at document start and wait for dom to load before sending message --- manifest.json | 1 + scripts/steamdb/global.js | 46 +++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/manifest.json b/manifest.json index adeaffca..b1c6bab4 100644 --- a/manifest.json +++ b/manifest.json @@ -83,6 +83,7 @@ "content_scripts": [ { + "run_at": "document_start", "matches": [ "https://steamdb.info/*" diff --git a/scripts/steamdb/global.js b/scripts/steamdb/global.js index b7f8f18f..ccc99786 100644 --- a/scripts/steamdb/global.js +++ b/scripts/steamdb/global.js @@ -60,26 +60,40 @@ GetOption( { 'steamdb-highlight': true }, ( items ) => contentScriptQuery: 'FetchSteamUserData', }, ( response ) => { - if( response.error ) + console.log( document.readyState ); + + const OnPageLoaded = () => { - WriteLog( 'Failed to load userdata', response.error ); + if( response.error ) + { + WriteLog( 'Failed to load userdata', response.error ); - window.postMessage( { - version: EXTENSION_INTEROP_VERSION, - type: 'steamdb:extension-error', - error: `Failed to load your games. ${response.error}`, - }, GetHomepage() ); - } + window.postMessage( { + version: EXTENSION_INTEROP_VERSION, + type: 'steamdb:extension-error', + error: `Failed to load your games. ${response.error}`, + }, GetHomepage() ); + } - if( response.data ) - { - window.postMessage( { - version: EXTENSION_INTEROP_VERSION, - type: 'steamdb:extension-loaded', - data: response.data, - }, GetHomepage() ); + if( response.data ) + { + window.postMessage( { + version: EXTENSION_INTEROP_VERSION, + type: 'steamdb:extension-loaded', + data: response.data, + }, GetHomepage() ); - WriteLog( 'Userdata loaded', `Packages: ${response.data.rgOwnedPackages.length}` ); + WriteLog( 'Userdata loaded', `Packages: ${response.data.rgOwnedPackages.length}` ); + } + }; + + if( document.readyState === 'loading' ) + { + document.addEventListener( 'DOMContentLoaded', OnPageLoaded, { once: true } ); + } + else + { + OnPageLoaded(); } } ); } );