fix moegirl ad blocker and optimize func
This commit is contained in:
@@ -13,6 +13,7 @@ final class InAppWebViewController: UIViewController {
|
||||
private var backButton: UIBarButtonItem!
|
||||
private var forwardButton: UIBarButtonItem!
|
||||
private var refreshButton: UIBarButtonItem!
|
||||
private var adCleanupTimer: Timer?
|
||||
|
||||
init(url: String, title: String?) {
|
||||
urlString = url
|
||||
@@ -59,15 +60,24 @@ final class InAppWebViewController: UIViewController {
|
||||
self.view.bringSubviewToFront(self.progressView)
|
||||
self.observeProgress(webView)
|
||||
self.updateNavigationButtons()
|
||||
self.startAdCleanupTimer()
|
||||
guard let url = Self.resolveURL(self.urlString) else { return }
|
||||
webView.load(URLRequest(url: url))
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
adCleanupTimer?.invalidate()
|
||||
progressObservation?.invalidate()
|
||||
}
|
||||
|
||||
private func startAdCleanupTimer() {
|
||||
adCleanupTimer?.invalidate()
|
||||
adCleanupTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
|
||||
self?.webView?.evaluateJavaScript(WebViewAdBlock.shared.domCleanupScript(), completionHandler: nil)
|
||||
}
|
||||
}
|
||||
|
||||
private func setupToolbar() {
|
||||
toolbar.translatesAutoresizingMaskIntoConstraints = false
|
||||
view.addSubview(toolbar)
|
||||
@@ -171,8 +181,7 @@ extension InAppWebViewController: WKNavigationDelegate {
|
||||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||
progressView.isHidden = true
|
||||
updateNavigationButtons()
|
||||
let url = webView.url?.absoluteString ?? urlString
|
||||
webView.evaluateJavaScript(WebViewAdBlock.shared.injectionScript(url: url), completionHandler: nil)
|
||||
webView.evaluateJavaScript(WebViewAdBlock.shared.domCleanupScript(), completionHandler: nil)
|
||||
}
|
||||
|
||||
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
import ComposeApp
|
||||
import WebKit
|
||||
|
||||
/// 将 Kotlin [WebViewAdBlock] 规则编译为 WKContentRuleList,并预装页面脚本。
|
||||
/// 将 Kotlin [WebViewAdBlock] 规则编译为 WKContentRuleList。页面脚本由控制器定时执行,不预装进文档。
|
||||
enum WebViewContentBlocker {
|
||||
private static let identifier = "llmpAdBlock.v4"
|
||||
private static let identifier = "llmpAdBlock.v5"
|
||||
|
||||
static func configure(
|
||||
_ configuration: WKWebViewConfiguration,
|
||||
pageUrl: String,
|
||||
completion: @escaping () -> Void
|
||||
) {
|
||||
configuration.userContentController.addUserScript(
|
||||
WKUserScript(
|
||||
source: WebViewAdBlock.shared.injectionScript(url: pageUrl),
|
||||
injectionTime: .atDocumentEnd,
|
||||
forMainFrameOnly: true
|
||||
)
|
||||
)
|
||||
|
||||
let json = WebViewAdBlock.shared.buildContentBlockerJson()
|
||||
guard let store = WKContentRuleListStore.default() else {
|
||||
DispatchQueue.main.async { completion() }
|
||||
|
||||
Reference in New Issue
Block a user