/
CustomSearchHandler.swift
32 lines (26 loc) · 1.21 KB
/
CustomSearchHandler.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
/* 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 Foundation
import WebKit
class CustomSearchHelper: TabHelper {
fileprivate weak var tab: Tab?
required init(tab: Tab) {
self.tab = tab
if let path = Bundle.main.path(forResource: "CustomSearchHelper", ofType: "js") {
if let source = try? NSString(contentsOfFile: path, encoding: String.Encoding.utf8.rawValue) as String {
let userScript = WKUserScript(source: source, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
tab.webView!.configuration.userContentController.addUserScript(userScript)
}
}
}
func scriptMessageHandlerName() -> String? {
return "customSearchHelper"
}
func userContentController(_ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
//We don't listen to messages because the BVC calls the searchHelper script by itself.
}
class func name() -> String {
return "CustomSearchHelper"
}
}