-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: message broker for user scripts
- Loading branch information
Shane Osbourne
committed
May 5, 2023
1 parent
6c2dd17
commit b80a0a6
Showing
7 changed files
with
1,031 additions
and
62 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
// | ||
// 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, UserScript, UserScriptMessaging { | ||
public var source: String = ""; | ||
|
||
public static let context = "specialPages" | ||
|
||
// special pages messaging cannot be isolated as we'll want regular page-scripts to be able to communicate | ||
public let broker = UserScriptMessageBroker(context: SpecialPagesUserScript.context, requiresRunInPageContentWorld: true ); | ||
|
||
public let messageNames: [String] = [ | ||
SpecialPagesUserScript.context | ||
] | ||
|
||
public let injectionTime: WKUserScriptInjectionTime = .atDocumentStart | ||
public let forMainFrameOnly: Bool = true | ||
public private(set) var requiresRunInPageContentWorld: Bool = true | ||
} | ||
|
||
@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) { | ||
let action = broker.messageHandlerFor(message); | ||
|
||
// special pages only support notifications on Catalina | ||
if case .notify = action { | ||
do { | ||
try broker.execute(action: action, original: message) { _ in | ||
message.webView?.evaluateJavaScript("console.log('🍎 doing nothing with notification')") | ||
} | ||
} catch let error { | ||
// there's no way of communicating this | ||
message.webView?.evaluateJavaScript("console.log('error \(error.localizedDescription)')") | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.