-
Notifications
You must be signed in to change notification settings - Fork 2
/
WKWebViewPreferencesManager.swift
118 lines (101 loc) · 3.94 KB
/
WKWebViewPreferencesManager.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
/**
*
* Where's My Browser
*
* Copyright (C) 2017, 2018 David Turco
*
* This program can be distributed under the terms of the GNU GPL.
* See the file COPYING.
*
* WARNING: This code is VULNERABLE-BY-DESIGN and it is intended as a learning tool
* DO NOT USE THIS CODE IN YOUR PROJECTS!!!
*
*/
import Foundation
import WebKit
class WKWebViewPreferencesManager {
let wkWebView:WKWebView
let wkWebViewPreferences:WKPreferences
let wkWebViewConfiguration:WKWebViewConfiguration
public class WebViewSetting {
let id: String
let name: String
let description: String
var value: Bool = false
init(id:String, name:String, description:String, value:Bool) {
self.id = id
self.name = name
self.description = description
self.value = value
}
}
public private(set) var options = [
WebViewSetting(id: "javaScript",
name: "JavaScript",
description: "Enables support for JavaScript within the WebView",
value: true),
WebViewSetting(id: "javaScriptBridge",
name: "JavaScript Bridge",
description: "Enables the JavaScript to native bridge message handler with name 'javaScriptBridge'",
value: true),
WebViewSetting(id: "undocumentedAllowAccessFromFileURL",
name: "Allow File Access from File",
description: "Allows file:// resources to access other files. This uses the undocumented 'allowFileAccessFromFileURLs' of 'WebViewPreferences'",
value: false)
]
var option_ids : [String] = []
var options_dict: [String:WebViewSetting] = [:]
init(wkWebView: WKWebView) {
self.wkWebView = wkWebView
self.wkWebViewConfiguration = wkWebView.configuration
self.wkWebViewPreferences = wkWebView.configuration.preferences
for option in options {
option_ids.append(option.id)
options_dict[option.id] = option
}
}
func enableOption(_ id: String, enabled: Bool ) {
if option_ids.contains(id) {
switch id {
case "javaScript":
enableJavaScript(enabled)
case "javaScriptBridge":
enableJavaScriptBridge(enabled)
case "undocumentedAllowAccessFromFileURL":
enableUndocumentedAllowAccessFromFileURLs(enabled)
default:break
}
}
}
func isOptionEnabled(_ id: String) -> Bool {
if option_ids.contains(id) {
return options_dict[id]!.value
}
return false
}
func enableJavaScript(_ enabled: Bool) {
options_dict["javaScript"]?.value = enabled
if wkWebViewPreferences.javaScriptEnabled != wkWebViewPreferences.javaScriptEnabled {
wkWebViewPreferences.javaScriptEnabled = enabled
wkWebView.reload()
}
}
func enableJavaScriptBridge(_ enabled: Bool) {
options_dict["javaScriptBridge"]?.value = enabled
let userContentController = wkWebViewConfiguration.userContentController
userContentController.removeScriptMessageHandler(forName: "javaScriptBridge")
if enabled {
let javaScriptBridgeMessageHandler = JavaScriptBridgeMessageHandler()
userContentController.add(javaScriptBridgeMessageHandler, name: "javaScriptBridge")
}
}
func enableUndocumentedAllowAccessFromFileURLs(_ enabled: Bool) {
options_dict["undocumentedAllowAccessFromFileURL"]?.value = enabled
// This is an undocumented workaround to allow file access from a file: origin
if enabled {
wkWebViewPreferences.setValue("Yes", forKey: "allowFileAccessFromFileURLs")
} else {
wkWebViewPreferences.setValue("No", forKey: "allowFileAccessFromFileURLs")
}
}
}