Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selective removal of content scripts to preserve web extension scripts #1178

Merged
merged 7 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public protocol UserContentControllerNewContent {

@objc(UserContentController)
final public class UserContentController: WKUserContentController {

public let privacyConfigurationManager: PrivacyConfigurationManaging
@MainActor
public weak var delegate: UserContentControllerDelegate?
Expand Down Expand Up @@ -78,7 +79,7 @@ final public class UserContentController: WKUserContentController {
@Published @MainActor public private(set) var contentBlockingAssets: ContentBlockingAssets? {
willSet {
self.removeAllContentRuleLists()
self.removeAllUserScripts()
self.removeInstalledUserScripts()

if let contentBlockingAssets = newValue {
Logger.contentBlocking.debug("📚 installing \(contentBlockingAssets.debugDescription)")
Expand Down Expand Up @@ -216,10 +217,39 @@ final public class UserContentController: WKUserContentController {
super.removeAllContentRuleLists()
}

#if os(macOS)
static let removeUserScriptSelector = "removeUserScript:"
private var removeUserScriptSupported: Bool {
responds(to: NSSelectorFromString("_" + Self.removeUserScriptSelector))
}
private var installedUserScripts = [WKUserScript]()
#endif

@MainActor
private func installUserScripts(_ wkUserScripts: [WKUserScript], handlers: [UserScript]) {
handlers.forEach { self.addHandler($0) }
wkUserScripts.forEach(self.addUserScript)

#if os(macOS)
installedUserScripts.append(contentsOf: wkUserScripts)
#endif
}

private func removeInstalledUserScripts() {
#if os(macOS)
// Avoid removal of web extension scripts
if removeUserScriptSupported {
for installedUserScript in installedUserScripts {
self.perform(NSSelectorFromString("_" + Self.removeUserScriptSelector), with: installedUserScript)
}
installedUserScripts.removeAll()
} else {
self.removeAllUserScripts()
}
#else
self.removeAllUserScripts()
#endif

}

@MainActor
Expand Down
1 change: 1 addition & 0 deletions Sources/Common/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ extension URL {
public static let about = NavigationalScheme(rawValue: "about")
public static let duck = NavigationalScheme(rawValue: "duck")
public static let mailto = NavigationalScheme(rawValue: "mailto")
public static let webkitExtension = NavigationalScheme(rawValue: "webkit-extension")

public init(rawValue: String) {
self.rawValue = rawValue
Expand Down
Loading