Adrià Vilanova Martínez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 1 | export 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 | |
| 12 | export enum ScriptEnvironment { |
| 13 | ContentScript, |
| 14 | InjectedScript, |
| 15 | } |
| 16 | |
| 17 | export enum ScriptPage { |
| 18 | CommunityConsole, |
| 19 | } |
| 20 | |
| 21 | export const ScriptRunPhaseToRunTime: Record< |
| 22 | ScriptRunPhase, |
| 23 | chrome.userScripts.RunAt |
| 24 | > = { |
| 25 | [ScriptRunPhase.Start]: 'document_start', |
| 26 | [ScriptRunPhase.Main]: 'document_idle', |
| 27 | }; |
| 28 | |
| 29 | export const ScriptEnvironmentToExecutionWorld: Record< |
| 30 | ScriptEnvironment, |
| 31 | chrome.scripting.ExecutionWorld |
| 32 | > = { |
| 33 | [ScriptEnvironment.ContentScript]: 'ISOLATED', |
| 34 | [ScriptEnvironment.InjectedScript]: 'MAIN', |
| 35 | }; |
| 36 | |
| 37 | export type ConcreteScript = { new (): Script }; |
| 38 | |
| 39 | export default abstract class Script { |
| 40 | /** |
| 41 | * Priority with which the script is executed. Scripts with a lower value are |
| 42 | * executed first. |
| 43 | */ |
Adrià Vilanova Martínez | e7f9be8 | 2024-05-31 22:37:04 +0200 | [diff] [blame] | 44 | readonly priority: number = 2 ** 31; |
Adrià Vilanova Martínez | 18d03c4 | 2024-04-21 16:43:01 +0200 | [diff] [blame] | 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 | } |