-
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.
Centralised Messaging for User Scripts (#299)
* feature: support message brokering for user scripts * Don't register handlers in none-isolated world (for now) * PR feedback --------- Co-authored-by: Shane Osbourne <sosbourne@duckduckgo.com>
- Loading branch information
1 parent
cbd9ed8
commit 3474664
Showing
6 changed files
with
944 additions
and
30 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 @@ | ||
// | ||
// SpecialPagesUserScript.swift | ||
// | ||
// Copyright © 2021 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
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 = true | ||
public let requiresRunInPageContentWorld = true | ||
} | ||
|
||
@available(macOS 11.0, iOS 14.0, *) | ||
extension SpecialPagesUserScript: WKScriptMessageHandlerWithReply { | ||
public func userContentController(_ userContentController: WKUserContentController, | ||
didReceive message: WKScriptMessage) async -> (Any?, String?) { | ||
let action = broker.messageHandlerFor(message) | ||
do { | ||
let json = try await broker.execute(action: action, original: message) | ||
return (json, nil) | ||
} catch { | ||
// forward uncaught errors to the client | ||
return (nil, error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Fallback for macOS 10.15 | ||
extension SpecialPagesUserScript: WKScriptMessageHandler { | ||
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | ||
// unsupported | ||
} | ||
} |
Oops, something went wrong.