-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shane Osbourne
committed
Apr 22, 2023
1 parent
3a829df
commit f4ffc54
Showing
4 changed files
with
126 additions
and
34 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
Sources/BrowserServicesKit/ContentScopeScript/SpecialPagesUserScript.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Created by shane osbourne on 22/04/2023. | ||
// | ||
|
||
import Foundation | ||
import WebKit | ||
import Combine | ||
import ContentScopeScripts | ||
import UserScript | ||
import Common | ||
import os.log | ||
|
||
public final class SpecialPagesUserScript: NSObject, StaticUserScript, UserScriptMessaging { | ||
|
||
public let broker = UserScriptMessageBroker(context: "specialPages") | ||
|
||
public let messageNames: [String] = [ | ||
"specialPages" | ||
] | ||
|
||
public var requiresRunInPageContentWorld: Bool { | ||
return true | ||
} | ||
|
||
public static var injectionTime: WKUserScriptInjectionTime { .atDocumentEnd } | ||
public static var forMainFrameOnly: Bool { true } | ||
public static var source: String = "" | ||
public static var script: WKUserScript = SpecialPagesUserScript.makeWKUserScript() | ||
} | ||
|
||
@available(macOS 11, *) | ||
extension SpecialPagesUserScript: WKScriptMessageHandlerWithReply { | ||
public func userContentController(_ userContentController: WKUserContentController, | ||
didReceive message: WKScriptMessage, | ||
replyHandler: @escaping (Any?, String?) -> Void) { | ||
let action = broker.messageHandlerFor(message) | ||
do { | ||
try broker.execute(action: action, original: message) { json in | ||
replyHandler(json, nil) | ||
} | ||
} catch let error { | ||
// forward uncaught errors to the client | ||
replyHandler(nil, error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Fallback for macOS 10.15 | ||
extension SpecialPagesUserScript: WKScriptMessageHandler { | ||
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | ||
// secure messaging not supported for ContentScopeScripts | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters