This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
UserScriptManager.swift
347 lines (299 loc) · 13 KB
/
UserScriptManager.swift
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import WebKit
import Shared
import Data
import BraveCore
import Preferences
import BraveWallet
import os.log
private class ScriptLoader: TabContentScriptLoader { }
class UserScriptManager {
static let shared = UserScriptManager()
static let securityToken = ScriptLoader.uniqueID
static let walletSolanaNameSpace = "W\(ScriptLoader.uniqueID)"
private let alwaysEnabledScripts: [ScriptType] = [
.faviconFetcher,
.rewardsReporting,
.playlist,
.resourceDownloader,
.windowRenderHelper,
.readyStateHelper,
.youtubeQuality
]
/// Scripts that are loaded after `staticScripts`
private let dynamicScripts: [ScriptType: WKUserScript] = {
ScriptType.allCases.reduce(into: [:]) { $0[$1] = $1.script }
}()
/// Scripts that are web-packed and should be loaded after `baseScripts` but before `dynamicScripts`
private let staticScripts: [WKUserScript] = {
return [
(WKUserScriptInjectionTime.atDocumentStart, mainFrameOnly: false, sandboxed: false),
(WKUserScriptInjectionTime.atDocumentEnd, mainFrameOnly: false, sandboxed: false),
(WKUserScriptInjectionTime.atDocumentStart, mainFrameOnly: false, sandboxed: true),
(WKUserScriptInjectionTime.atDocumentEnd, mainFrameOnly: false, sandboxed: true),
(WKUserScriptInjectionTime.atDocumentStart, mainFrameOnly: true, sandboxed: false),
(WKUserScriptInjectionTime.atDocumentEnd, mainFrameOnly: true, sandboxed: false),
(WKUserScriptInjectionTime.atDocumentStart, mainFrameOnly: true, sandboxed: true),
(WKUserScriptInjectionTime.atDocumentEnd, mainFrameOnly: true, sandboxed: true),
].compactMap { (injectionTime, mainFrameOnly, sandboxed) in
let name = (mainFrameOnly ? "MainFrame" : "AllFrames") + "AtDocument" + (injectionTime == .atDocumentStart ? "Start" : "End") + (sandboxed ? "Sandboxed" : "")
if let source = ScriptLoader.loadUserScript(named: name) {
let wrappedSource = "(function() { const SECURITY_TOKEN = '\(UserScriptManager.securityToken)'; \(source) })()"
return WKUserScript(
source: wrappedSource,
injectionTime: injectionTime,
forMainFrameOnly: mainFrameOnly,
in: sandboxed ? .defaultClient : .page)
}
return nil
}
}()
/// Scripts injected before all other scripts.
private let baseScripts: [WKUserScript] = {
[
(WKUserScriptInjectionTime.atDocumentStart, mainFrameOnly: false, sandboxed: false),
(WKUserScriptInjectionTime.atDocumentEnd, mainFrameOnly: false, sandboxed: false),
(WKUserScriptInjectionTime.atDocumentStart, mainFrameOnly: false, sandboxed: true),
(WKUserScriptInjectionTime.atDocumentEnd, mainFrameOnly: false, sandboxed: true),
].compactMap { (injectionTime, mainFrameOnly, sandboxed) in
if let source = ScriptLoader.loadUserScript(named: "__firefox__") {
return WKUserScript(
source: source,
injectionTime: injectionTime,
forMainFrameOnly: mainFrameOnly,
in: sandboxed ? .defaultClient : .page)
}
return nil
}
}()
private var walletEthProviderScript: WKUserScript?
private var walletSolProviderScript: WKUserScript?
private var walletSolanaWeb3Script: WKUserScript?
private var walletSolanaWalletStandardScript: WKUserScript?
enum ScriptType: String, CaseIterable {
case faviconFetcher
case cookieBlocking
case rewardsReporting
case mediaBackgroundPlay
case playlistMediaSource
case playlist
case nightMode
case deAmp
case requestBlocking
case trackerProtectionStats
case resourceDownloader
case windowRenderHelper
case readyStateHelper
case ethereumProvider
case solanaProvider
case youtubeQuality
fileprivate var script: WKUserScript? {
switch self {
// Conditionally enabled scripts
case .cookieBlocking: return loadScript(named: "CookieControlScript")
case .mediaBackgroundPlay: return loadScript(named: "MediaBackgroundingScript")
case .playlistMediaSource: return loadScript(named: "PlaylistSwizzlerScript")
case .nightMode: return NightModeScriptHandler.userScript
case .deAmp: return DeAmpScriptHandler.userScript
case .requestBlocking: return RequestBlockingContentScriptHandler.userScript
case .trackerProtectionStats: return ContentBlockerHelper.userScript
case .ethereumProvider: return EthereumProviderScriptHandler.userScript
case .solanaProvider: return SolanaProviderScriptHandler.userScript
// Always enabled scripts
case .faviconFetcher: return FaviconScriptHandler.userScript
case .rewardsReporting: return RewardsReportingScriptHandler.userScript
case .playlist: return PlaylistScriptHandler.userScript
case .resourceDownloader: return ResourceDownloadScriptHandler.userScript
case .windowRenderHelper: return WindowRenderScriptHandler.userScript
case .readyStateHelper: return ReadyStateScriptHandler.userScript
case .youtubeQuality: return YoutubeQualityScriptHandler.userScript
}
}
private func loadScript(named: String) -> WKUserScript? {
guard var script = ScriptLoader.loadUserScript(named: named) else {
return nil
}
script = ScriptLoader.secureScript(handlerNamesMap: [:], securityToken: "", script: script)
return WKUserScript(source: script, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
}
}
func fetchWalletScripts(from braveWalletAPI: BraveWalletAPI) {
if let ethJS = braveWalletAPI.providerScripts(for: .eth)[.ethereum] {
let providerJS = """
window.__firefox__.execute(function($, $Object) {
if (window.isSecureContext) {
\(ethJS)
}
});
"""
walletEthProviderScript = WKUserScript(
source: providerJS,
injectionTime: .atDocumentStart,
forMainFrameOnly: true,
in: EthereumProviderScriptHandler.scriptSandbox
)
}
if let solanaWeb3Script = braveWalletAPI.providerScripts(for: .sol)[.solanaWeb3] {
let script = """
// Define a global variable with a random name
// Local variables are NOT enumerable!
let \(UserScriptManager.walletSolanaNameSpace);
window.__firefox__.execute(function($, $Object, $Function, $Array) {
// Inject Solana as a Local Variable.
\(solanaWeb3Script)
\(UserScriptManager.walletSolanaNameSpace) = $({
solanaWeb3: $(solanaWeb3)
});
// Failed to load SolanaWeb3
if (typeof \(UserScriptManager.walletSolanaNameSpace) === 'undefined') {
return;
}
const freezeExceptions = $Array.of("BN");
for (const value of $Object.values(\(UserScriptManager.walletSolanaNameSpace).solanaWeb3)) {
if (!value) {
continue;
}
$.extensiveFreeze(value, freezeExceptions);
}
$.deepFreeze(\(UserScriptManager.walletSolanaNameSpace).solanaWeb3);
$.deepFreeze(\(UserScriptManager.walletSolanaNameSpace));
});
"""
self.walletSolanaWeb3Script = WKUserScript(
source: script,
injectionTime: .atDocumentStart,
forMainFrameOnly: true,
in: SolanaProviderScriptHandler.scriptSandbox
)
}
if let walletSolProviderScript = braveWalletAPI.providerScripts(for: .sol)[.solana] {
let script = """
window.__firefox__.execute(function($, $Object) {
\(walletSolProviderScript)
});
"""
self.walletSolProviderScript = WKUserScript(
source: script,
injectionTime: .atDocumentStart,
forMainFrameOnly: true,
in: SolanaProviderScriptHandler.scriptSandbox
)
}
if let walletStandardScript = braveWalletAPI.providerScripts(for: .sol)[.walletStandard] {
let script = """
window.__firefox__.execute(function($, $Object) {
\(walletStandardScript)
window.addEventListener('wallet-standard:app-ready', (e) => {
walletStandardBrave.initialize(window.braveSolana);
})
});
"""
self.walletSolanaWalletStandardScript = WKUserScript(
source: script,
injectionTime: .atDocumentStart,
forMainFrameOnly: true,
in: SolanaProviderScriptHandler.scriptSandbox
)
}
}
public func loadScripts(into webView: WKWebView, scripts: Set<ScriptType>) {
var scripts = scripts
webView.configuration.userContentController.do { scriptController in
scriptController.removeAllUserScripts()
// Inject all base scripts
self.baseScripts.forEach {
scriptController.addUserScript($0)
}
// Inject specifically trackerProtectionStats BEFORE request blocking
// this is because it needs to hook requests before requestBlocking
if scripts.contains(.trackerProtectionStats), let script = self.dynamicScripts[.trackerProtectionStats] {
scripts.remove(.trackerProtectionStats)
scriptController.addUserScript(script)
}
// Inject specifically RequestBlocking BEFORE other scripts
// this is because it needs to hook requests before RewardsReporting
if scripts.contains(.requestBlocking), let script = self.dynamicScripts[.requestBlocking] {
scripts.remove(.requestBlocking)
scriptController.addUserScript(script)
}
// Inject all static scripts
self.staticScripts.forEach {
scriptController.addUserScript($0)
}
// Inject all scripts that are dynamic, but always enabled
self.dynamicScripts.filter({ self.alwaysEnabledScripts.contains($0.key) }).forEach {
scriptController.addUserScript($0.value)
}
// Inject all optional scripts
self.dynamicScripts.filter({ scripts.contains($0.key) }).forEach {
scriptController.addUserScript($0.value)
}
}
}
// TODO: Get rid of this OR refactor wallet and domain scripts
func loadCustomScripts(
into tab: Tab,
userScripts: Set<ScriptType>,
customScripts: Set<UserScriptType>
) {
guard let webView = tab.webView else {
Logger.module.info("Injecting Scripts into a Tab that has no WebView")
return
}
let logComponents = [
userScripts.sorted(by: { $0.rawValue < $1.rawValue}).map { scriptType in
" \(scriptType.rawValue)"
}.joined(separator: "\n"),
customScripts.sorted(by: { $0.order < $1.order}).map { scriptType in
" #\(scriptType.order) \(scriptType.debugDescription)"
}.joined(separator: "\n")
]
ContentBlockerManager.log.debug("Loaded \(userScripts.count + customScripts.count) script(s): \n\(logComponents.joined(separator: "\n"))")
loadScripts(into: webView, scripts: userScripts)
webView.configuration.userContentController.do { scriptController in
// TODO: Somehow refactor wallet and get rid of this
// Inject WALLET specific scripts
if !tab.isPrivate,
Preferences.Wallet.WalletType(rawValue: Preferences.Wallet.defaultEthWallet.value) == .brave,
let script = self.dynamicScripts[.ethereumProvider] {
// Inject ethereum provider
scriptController.addUserScript(script)
if let walletEthProviderScript = walletEthProviderScript {
scriptController.addUserScript(walletEthProviderScript)
}
}
// Inject SolanaWeb3Script.js
if !tab.isPrivate,
Preferences.Wallet.WalletType(rawValue: Preferences.Wallet.defaultSolWallet.value) == .brave,
let solanaWeb3Script = self.walletSolanaWeb3Script {
scriptController.addUserScript(solanaWeb3Script)
}
if !tab.isPrivate,
Preferences.Wallet.WalletType(rawValue: Preferences.Wallet.defaultSolWallet.value) == .brave,
let script = self.dynamicScripts[.solanaProvider] {
// Inject solana provider
scriptController.addUserScript(script)
if let walletSolProviderScript = walletSolProviderScript {
scriptController.addUserScript(walletSolProviderScript)
}
}
if !tab.isPrivate,
let walletStandardScript = self.walletSolanaWalletStandardScript {
scriptController.addUserScript(walletStandardScript)
}
// TODO: Refactor this and get rid of the `UserScriptType`
// Inject Custom scripts
for userScriptType in customScripts.sorted(by: { $0.order < $1.order }) {
do {
let script = try ScriptFactory.shared.makeScript(for: userScriptType)
scriptController.addUserScript(script)
} catch {
assertionFailure("Should never happen. The scripts are packed in the project and loading/modifying should always be possible.")
Logger.module.error("\(error.localizedDescription)")
}
}
}
}
}