diff --git a/App/Browser View/BrowserViewController.swift b/App/Browser View/BrowserViewController.swift index a00d4a9..c840b34 100644 --- a/App/Browser View/BrowserViewController.swift +++ b/App/Browser View/BrowserViewController.swift @@ -534,12 +534,7 @@ extension BrowserViewController: UITextFieldDelegate tab.beginLoadingURL(url) } else { - // Assume google search - let queryString = text - .replacingOccurrences(of: " ", with: "+") - .addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! - - let searchURL = URL(string: "https://google.com/search?q=\(queryString)&gbv=1")! // gbv=1: no JS + let searchURL = Settings.shared.searchProvider.provider().searchURLWithQuery(text) tab.beginLoadingURL(searchURL) } diff --git a/App/Settings/Settings.swift b/App/Settings/Settings.swift new file mode 100644 index 0000000..6e6ad8c --- /dev/null +++ b/App/Settings/Settings.swift @@ -0,0 +1,51 @@ +// +// Settings.swift +// App +// +// Created by James Magahern on 3/9/21. +// + +import Foundation + +@propertyWrapper +public struct SettingProperty +{ + public var key: String + public var defaultValue: T + public init(wrappedValue: T, key: String) { + self.key = key + self.defaultValue = wrappedValue + } + + public var wrappedValue: T { + get { + guard let rawValue = UserDefaults.standard.object(forKey: key) as? T.RawValue else { return defaultValue } + return T(rawValue: rawValue) ?? defaultValue + } + set { + UserDefaults.standard.setValue(newValue.rawValue, forKey: key) + } + } +} + +class Settings +{ + static let shared = Settings() + + public enum SearchProviderSetting: String, CaseIterable { + case google = "Google" + case duckduckgo = "DuckDuckGo" + case searxnor = "Searx.nor" + + func provider() -> SearchProvider { + switch self { + case .google: return SearchProvider.google + case .duckduckgo: return SearchProvider.duckduckgo + case .searxnor: return SearchProvider.searxnor + } + } + } + + @SettingProperty(key: "searchProvider") + public var searchProvider: SearchProviderSetting = .searxnor +} diff --git a/App/Settings/SettingsView.swift b/App/Settings/SettingsView.swift index dff0d87..d9ccad9 100644 --- a/App/Settings/SettingsView.swift +++ b/App/Settings/SettingsView.swift @@ -23,12 +23,30 @@ struct SettingsView: View { @Environment(\.presentationMode) @Binding private var presentationMode + @State private var searchProvider = Settings.shared.searchProvider { + didSet { Settings.shared.searchProvider = searchProvider } + } + var body: some View { NavigationView { List { Section(header: Text("Redirect Rules"), content: { Text("To Do") }) + + Section(header: Text("Search Provider"), content: { + ForEach(Settings.SearchProviderSetting.allCases, id: \.self, content: { setting in + Button(action: { searchProvider = setting }, label: { + HStack { + Text(setting.rawValue) + Spacer() + if searchProvider == setting { + Image(systemName: "checkmark") + } + } + }).buttonStyle(PlainButtonStyle()) + }) + }) } .listStyle(InsetGroupedListStyle()) .navigationBarTitle("Settings", displayMode: .inline) diff --git a/App/Web Search/SearchProvider.swift b/App/Web Search/SearchProvider.swift new file mode 100644 index 0000000..5e7c482 --- /dev/null +++ b/App/Web Search/SearchProvider.swift @@ -0,0 +1,42 @@ +// +// SearchProvider.swift +// App +// +// Created by James Magahern on 3/9/21. +// + +import Foundation + +class SearchProvider +{ + static let google = SearchProvider(resolver: { query in + // gbv=1: no JS + URL(string: "https://google.com/search?q=\(query.sanitized())&gbv=1")! + }) + + static let duckduckgo = SearchProvider(resolver: { query in + // html version is the one without JS + URL(string: "https://html.duckduckgo.com/html/?q=\(query.sanitized())")! + }) + + static let searxnor = SearchProvider(resolver: { query in + URL(string: "http://searx.nor/search?q=\(query.sanitized())&categories=general")! + }) + + func searchURLWithQuery(_ query: String) -> URL { + return resolver(query) + } + + internal let resolver: (String) -> URL + internal init(resolver: @escaping (String) -> URL) { + self.resolver = resolver + } +} + +fileprivate extension String { + func sanitized() -> String { + self + .replacingOccurrences(of: " ", with: "+") + .addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! + } +} diff --git a/SBrowser.xcodeproj/project.pbxproj b/SBrowser.xcodeproj/project.pbxproj index acd254b..d60b7f8 100644 --- a/SBrowser.xcodeproj/project.pbxproj +++ b/SBrowser.xcodeproj/project.pbxproj @@ -58,6 +58,8 @@ CDCE2664251AA80F007FE92A /* DocumentControlViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE2663251AA80F007FE92A /* DocumentControlViewController.swift */; }; CDCE2666251AA840007FE92A /* StackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE2665251AA840007FE92A /* StackView.swift */; }; CDCE2668251AAA9A007FE92A /* FontSizeAdjustView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE2667251AAA9A007FE92A /* FontSizeAdjustView.swift */; }; + CDD0522125F8023700DD1771 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD0522025F8023700DD1771 /* Settings.swift */; }; + CDD0522425F8055700DD1771 /* SearchProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD0522325F8055700DD1771 /* SearchProvider.swift */; }; CDE6A30425F023BC00E912A4 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDE6A30325F023BC00E912A4 /* SettingsViewController.swift */; }; CDE6A30625F023EA00E912A4 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDE6A30525F023EA00E912A4 /* SettingsView.swift */; }; CDEDD8AA25D62ADB00862605 /* UITraitCollection+MacLike.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDEDD8A925D62ADB00862605 /* UITraitCollection+MacLike.swift */; }; @@ -148,6 +150,8 @@ CDCE2663251AA80F007FE92A /* DocumentControlViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentControlViewController.swift; sourceTree = ""; }; CDCE2665251AA840007FE92A /* StackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackView.swift; sourceTree = ""; }; CDCE2667251AAA9A007FE92A /* FontSizeAdjustView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontSizeAdjustView.swift; sourceTree = ""; }; + CDD0522025F8023700DD1771 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = ""; }; + CDD0522325F8055700DD1771 /* SearchProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchProvider.swift; sourceTree = ""; }; CDE6A30325F023BC00E912A4 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = ""; }; CDE6A30525F023EA00E912A4 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = ""; }; CDEDD8A925D62ADB00862605 /* UITraitCollection+MacLike.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITraitCollection+MacLike.swift"; sourceTree = ""; }; @@ -265,6 +269,7 @@ 1AB88F0424D3E1F90006F850 /* Titlebar and URL Bar */, 1ADFF4C124CA6AE4006DC7AE /* Utilities */, 1ADFF4AF24C92E2F006DC7AE /* Web Process Bundle Bridge */, + CDD0522225F8054A00DD1771 /* Web Search */, 1A14FC2424D2517A009B3F83 /* Resources */, 1ADFF47624C7DF7F006DC7AE /* Supporting Files */, ); @@ -392,11 +397,20 @@ path = "Document Controls UI"; sourceTree = ""; }; + CDD0522225F8054A00DD1771 /* Web Search */ = { + isa = PBXGroup; + children = ( + CDD0522325F8055700DD1771 /* SearchProvider.swift */, + ); + path = "Web Search"; + sourceTree = ""; + }; CDE6A30225F023A000E912A4 /* Settings */ = { isa = PBXGroup; children = ( CDE6A30325F023BC00E912A4 /* SettingsViewController.swift */, CDE6A30525F023EA00E912A4 /* SettingsView.swift */, + CDD0522025F8023700DD1771 /* Settings.swift */, ); path = Settings; sourceTree = ""; @@ -513,6 +527,7 @@ 1ADFF4C024CA6964006DC7AE /* URLBar.swift in Sources */, CDCE2666251AA840007FE92A /* StackView.swift in Sources */, CD853BD124E778B800D2BDCC /* History.xcdatamodeld in Sources */, + CDD0522125F8023700DD1771 /* Settings.swift in Sources */, 1AD31040252545BF00A4A952 /* FindOnPageView.swift in Sources */, CD470C4225DE056600AFBE0E /* BrowserViewController+WebKitDelegate.swift in Sources */, CDEDD8AA25D62ADB00862605 /* UITraitCollection+MacLike.swift in Sources */, @@ -525,6 +540,7 @@ 1ADFF4D024CBBCD1006DC7AE /* ScriptPolicyControl.swift in Sources */, 1A03810D24E71CA700826501 /* ToolbarView.swift in Sources */, CD470C4425DE070400AFBE0E /* BrowserViewController+Keyboard.swift in Sources */, + CDD0522425F8055700DD1771 /* SearchProvider.swift in Sources */, CD853BD424E77BF900D2BDCC /* HistoryItem.swift in Sources */, 1ADFF48D24C8C176006DC7AE /* SBRProcessBundleBridge.m in Sources */, 1AD3103D252541E600A4A952 /* PersonalRedirectRules.swift in Sources */,