Skip to content

Commit bf48c40

Browse files
committed
lazier resiliency
1 parent 563d69a commit bf48c40

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

buttons-injection.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,52 +60,69 @@ function buttonBoxCheckingAndInjection(enableResiliency = true, activeWebsite) {
6060
}
6161

6262
/**
63-
* Initiates enhanced resiliency checks to ensure the extension remains functional on the webpage.
63+
* The resiliency mechanism continuously monitors the DOM to verify that custom modifications remain intact.
64+
* It performs periodic checks and counts consecutive iterations where the modifications are absent.
65+
* Once a predefined threshold is reached, or after a maximum number of iterations, it triggers a reinjection of the elements.
66+
* This approach ensures the extension maintains functionality even when the webpage dynamically resets or modifies its content.
6467
*/
6568
function commenceEnhancedResiliencyChecks() {
6669
let consecutiveClearCheckCount = 0;
67-
const requiredConsecutiveClearChecks = 2; // missing for this many times = reinsert
68-
const maximumTotalIterations = 16;
70+
const requiredConsecutiveClearChecks = 2; // Missing for this many consecutive checks triggers reinjection
71+
const maximumTotalIterations = 160;
6972
let totalIterationsPerformed = 0;
70-
const intervalTimeInMilliseconds = 50;
7173

72-
logConCgp('[button-injection] Beginning enhanced resiliency checks...');
74+
logConCgp('[button-injection] Beginning enhanced resiliency checks with dynamic interval...');
7375
logConCgp(`[button-injection] Requires ${requiredConsecutiveClearChecks} consecutive clear checks.`);
7476

75-
const resiliencyCheckInterval = setInterval(() => {
77+
// The adaptive check function uses a dynamic delay based on current state.
78+
function adaptiveCheck() {
7679
totalIterationsPerformed++;
77-
78-
if (doCustomModificationsExist()) {
79-
consecutiveClearCheckCount = 0; // Reset counter if modifications are detected
80-
logConCgp(`[button-injection] Existing modifications detected. Resetting consecutive clear check counter. (Iteration ${totalIterationsPerformed}/${maximumTotalIterations})`);
80+
const modificationsExist = doCustomModificationsExist();
81+
let delay;
82+
83+
if (modificationsExist) {
84+
// When modifications are present, reset the missing counter.
85+
consecutiveClearCheckCount = 0;
86+
// Log only every 10 iterations to avoid excessive logs.
87+
if (totalIterationsPerformed % 10 === 0) {
88+
logConCgp(`[button-injection] Modifications detected. Total iterations: ${totalIterationsPerformed}/${maximumTotalIterations}.`);
89+
}
90+
// Slow down checks when everything is okay.
91+
delay = 500;
8192
} else {
8293
consecutiveClearCheckCount++;
83-
logConCgp(`[button-injection] No modifications detected. Consecutive clear checks: ${consecutiveClearCheckCount}/${requiredConsecutiveClearChecks}`);
94+
logConCgp(`[button-injection] No modifications detected. Consecutive missing: ${consecutiveClearCheckCount}/${requiredConsecutiveClearChecks}`);
95+
// Rapid checks when modifications are missing.
96+
delay = 50;
8497
}
8598

86-
// Verify if the required number of consecutive clear checks has been met
99+
// If we have reached the required consecutive clear checks, reinject.
87100
if (consecutiveClearCheckCount >= requiredConsecutiveClearChecks) {
88101
logConCgp('[button-injection] Required consecutive clear checks achieved. Proceeding with initialization.');
89-
clearInterval(resiliencyCheckInterval);
90102
enforceResiliencyMeasures();
103+
return;
91104
}
92105

93-
// Safety measure to prevent infinite loops
106+
// Safety: if maximum iterations have been reached, decide based on current state.
94107
if (totalIterationsPerformed >= maximumTotalIterations) {
95-
logConCgp('[button-injection] Maximum iterations reached without achieving consecutive clear checks.');
96-
clearInterval(resiliencyCheckInterval);
97-
98-
// Only proceed if no modifications are present at this point
108+
logConCgp('[button-injection] Maximum iterations reached.');
99109
if (!doCustomModificationsExist()) {
100110
logConCgp('[button-injection] No modifications present after maximum iterations. Proceeding cautiously.');
101111
enforceResiliencyMeasures();
102112
} else {
103113
logConCgp('[button-injection] Modifications still present after maximum iterations. Aborting initialization.');
104114
}
115+
return;
105116
}
106-
}, intervalTimeInMilliseconds);
117+
118+
// Schedule the next check with the dynamic delay.
119+
setTimeout(adaptiveCheck, delay);
120+
}
121+
122+
adaptiveCheck();
107123
}
108124

125+
109126
/**
110127
* Enforces resiliency by re-initializing the extension without further resiliency checks.
111128
* Inserts custom elements into the webpage to restore functionality.

0 commit comments

Comments
 (0)