@@ -59,15 +59,6 @@ function commenceExtensionInitialization(configurationObject) {
5959
6060 logConCgp ( '[init] Configuration has been received:' , configurationObject ) ;
6161
62- /**
63- * Checks whether the custom buttons modifications already exist in the DOM.
64- * @returns {boolean } - True if modifications exist, false otherwise.
65- * @description Helper function that checks if the custom buttons container exists in the DOM to prevent duplication.
66- */
67- function doCustomModificationsExist ( ) {
68- return document . getElementById ( window . InjectionTargetsOnWebsite . selectors . buttonsContainerId ) !== null ;
69- }
70-
7162 /**
7263 * Selects the correct initialization path based on the active website and sets up keyboard shortcuts if enabled.
7364 * Injects custom elements into the webpage accordingly.
@@ -78,7 +69,7 @@ function commenceExtensionInitialization(configurationObject) {
7869 const activeWebsite = window . InjectionTargetsOnWebsite . activeSite ;
7970 logConCgp ( '[init] Active website detected:' , activeWebsite ) ;
8071
81- // Initialize based on the active website
72+ // Initialize button injection logic (moved to separate file "button-injection.js")
8273 buttonBoxCheckingAndInjection ( true , activeWebsite ) ;
8374
8475 // Enable keyboard shortcuts if configured and on ChatGPT
@@ -124,106 +115,6 @@ function commenceExtensionInitialization(configurationObject) {
124115 }
125116 }
126117
127- /**
128- * Inserts custom buttons, separators and settings toggles into the webpage and starts resiliency checks if enabled.
129- * @param {boolean } enableResiliency - Flag to enable or disable resiliency checks.
130- */
131- function buttonBoxCheckingAndInjection ( enableResiliency = true , activeWebsite ) {
132- logConCgp ( '[init] Checking if mods already exist...' ) ;
133- if ( doCustomModificationsExist ( ) && ! enableResiliency ) {
134- logConCgp ( '[init] Modifications already exist and resiliency is disabled. Skipping initialization.' ) ;
135- return ;
136- }
137-
138- // Load the saved states of toggle switches
139- MaxExtensionInterface . loadToggleStates ( ) ;
140- logConCgp ( '[init] Toggle states have been loaded.' ) ;
141-
142- // Initialize the shared flag
143- let targetFound = false ;
144-
145- // Define the selector to wait for using InjectionTargetsOnWebsite
146- const selectors = window . InjectionTargetsOnWebsite . selectors . containers ;
147- // A unified callback function will search for div where we will insert stuff and
148- const handleTargetDiv = ( targetDiv ) => {
149- if ( ! targetFound ) {
150- targetFound = true ; // Set the flag to prevent other callbacks from executing
151- logConCgp ( '[init] Target div has been found:' , targetDiv ) ;
152- // Insert custom elements into the target container on the webpage
153- window . MaxExtensionButtonsInit . createAndInsertCustomElements ( targetDiv ) ;
154-
155- // Initiate resiliency checks only after the first successful modification
156- if ( ! globalMaxExtensionConfig . firstModificationCompleted && enableResiliency ) {
157- globalMaxExtensionConfig . firstModificationCompleted = true ;
158- logConCgp ( '[init] First modification complete. Starting resiliency checks.' ) ;
159- commenceEnhancedResiliencyChecks ( ) ;
160- }
161- }
162- } ;
163-
164- // Wait for the target element to appear in the DOM and then handle it
165- MaxExtensionUtils . waitForElements ( selectors , handleTargetDiv ) ;
166- }
167-
168- /**
169- * Initiates enhanced resiliency checks to ensure the extension remains functional on the webpage.
170- * Periodically checks if custom modifications exist and re-initializes if necessary.
171- */
172- function commenceEnhancedResiliencyChecks ( ) {
173- let consecutiveClearCheckCount = 0 ;
174- // This looks if the buttons present for few times, and tries to insert them again.
175- const requiredConsecutiveClearChecks = 2 ; // missing for this many times = reinsert
176- const maximumTotalIterations = 16 ;
177- let totalIterationsPerformed = 0 ;
178- const intervalTimeInMilliseconds = 50 ;
179-
180- logConCgp ( '[init] Beginning enhanced resiliency checks...' ) ;
181- logConCgp ( `[init] Requires ${ requiredConsecutiveClearChecks } consecutive clear checks.` ) ;
182-
183-
184- const resiliencyCheckInterval = setInterval ( ( ) => {
185- totalIterationsPerformed ++ ;
186-
187- if ( doCustomModificationsExist ( ) ) {
188- consecutiveClearCheckCount = 0 ; // Reset counter if modifications are detected
189- logConCgp ( `[init] Existing modifications detected. Resetting consecutive clear check counter. (Iteration ${ totalIterationsPerformed } /${ maximumTotalIterations } )` ) ;
190- } else {
191- consecutiveClearCheckCount ++ ;
192- logConCgp ( `[init] No modifications detected. Consecutive clear checks: ${ consecutiveClearCheckCount } /${ requiredConsecutiveClearChecks } ` ) ;
193- }
194-
195- // Verify if the required number of consecutive clear checks has been met
196- if ( consecutiveClearCheckCount >= requiredConsecutiveClearChecks ) {
197- logConCgp ( '[init] Required consecutive clear checks achieved. Proceeding with initialization.' ) ;
198- clearInterval ( resiliencyCheckInterval ) ;
199- enforceResiliencyMeasures ( ) ;
200- }
201-
202- // Safety measure to prevent infinite loops
203- if ( totalIterationsPerformed >= maximumTotalIterations ) {
204- logConCgp ( '[init] Maximum iterations reached without achieving consecutive clear checks.' ) ;
205- clearInterval ( resiliencyCheckInterval ) ;
206-
207- // Only proceed if no modifications are present at this point
208- if ( ! doCustomModificationsExist ( ) ) {
209- logConCgp ( '[init] No modifications present after maximum iterations. Proceeding cautiously.' ) ;
210- enforceResiliencyMeasures ( ) ;
211- } else {
212- logConCgp ( '[init] Modifications still present after maximum iterations. Aborting initialization.' ) ;
213- }
214- }
215- } , intervalTimeInMilliseconds ) ;
216- }
217-
218- /**
219- * Enforces resiliency by re-initializing the extension without further resiliency checks.
220- * Inserts custom elements into the webpage to restore functionality.
221- */
222- function enforceResiliencyMeasures ( ) {
223- logConCgp ( '[init] Enforcing resiliency measures. Re-initializing without resiliency checks.' ) ;
224- buttonBoxCheckingAndInjection ( false ) ;
225- }
226-
227118 /**
228119 * Debounces a function to limit how often it can be executed.
229120 * @param {Function } func - The function to debounce.
0 commit comments