2020-07-07 04:14:05 +02:00
|
|
|
|
//
|
|
|
|
|
// PreloadedWebView.swift
|
|
|
|
|
// Multiplatform iOS
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 7/6/20.
|
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import WebKit
|
2020-07-28 02:25:39 +02:00
|
|
|
|
import RSWeb
|
2020-07-07 04:14:05 +02:00
|
|
|
|
|
|
|
|
|
class PreloadedWebView: WKWebView {
|
|
|
|
|
|
|
|
|
|
private var isReady: Bool = false
|
2020-09-02 01:54:46 +02:00
|
|
|
|
private var readyCompletion: (() -> Void)?
|
2020-07-07 04:14:05 +02:00
|
|
|
|
|
|
|
|
|
init(articleIconSchemeHandler: ArticleIconSchemeHandler) {
|
|
|
|
|
let preferences = WKPreferences()
|
|
|
|
|
preferences.javaScriptCanOpenWindowsAutomatically = false
|
|
|
|
|
|
|
|
|
|
let configuration = WKWebViewConfiguration()
|
|
|
|
|
configuration.preferences = preferences
|
|
|
|
|
configuration.setValue(true, forKey: "allowUniversalAccessFromFileURLs")
|
2020-07-08 02:31:24 +02:00
|
|
|
|
#if os(iOS)
|
2020-07-07 04:14:05 +02:00
|
|
|
|
configuration.allowsInlineMediaPlayback = true
|
2020-07-08 02:31:24 +02:00
|
|
|
|
#endif
|
2020-07-07 04:14:05 +02:00
|
|
|
|
configuration.mediaTypesRequiringUserActionForPlayback = .audio
|
|
|
|
|
configuration.setURLSchemeHandler(articleIconSchemeHandler, forURLScheme: ArticleRenderer.imageIconScheme)
|
|
|
|
|
|
|
|
|
|
super.init(frame: .zero, configuration: configuration)
|
2020-07-28 02:25:39 +02:00
|
|
|
|
|
|
|
|
|
if let userAgent = UserAgent.fromInfoPlist() {
|
|
|
|
|
customUserAgent = userAgent
|
|
|
|
|
}
|
2020-07-07 04:14:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
super.init(coder: coder)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func preload() {
|
|
|
|
|
navigationDelegate = self
|
|
|
|
|
loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
|
func ready(completion: @escaping () -> Void) {
|
2020-07-07 04:14:05 +02:00
|
|
|
|
if isReady {
|
|
|
|
|
completeRequest(completion: completion)
|
|
|
|
|
} else {
|
|
|
|
|
readyCompletion = completion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 17:24:16 +02:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
|
|
|
|
|
// There’s no API for affecting a WKWebView’s contextual menu.
|
|
|
|
|
// (WebView had API for this.)
|
|
|
|
|
//
|
|
|
|
|
// This a minor hack. It hides unwanted menu items.
|
|
|
|
|
// The menu item identifiers are not documented anywhere;
|
|
|
|
|
// they could change, and this code would need updating.
|
|
|
|
|
for menuItem in menu.items {
|
|
|
|
|
if shouldHideMenuItem(menuItem) {
|
|
|
|
|
menuItem.isHidden = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
super.willOpenMenu(menu, with: event)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2020-07-07 04:14:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: WKScriptMessageHandler
|
|
|
|
|
|
|
|
|
|
extension PreloadedWebView: WKNavigationDelegate {
|
|
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
|
|
isReady = true
|
|
|
|
|
if let completion = readyCompletion {
|
|
|
|
|
completeRequest(completion: completion)
|
|
|
|
|
readyCompletion = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
|
|
private extension PreloadedWebView {
|
|
|
|
|
|
2020-09-02 01:54:46 +02:00
|
|
|
|
func completeRequest(completion: @escaping () -> Void) {
|
2020-07-07 04:14:05 +02:00
|
|
|
|
isReady = false
|
|
|
|
|
navigationDelegate = nil
|
2020-09-02 01:54:46 +02:00
|
|
|
|
completion()
|
2020-07-07 04:14:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-07-08 17:24:16 +02:00
|
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
private extension NSUserInterfaceItemIdentifier {
|
|
|
|
|
|
|
|
|
|
static let DetailMenuItemIdentifierReload = NSUserInterfaceItemIdentifier(rawValue: "WKMenuItemIdentifierReload")
|
|
|
|
|
static let DetailMenuItemIdentifierOpenLink = NSUserInterfaceItemIdentifier(rawValue: "WKMenuItemIdentifierOpenLink")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension PreloadedWebView {
|
|
|
|
|
|
|
|
|
|
static let menuItemIdentifiersToHide: [NSUserInterfaceItemIdentifier] = [.DetailMenuItemIdentifierReload, .DetailMenuItemIdentifierOpenLink]
|
|
|
|
|
static let menuItemIdentifierMatchStrings = ["newwindow", "download"]
|
|
|
|
|
|
|
|
|
|
func shouldHideMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
|
|
|
|
|
|
|
|
guard let identifier = menuItem.identifier else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if PreloadedWebView.menuItemIdentifiersToHide.contains(identifier) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let lowerIdentifier = identifier.rawValue.lowercased()
|
|
|
|
|
for matchString in PreloadedWebView.menuItemIdentifierMatchStrings {
|
|
|
|
|
if lowerIdentifier.contains(matchString) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|