Adds customizable user script and user stylesheet.

This commit is contained in:
James Magahern
2021-12-16 18:40:45 -08:00
parent 026306b6df
commit b79f0ac5db
7 changed files with 143 additions and 2 deletions

View File

@@ -0,0 +1,82 @@
//
// CodeEditorSettingsViewController.swift
// SBrowser
//
// Copyright © 2021 Apple Inc. All rights reserved.
//
import UIKit
class CodeEditorSettingsView: UIView
{
public let textView = UITextView()
override init(frame: CGRect) {
super.init(frame: frame)
textView.font = .monospacedSystemFont(ofSize: 12.0, weight: .regular)
textView.autocorrectionType = .no
textView.autocapitalizationType = .none
textView.smartDashesType = .no
textView.smartQuotesType = .no
addSubview(textView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
textView.frame = bounds.inset(by: layoutMargins).insetBy(dx: 0.0, dy: 18.0)
}
}
class CodeEditorSettingsViewController: UIViewController, UITextViewDelegate
{
private let settingsKeypath: ReferenceWritableKeyPath<Settings, String>
private var saveTimer: Timer?
private let settingsView = CodeEditorSettingsView(frame: .zero)
public init(settingsKeypath: ReferenceWritableKeyPath<Settings, String>) {
self.settingsKeypath = settingsKeypath
super.init(nibName: nil, bundle: nil)
if settingsKeypath == \.userStylesheet {
tabBarItem.title = "Stylesheet"
tabBarItem.image = UIImage(systemName: "newspaper")
} else if settingsKeypath == \.userScript {
tabBarItem.title = "Script"
tabBarItem.image = UIImage(systemName: "applescript")
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
self.view = settingsView
}
override func viewDidLoad() {
super.viewDidLoad()
settingsView.textView.delegate = self
settingsView.textView.text = Settings.shared[keyPath: settingsKeypath]
}
private func saveContents() {
Settings.shared[keyPath: settingsKeypath] = settingsView.textView.text!
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if self.saveTimer?.isValid == true { return true }
self.saveTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: { [weak self] timer in
self?.saveContents()
})
return true
}
}

View File

@@ -28,6 +28,8 @@ public struct SettingProperty<T: RawRepresentable>
}
}
// - These coercions into RawRepresentable are stupid. How do I write specializations for each type instead?
extension Dictionary: RawRepresentable where Key == String, Value == String {
public typealias RawValue = [String: String]
@@ -41,6 +43,18 @@ extension Dictionary: RawRepresentable where Key == String, Value == String {
}
}
extension String: RawRepresentable {
public typealias RawValue = String
public init?(rawValue: String) {
self.init(rawValue)
}
public var rawValue: String {
return self
}
}
class Settings
{
static let shared = Settings()
@@ -80,4 +94,10 @@ class Settings
return nil
}
@SettingProperty(key: "userScript")
public var userScript: String = ""
@SettingProperty(key: "userStylesheet")
public var userStylesheet: String = ""
}

View File

@@ -30,6 +30,8 @@ class SettingsViewController: UITabBarController, NSToolbarDelegate
self.viewControllers = [
GeneralSettingsViewController(),
RedirectRulesSettingsViewController(),
CodeEditorSettingsViewController(settingsKeypath: \.userScript),
CodeEditorSettingsViewController(settingsKeypath: \.userStylesheet),
]
navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction { [unowned self] _ in

View File

@@ -45,6 +45,8 @@ NS_SWIFT_NAME(ProcessBundleBridge)
- (void)parseDocumentForReaderMode:(void(^)(NSString *))completionBlock NS_SWIFT_NAME(parseDocumentForReaderMode(completion:));
- (void)reloadCustomizedUserScriptsAndStylesheets;
- (instancetype)init NS_UNAVAILABLE;
@end

View File

@@ -34,6 +34,10 @@
WKUserScript *_readabilityScript;
NSArray<WKUserScript *> *_userScripts;
// These come from settings.
_WKUserStyleSheet *_customizedUserStylesheet;
WKUserScript *_customizedUserScript;
}
- (void)tearDown
@@ -101,6 +105,9 @@
[userContentController addUserScript:script];
}
// Reload customized user scripts/stylesheets from settings
[self reloadCustomizedUserScriptsAndStylesheets];
// Instantiate web view
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webViewConfiguration];
@@ -135,6 +142,30 @@
return _userScripts;
}
- (void)reloadCustomizedUserScriptsAndStylesheets
{
WKUserContentController *userContentController = [_webViewConfiguration userContentController];
if (_customizedUserScript) {
[userContentController _removeUserScript:_customizedUserScript];
}
NSString *scriptSource = [[NSUserDefaults standardUserDefaults] stringForKey:@"userScript"];
if ([scriptSource length]) {
_customizedUserScript = [[WKUserScript alloc] initWithSource:scriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[userContentController addUserScript:_customizedUserScript];
}
if (_customizedUserStylesheet) {
[userContentController _removeUserStyleSheet:_customizedUserStylesheet];
}
NSString *stylesheetSource = [[NSUserDefaults standardUserDefaults] stringForKey:@"userStylesheet"];
if ([stylesheetSource length]) {
_customizedUserStylesheet = [[_WKUserStyleSheet alloc] initWithSource:stylesheetSource forMainFrameOnly:YES];
[userContentController _addUserStyleSheet:_customizedUserStylesheet];
}
}
#pragma mark <SBRWebProcessDelegate>
- (void)webProcessDidConnect