-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbase.ts
More file actions
149 lines (128 loc) · 4.68 KB
/
base.ts
File metadata and controls
149 lines (128 loc) · 4.68 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export type WebAuthenticationChallengeDetails =
| chrome.webRequest.OnAuthRequiredDetails
| browser.webRequest._OnAuthRequiredDetails;
export type BlockingResponse = chrome.webRequest.BlockingResponse;
export type WebRequestResponseStartedDetails =
| chrome.webRequest.OnResponseStartedDetails
| browser.webRequest._OnResponseStartedDetails;
export type WebRequestCompletedDetails =
| chrome.webRequest.OnCompletedDetails
| browser.webRequest._OnCompletedDetails;
export type WebRequestErrorOccurredDetails =
| chrome.webRequest.OnErrorOccurredDetails
| browser.webRequest._OnErrorOccurredDetails;
export type MessageSender =
| chrome.runtime.MessageSender
| browser.runtime.MessageSender;
export type Tab = chrome.tabs.Tab | browser.tabs.Tab;
export type ProxyConfig = chrome.proxy.ProxyConfig;
export type ProxyErrorDetails = chrome.proxy.ErrorDetails | Error;
export type ProxySettingResultDetails =
| chrome.types.ChromeSettingGetResult<ProxyConfig>
| {
/**
* One of
* • not_controllable: cannot be controlled by any extension
* • controlled_by_other_extensions: controlled by extensions with higher precedence
* • controllable_by_this_extension: can be controlled by this extension
* • controlled_by_this_extension: controlled by this extension
*/
levelOfControl:
| "not_controllable"
| "controlled_by_other_extensions"
| "controllable_by_this_extension"
| "controlled_by_this_extension";
/** The value of the setting. */
value: ProxyConfig;
/**
* Optional.
* Whether the effective value is specific to the incognito session.
* This property will only be present if the incognito property in the details parameter of get() was true.
*/
incognitoSpecific?: boolean | undefined;
};
export type SimpleProxyServer = {
host: string;
port?: number;
scheme?: "direct" | "http" | "https" | "socks4" | "socks5";
};
export type PacScript = chrome.proxy.PacScript;
export type ProxyRules = chrome.proxy.ProxyRules;
export enum BrowserFlavor {
Unknown = "unknown",
Web = "web", // now only for local dev purpose
Chrome = "chrome",
}
export abstract class BaseAdapter {
get flavor(): BrowserFlavor {
return BrowserFlavor.Unknown;
}
// local storage
abstract set<T>(key: string, val: T): Promise<void>;
abstract get<T>(key: string): Promise<T | undefined>;
async getWithDefault<T>(key: string, defaultVal: T): Promise<T> {
const ret = await this.get<T>(key);
if (ret === undefined) {
return defaultVal;
}
return ret;
}
// Fires when local storage changes in any extension context, including
// this one — the browser dispatches the event everywhere.
abstract onLocalStorageChanged(
callback: (changes: Record<string, { newValue?: unknown }>) => void
): void;
// proxy
abstract setProxy(cfg: ProxyConfig): Promise<void>;
abstract clearProxy(): Promise<void>;
abstract onProxyError(callback: (error: ProxyErrorDetails) => void): void;
abstract onProxyChanged(
callback: (setting: ProxySettingResultDetails) => void
): void;
abstract getProxySettings(): Promise<ProxySettingResultDetails>;
// indicator
abstract setBadge(text: string, color: string, tabID?: number): Promise<void>;
// webRequest
abstract onWebRequestAuthRequired(
callback: (
details: WebAuthenticationChallengeDetails,
asyncCallback?: (response: BlockingResponse) => void
) => BlockingResponse | undefined
): void;
abstract onWebRequestResponseStarted(
callback: (details: WebRequestResponseStartedDetails) => void
): void;
abstract onWebRequestCompleted(
callback: (details: WebRequestCompletedDetails) => void
): void;
abstract onWebRequestErrorOccurred(
callback: (details: WebRequestErrorOccurredDetails) => void
): void;
// tabs
abstract getActiveTab(): Promise<Tab | undefined>;
abstract onTabRemoved(callback: (tabID: number) => void): void;
// messages
abstract onMessage(
callback: (
message: any,
sender: MessageSender,
sendResponse: (response: any) => void
) => void
): void;
abstract sendMessage(message: any): Promise<any>;
// alarms
abstract createPeriodicAlarm(
name: string,
periodInMinutes: number
): Promise<void>;
abstract clearAlarm(name: string): Promise<void>;
abstract getAllAlarmNames(): Promise<string[]>;
abstract onAlarm(callback: (name: string) => void): void;
// i18n
abstract currentLocale(): string;
abstract getMessage(key: string, substitutions?: string | string[]): string;
// compatible issues, return an error message in HTML format
async error(): Promise<string | undefined> {
return;
}
}