17
17
*/
18
18
19
19
import { getHostname } from 'tldts' ;
20
- import { type Tabs } from 'webextension-polyfill' ;
20
+ import browser , { type Tabs } from 'webextension-polyfill' ;
21
21
22
22
import { isHttpRequest } from 'tswebextension' ;
23
23
@@ -39,6 +39,11 @@ import { createPromiseWithTimeout } from './utils/timeouts';
39
39
* Helper class for injecting content script into tabs, opened before extension initialization.
40
40
*/
41
41
export class ContentScriptInjector {
42
+ /**
43
+ * Key used to store the injected flag in session storage.
44
+ */
45
+ private static INJECTED_KEY = 'content_script_injected' ;
46
+
42
47
private static INJECTION_LIMIT_MS = 1000 ;
43
48
44
49
/**
@@ -202,4 +207,36 @@ export class ContentScriptInjector {
202
207
203
208
return true ;
204
209
}
210
+
211
+ /**
212
+ * Sets the injected flag in session storage.
213
+ * This method updates the session storage to indicate that content scripts have been injected.
214
+ */
215
+ public static async setInjected ( ) : Promise < void > {
216
+ try {
217
+ await browser . storage . session . set ( { [ ContentScriptInjector . INJECTED_KEY ] : true } ) ;
218
+ } catch ( e ) {
219
+ logger . error ( 'Cannot set injected flag in session storage' , e ) ;
220
+ }
221
+ }
222
+
223
+ /**
224
+ * Checks if content scripts have been injected.
225
+ * Uses session storage since it is faster than sending a message to the content script.
226
+ * As of November 25, 2025, Firefox v132.0.2 takes 1 second to send a message,
227
+ * whereas reading from the session storage takes only 1 ms.
228
+ *
229
+ * @returns True if content scripts were injected; otherwise, false.
230
+ */
231
+ public static async isInjected ( ) : Promise < boolean > {
232
+ let isInjected = false ;
233
+ try {
234
+ const result = await browser . storage . session . get ( ContentScriptInjector . INJECTED_KEY ) ;
235
+ isInjected = result [ ContentScriptInjector . INJECTED_KEY ] === true ;
236
+ } catch ( e ) {
237
+ logger . error ( 'Cannot get injected flag from session storage' , e ) ;
238
+ }
239
+
240
+ return isInjected ;
241
+ }
205
242
}
0 commit comments