blob: 5cca2c4f6d8cb943f5082340421d0eb80402c4c9 [file] [log] [blame]
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +02001export enum ScriptRunPhase {
2 /**
3 * Executed before any Javascript is executed.
4 */
5 Start,
6 /**
7 * Executed after the document is ready.
8 */
9 Main,
10}
11
12export enum ScriptEnvironment {
13 ContentScript,
14 InjectedScript,
15}
16
17export enum ScriptPage {
18 CommunityConsole,
19}
20
21export const ScriptRunPhaseToRunTime: Record<
22 ScriptRunPhase,
23 chrome.userScripts.RunAt
24> = {
25 [ScriptRunPhase.Start]: 'document_start',
26 [ScriptRunPhase.Main]: 'document_idle',
27};
28
29export const ScriptEnvironmentToExecutionWorld: Record<
30 ScriptEnvironment,
31 chrome.scripting.ExecutionWorld
32> = {
33 [ScriptEnvironment.ContentScript]: 'ISOLATED',
34 [ScriptEnvironment.InjectedScript]: 'MAIN',
35};
36
37export type ConcreteScript = { new (): Script };
38
39export default abstract class Script {
40 /**
41 * Priority with which the script is executed. Scripts with a lower value are
42 * executed first.
43 */
44 readonly priority: Number = 2 ** 31;
45
46 /**
47 * Page where the script should be executed.
48 */
49 abstract readonly page: ScriptPage;
50
51 /**
52 * Environment where the script should be executed.
53 */
54 abstract readonly environment: ScriptEnvironment;
55
56 /**
57 * If {@link environment} is {@link ScriptEnvironment.ContentScript}, phase of
58 * the page loading when the script should be executed.
59 */
60 abstract readonly runPhase?: ScriptRunPhase;
61
62 /**
63 * Method which contains the logic of the script.
64 */
65 abstract execute(): void;
66}