-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
full.js
246 lines (215 loc) · 7.56 KB
/
full.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// WARNING: only import js inside scripts/ folder
export default {
icon: "",
name: {
en: "",
vi: "",
},
description: {
en: "",
vi: "",
// optional
img: "",
video: "",
},
// buttons that show beside favorite/view-source button. Show when hover script
buttons: [
{
icon: '<i class="fa-regular fa-circle-question"></i>',
name: {
vi: "Đây là gì?",
en: "What is this?",
},
onClick: () => {},
},
],
badges: [
// BADGES.hot, BADGES.beta, BADGES.new, BADGES.beta
],
// easier way to add infor button beside favorite/view-source button
// will open link in new tab when user click
infoLink: "",
// show change-logs at the bottom of script's description
changeLogs: {
date: "description",
},
// determize which website this script supported
blackList: [],
whiteList: [],
// run in extension popup context
// - CANNOT autorun - only executed when user click
// - can use chrome extension APIs
// - can use imported functions (utils, shared)
// + can access/modify DOM/variables in webpage/content-script context (via utils.runScriptInCurrentTab)
// - can use UfsGlobal
// + can run scripts in background-context (limited - via UseGlobal.Extension)
popupScript: {
// run when enable/disable autorun script
onEnable: () => {}, // tip: 'return false' to cancel enable action
onDisable: () => {}, // tip: 'return false' to cancel disable action
onClick: () => {},
},
// run in content-script context (ISOLATED/SANDBOX world)
// - can autorun
// - can use chrome extension APIs (limited)
// - CANNOT use imported functions (utils, shared)
// - can access/modify DOM
// - CANNOT access/modify variables in page context (different world)
// - can use UfsGlobal
// + can run scripts in background-context (limited - via UseGlobal.Extension)
contentScript: {
// run (if enable autorun) in content-script context
onDocumentStart: (details) => {},
onDocumentIdle: (details) => {},
onDocumentEnd: (details) => {},
onClick: () => {},
// advanced
runtime: {
onInstalled: (reason) => {},
onStartup: (nil) => {},
onMessage: (request, sender, sendResponse) => {},
},
webNavigation: {
onCreatedNavigationTarget: (details) => {},
onHistoryStateUpdated: (details) => {},
onBeforeNavigate: (details) => {},
},
webRequest: {
onBeforeRedirect: (details) => {},
onBeforeRequest: (details) => {},
onBeforeSendHeaders: (details) => {},
onCompleted: (details) => {},
onErrorOccurred: (details) => {},
onHeadersReceived: (details) => {},
onResponseStarted: (details) => {},
onSendHeaders: (details) => {},
},
},
// run in webpage context (MAIN world)
// - can autorun
// - CANNOT use chrome extension APIs
// - CANNOT use imported functions (utils, shared)
// - can access/modify DOM/variables -> can override default behaviors
// - can use UfsGlobal
// + can run scripts in background-context (limited - via UseGlobal.Extension)
pageScript: {
// run (if enable autorun) in webpage context
onDocumentStart: (details) => {},
onDocumentIdle: (details) => {},
onDocumentEnd: (details) => {},
onClick: () => {},
// advanced
runtime: {
onInstalled: (reason) => {},
onStartup: (nil) => {},
onMessage: (request, sender, sendResponse) => {},
},
webNavigation: {
onCreatedNavigationTarget: (details) => {},
onHistoryStateUpdated: (details) => {},
onBeforeNavigate: (details) => {},
},
webRequest: {
onBeforeRedirect: (details) => {},
onBeforeRequest: (details) => {},
onBeforeSendHeaders: (details) => {},
onCompleted: (details) => {},
onErrorOccurred: (details) => {},
onHeadersReceived: (details) => {},
onResponseStarted: (details) => {},
onSendHeaders: (details) => {},
},
},
// run in background (service worker) context
// - can autorun
// - can use chrome extension APIs
// - CANNOT use dynamic imports, but can use GLOBAL variables in background_script.js
// - can use UfsGlobal
// - context = GLOBAL variable in background_script.js
backgroundScript: {
onDocumentStart: (details, context) => {},
onDocumentIdle: (details, context) => {},
onDocumentEnd: (details, context) => {},
// advanced
runtime: {
onInstalled: (reason, context) => {},
onStartup: (nil, context) => {},
onMessage: ({ request, sender, sendResponse }, context) => {},
onMessageExternal: ({ request, sender, sendResponse }, context) => {},
},
contextMenus: {
onClicked: ({ info, tab }, context) => {},
},
webNavigation: {
onCreatedNavigationTarget: (details, context) => {},
onHistoryStateUpdated: (details, context) => {},
onBeforeNavigate: (details, context) => {},
},
webRequest: {
// Can only modify requestHeaders (onBeforeSendHeaders) AND responseHeaders (onHeadersReceived)
// Don't use async await for these two events if you want to return modified values
onBeforeRedirect: (details, context) => {},
onBeforeRequest: (details, context) => {},
onBeforeSendHeaders: (details, context) => {},
onCompleted: (details, context) => {},
onErrorOccurred: (details, context) => {},
onHeadersReceived: (details, context) => {},
onResponseStarted: (details, context) => {},
onSendHeaders: (details, context) => {},
},
tabs: {
onActivated: (details, context) => {},
onAttached: (details, context) => {},
onCreated: (details, context) => {},
onDetached: (details, context) => {},
onHighlighted: (details, context) => {},
onMoved: (details, context) => {},
onRemoved: (details, context) => {},
onReplaced: (details, context) => {},
onUpdated: (details, context) => {},
onZoomChange: (details, context) => {},
},
storage: {
onChanged: (details, context) => {},
},
windows: {
onCreated: (details, context) => {},
onFocusChanged: (details, context) => {},
onBoundsChanged: (details, context) => {},
onRemoved: (details, context) => {},
},
},
};
/* Run scripts in all frames (main-frames/sub-frame/iframes)
just add _ at the end of function name
e.g: onDocumentStart_() onMessage_()
these functions are not support allFrames:
onInstalled, onStartup
onMessage, storage.onChanged, tabs.*: default to listen all messages (included allFrames + popup + background)
*/
/* Comunicate between contexts
There are 4 contexts:
+ popupScript: run in extension popup
+ contentScript: run in webpage ISOLATED world
+ pageScript: run in webpage MAIN world
+ backgroundScript: run in service worker
Each context has its own world that can not be shared (variables) with other contexts.
But there are few ways to communicate between these contexts:
-> : one way communicate (may support callback)
<-> : two way communicate
popupScript -> pageScript/contentScript:
+ chrome.scripting.executeScript (utils.runScriptInCurrentTab, ...)
popupScript <-> backgroundScript:
+ chrome.runtime.sendMessage
contentScript <-> backgroundScript:
+ chrome.runtime.sendMessage
pageScript <-> contentScript:
+ window.postMessage
pageScript -> backgroundScript:
+ UfsGlobal.Extension.runInBackground
backgroundScript -> pageScript/contentScript:
+ chrome.scripting.executeScript
backgroundScript <-> backgroundScript (different scripts)
+ chrome.runtime.sendMessage
+ context cache (setCache, getCache)
*/