Redirect rules: for example, twitter->nitter

This commit is contained in:
James Magahern
2020-09-30 15:55:52 -07:00
parent d725bbb9d7
commit 1d27674d7d
3 changed files with 38 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
//
// PersonalRedirectRules.swift
// App
//
// Created by James Magahern on 9/30/20.
//
import Foundation
class PersonalRedirectRules
{
func redirectedURL(for url: URL) -> URL? {
if var components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
// Rule for nitter.net
if url.host == "twitter.com" {
components.host = "nitter.net"
return components.url
}
}
return nil
}
}

View File

@@ -21,6 +21,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
private let toolbarController = ToolbarViewController()
private let autocompleteViewController = AutocompleteViewController()
private let redirectRules = PersonalRedirectRules()
private var policyManager: ResourcePolicyManager { tabController.policyManager }
@@ -373,7 +374,15 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
}
preferences.allowsContentJavaScript = allowJavaScript
decisionHandler(.allow, preferences)
if let url = navigationAction.request.url,
let redirectedURL = redirectRules.redirectedURL(for: url)
{
tab.beginLoadingURL(redirectedURL)
decisionHandler(.cancel, preferences)
} else {
decisionHandler(.allow, preferences)
}
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {