This repository has been archived by the owner on Jun 23, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
api.ts
213 lines (193 loc) · 6.13 KB
/
api.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { IpcEvent } from '../models/ipc-event';
import { ipcInvoker } from './ipc-invoker';
import { WINDOW_ID_NONE, WINDOW_ID_CURRENT, TAB_ID_NONE } from '../constants';
import { WebRequestEvent } from '../models/web-request-event';
import { imageData2base64 } from '../utils/images';
declare const chrome: any;
class PolicyConfig {
get() {}
set() {}
clear() {}
}
export const injectAPI = () => {
const manifest = chrome.runtime.getManifest();
const tabs = {
...chrome.tabs,
TAB_ID_NONE,
getCurrent: ipcInvoker('tabs.getCurrent'),
create: ipcInvoker('tabs.create'),
get: ipcInvoker('tabs.get'),
remove: ipcInvoker('tabs.remove'),
getAllInWindow: ipcInvoker('tabs.getAllInWindow'),
getSelected: ipcInvoker('tabs.getSelected'),
insertCSS: ipcInvoker('tabs.insertCSS'),
query: ipcInvoker('tabs.query'),
reload: ipcInvoker('tabs.reload'),
update: ipcInvoker('tabs.update'),
onCreated: new IpcEvent('tabs.onCreated'),
onRemoved: new IpcEvent('tabs.onRemoved'),
onUpdated: new IpcEvent('tabs.onUpdated'),
onActivated: new IpcEvent('tabs.onActivated'),
};
const cookies = {
...chrome.cookies,
get: ipcInvoker('cookies.get'),
getAll: ipcInvoker('cookies.getAll'),
remove: ipcInvoker('cookies.remove'),
set: ipcInvoker('cookies.set'),
onChanged: new IpcEvent('cookies.onChanged'),
};
const windows = {
...chrome.windows,
WINDOW_ID_NONE,
WINDOW_ID_CURRENT,
get: ipcInvoker('windows.get'),
getAll: ipcInvoker('windows.getAll'),
getCurrent: ipcInvoker('windows.getCurrent'),
getLastFocused: ipcInvoker('windows.getLastFocused'),
create: ipcInvoker('windows.create'),
update: ipcInvoker('windows.update'),
remove: ipcInvoker('windows.remove'),
onCreated: new IpcEvent('windows.onCreated'),
onRemoved: new IpcEvent('windows.onRemoved'),
onFocusChanged: new IpcEvent('windows.onFocusChanged'),
};
const extension = {
...chrome.extension,
getViews: (): any[] => [],
isAllowedFileSchemeAccess: (cb: any) => cb && cb(false),
isAllowedIncognitoAccess: (cb: any) => cb && cb(false),
};
const contextMenus = {
...chrome.contextMenus,
onClicked: new IpcEvent('contextMenus.onClicked'),
create: ipcInvoker('contextMenus.create', { noop: true }),
removeAll: ipcInvoker('contextMenus.removeAll', { noop: true }),
remove: ipcInvoker('contextMenus.remove', { noop: true }),
};
const notifications = {
...chrome.notifications,
create() {},
update() {},
clear() {},
getAll() {},
getPermissionLevel() {},
onClosed: new IpcEvent('notifications.onClosed'),
onClicked: new IpcEvent('notifications.onClicked'),
onButtonClicked: new IpcEvent('notifications.onButtonClicked'),
onPermissionLevelChanged: new IpcEvent(
'notifications.onPermissionLevelChanged',
),
onShowSettings: new IpcEvent('notifications.onShowSettings'),
};
const permissions = {
...chrome.permissions,
onAdded: new IpcEvent('permissions.onAdded'),
getAll: () => {},
};
const privacy = {
...chrome.privacy,
network: {
networkPredictionEnabled: new PolicyConfig(),
webRTCIPHandlingPolicy: new PolicyConfig(),
webRTCMultipleRoutesEnabled: new PolicyConfig(),
webRTCNonProxiedUdpEnabled: new PolicyConfig(),
},
websites: {
hyperlinkAuditingEnabled: new PolicyConfig(),
},
};
const browserAction = {
...chrome.browserAction,
setBadgeBackgroundColor: ipcInvoker(
'browserAction.setBadgeBackgroundColor',
{
includeId: true,
},
),
setBadgeText: ipcInvoker('browserAction.setBadgeText', {
includeId: true,
}),
setIcon: ipcInvoker('browserAction.setIcon', {
includeId: true,
serialize: (details: any) => {
if (details.imageData) {
if (details.imageData instanceof ImageData) {
details.imageData = imageData2base64(details.imageData);
} else {
details.imageData = Object.entries(details.imageData).reduce(
(obj: any, pair: any) => {
obj[pair[0]] = imageData2base64(pair[1]);
return obj;
},
{},
);
}
}
return [details];
},
}),
setTitle: ipcInvoker('browserAction.setTitle', {
includeId: true,
}),
setPopup: ipcInvoker('browserAction.setPopup', {
includeId: true,
}),
onClicked: new IpcEvent('browserAction.onClicked'),
};
const webRequest = {
...chrome.webRequest,
ResourceType: {
CSP_REPORT: 'csp_report',
FONT: 'font',
IMAGE: 'image',
MAIN_FRAME: 'main_frame',
MEDIA: 'media',
OBJECT: 'object',
OTHER: 'other',
PING: 'ping',
SCRIPT: 'script',
STYLESHEET: 'stylesheet',
SUB_FRAME: 'sub_frame',
WEBSOCKET: 'websocket',
XMLHTTPREQUEST: 'xmlhttprequest',
},
onBeforeRequest: new WebRequestEvent('onBeforeRequest'),
onBeforeSendHeaders: new WebRequestEvent('onBeforeSendHeaders'),
onHeadersReceived: new WebRequestEvent('onHeadersReceived'),
onSendHeaders: new WebRequestEvent('onSendHeaders'),
onResponseStarted: new WebRequestEvent('onResponseStarted'),
onBeforeRedirect: new WebRequestEvent('onBeforeRedirect'),
onCompleted: new WebRequestEvent('onCompleted'),
onErrorOccurred: new WebRequestEvent('onErrorOccurred'),
onAuthRequired: new WebRequestEvent('onAuthRequired'),
};
const webNavigation = {
...chrome.webNavigation,
onBeforeNavigate: new IpcEvent('webNavigation.onBeforeNavigate'),
onCompleted: new IpcEvent('webNavigation.onCompleted'),
onCreatedNavigationTarget: new IpcEvent(
'webNavigation.onCreatedNavigationTarget',
),
onCommitted: new IpcEvent('webNavigation.onCommitted'),
};
Object.assign(chrome, {
tabs,
cookies,
windows,
extension,
notifications,
permissions,
contextMenus,
webNavigation,
webRequest,
privacy,
});
if (manifest.browser_action) {
chrome.browserAction = browserAction;
}
if (chrome.storage) {
chrome.storage.sync = chrome.storage.local;
}
Object.freeze(chrome);
};