Adds ability to change search provider
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
51
App/Settings/Settings.swift
Normal file
51
App/Settings/Settings.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Settings.swift
|
||||
// App
|
||||
//
|
||||
// Created by James Magahern on 3/9/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@propertyWrapper
|
||||
public struct SettingProperty<T: RawRepresentable>
|
||||
{
|
||||
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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
42
App/Web Search/SearchProvider.swift
Normal file
42
App/Web Search/SearchProvider.swift
Normal file
@@ -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)!
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user