-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInjectScriptV3.ts
More file actions
67 lines (54 loc) · 2.08 KB
/
InjectScriptV3.ts
File metadata and controls
67 lines (54 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {browser, executeScript} from "@addon-core/browser";
import AbstractInjectScript from "./AbstractInjectScript";
type Awaited<T> = chrome.scripting.Awaited<T>;
type InjectionTarget = chrome.scripting.InjectionTarget;
type InjectionResult<T> = chrome.scripting.InjectionResult<T>;
export default class extends AbstractInjectScript {
public async run<A extends any[], R>(func: (...args: A) => R, args?: A): Promise<InjectionResult<Awaited<R>>[]> {
return executeScript({
target: this.target(),
world: this._options.world,
injectImmediately: this.injectImmediately,
func,
args,
});
}
public async file(fileList: string | string[]): Promise<void> {
await executeScript({
target: this.target(),
world: this._options.world,
injectImmediately: this.injectImmediately,
files: typeof fileList === "string" ? [fileList] : fileList,
});
}
protected target(): InjectionTarget {
const target = {tabId: this._options.tabId};
if (this.frameIds && this.frameIds.length > 0) {
return {...target, frameIds: this.frameIds};
}
if (this.allFrames === true) {
return {...target, allFrames: true};
}
// Firefox does not support `documentIds` in the target
// getBrowserInfo is only available in firefox
let isFirefox = false;
try {
// @ts-expect-error
isFirefox = !!browser().runtime.getBrowserInfo;
} catch (_e) {}
if (!isFirefox) {
const documentIds = this.documentIds;
if (documentIds && documentIds.length > 0) {
return {...target, documentIds};
}
}
return target;
}
protected get documentIds(): string[] | undefined {
const {documentId} = this._options;
return typeof documentId === "string" ? [documentId] : documentId;
}
protected get injectImmediately(): boolean {
return this._options.runAt === "document_start";
}
}