Fix build errors in PreloadedWebView.

This commit is contained in:
Brent Simmons 2024-02-22 22:01:41 -08:00
parent 3de86ba877
commit bb04233a33

View File

@ -10,20 +10,10 @@ import Foundation
import WebKit import WebKit
class PreloadedWebView: WKWebView { class PreloadedWebView: WKWebView {
private var isReady: Bool = false private var isReady: Bool = false
private var readyCompletion: (() -> Void)? private var readyCompletion: (() -> Void)?
static let userScripts: [WKUserScript] = {
var scripts = [WKUserScript]()
for fileName in ["main.js", "main_ios.js", "newsfoot.js"] {
let scriptSource = try! String(contentsOf: baseURL.appending(path: fileName, directoryHint: .notDirectory))
let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentStart, forMainFrameOnly: true, in: appScriptsWorld)
scripts.append(script)
}
return scripts
}()
init(articleIconSchemeHandler: ArticleIconSchemeHandler) { init(articleIconSchemeHandler: ArticleIconSchemeHandler) {
let preferences = WKPreferences() let preferences = WKPreferences()
preferences.javaScriptCanOpenWindowsAutomatically = false preferences.javaScriptCanOpenWindowsAutomatically = false
@ -35,26 +25,19 @@ class PreloadedWebView: WKWebView {
configuration.allowsInlineMediaPlayback = true configuration.allowsInlineMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = .audio configuration.mediaTypesRequiringUserActionForPlayback = .audio
configuration.setURLSchemeHandler(articleIconSchemeHandler, forURLScheme: ArticleRenderer.imageIconScheme) configuration.setURLSchemeHandler(articleIconSchemeHandler, forURLScheme: ArticleRenderer.imageIconScheme)
let userContentController = WKUserContentController()
let appScriptsWorld = WKContentWorld.world(name: "NetNewsWire")
for script in Self.userScripts {
userContentController.addUserScript(script)
}
configuration.userContentController = userContentController
super.init(frame: .zero, configuration: configuration) super.init(frame: .zero, configuration: configuration)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
} }
func preload() { func preload() {
navigationDelegate = self navigationDelegate = self
loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL) loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL)
} }
func ready(completion: @escaping () -> Void) { func ready(completion: @escaping () -> Void) {
if isReady { if isReady {
completeRequest(completion: completion) completeRequest(completion: completion)
@ -62,7 +45,7 @@ class PreloadedWebView: WKWebView {
readyCompletion = completion readyCompletion = completion
} }
} }
} }
// MARK: WKScriptMessageHandler // MARK: WKScriptMessageHandler
@ -76,17 +59,18 @@ extension PreloadedWebView: WKNavigationDelegate {
readyCompletion = nil readyCompletion = nil
} }
} }
} }
// MARK: Private // MARK: Private
private extension PreloadedWebView { private extension PreloadedWebView {
func completeRequest(completion: @escaping () -> Void) { func completeRequest(completion: @escaping () -> Void) {
isReady = false isReady = false
navigationDelegate = nil navigationDelegate = nil
completion() completion()
} }
} }