blob: 991afa501f6fcf307dacf8b02e5ea0e9999a0098 [file] [log] [blame]
Adrià Vilanova Martínezdfe71fc2024-05-25 22:13:32 +02001import StylesheetManager from '../../../StylesheetManager';
2import { StylesheetAttributes } from '../../../contentScriptsUtils';
3import OptionsProvider from '../../../options/OptionsProvider';
4import DependenciesProviderSingleton, {
5 OptionsProviderDependency,
6} from '../../dependenciesProvider/DependenciesProvider';
7import Script, { ScriptEnvironment, ScriptRunPhase } from '../Script';
8
9/**
10 * Script which injects a stylesheet depending on a set condition. It
11 * dynamically reevaluates the condition when the options configuration changes.
12 */
13export default abstract class StylesheetScript extends Script {
14 readonly environment = ScriptEnvironment.ContentScript;
15 readonly runPhase = ScriptRunPhase.Start;
16
17 /**
18 * Relative path to the stylesheet from the extension root.
19 */
20 abstract readonly stylesheet: string;
21 /**
22 * Attributes to include in the injected <link> element.
23 */
24 readonly attributes: StylesheetAttributes = {};
25
26 protected optionsProvider: OptionsProvider;
27 private stylesheetManager: StylesheetManager;
28
29 constructor() {
30 super();
31 const dependenciesProvider = DependenciesProviderSingleton.getInstance();
32 this.optionsProvider = dependenciesProvider.getDependency(
33 OptionsProviderDependency,
34 );
35 }
36
37 /**
38 * Condition which decides whether the stylesheet should be injected or not.
39 *
40 * @returns {boolean} Whether the stylesheet should be injected.
41 */
42 abstract shouldBeInjected(): Promise<boolean>;
43
44 execute() {
45 this.stylesheetManager = new StylesheetManager(
46 this.stylesheet,
47 this.attributes,
48 );
49 this.optionsProvider.addListener(this.evaluateInjection.bind(this));
50 this.evaluateInjection();
51 }
52
53 async evaluateInjection() {
54 const shouldBeInjected = await this.shouldBeInjected();
55 if (!this.stylesheetManager.isInjected() && shouldBeInjected) {
56 this.stylesheetManager.inject();
57 }
58 if (this.stylesheetManager.isInjected() && !shouldBeInjected) {
59 this.stylesheetManager.remove();
60 }
61 }
62}