-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Safari plugin #15: send selected text from Safari to text field in th…
…e app
- Loading branch information
Showing
7 changed files
with
60 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// ExtensionListener.swift | ||
// ReaderTranslator | ||
// | ||
// Created by Viktor Kushnerov on 10/3/19. | ||
// Copyright © 2019 Viktor Kushnerov. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class SafariExtensionManager { | ||
typealias onMessageChangedType = (_ name: String) -> () | ||
|
||
static private let center = CFNotificationCenterGetDarwinNotifyCenter() | ||
|
||
static private let domainName = "by.filimo.ReaderTranslatorMac.ReaderTranslatorSafari" | ||
static private let notificationName = "onMessagedChanged" | ||
static private var eventName: CFString { "\(domainName).\(notificationName)" as CFString } | ||
|
||
private var onMessageChanged: onMessageChangedType? | ||
|
||
func start(onMessageChanged: @escaping onMessageChangedType) { | ||
self.onMessageChanged = onMessageChanged | ||
CFNotificationCenterAddObserver(Self.center, Unmanaged.passRetained(self).toOpaque(), callBack, Self.eventName, nil, .deliverImmediately) | ||
} | ||
|
||
deinit { | ||
CFNotificationCenterRemoveEveryObserver(Self.center, Unmanaged.passRetained(self).toOpaque()) | ||
} | ||
|
||
private var callBack: CFNotificationCallback = { (_, observer, name, object, _) -> () in | ||
if let observer = observer, let name = name { | ||
let safariExtensionManagerSelf = Unmanaged<SafariExtensionManager>.fromOpaque(observer).takeUnretainedValue() | ||
safariExtensionManagerSelf.onMessageChanged?(name.rawValue as String) | ||
} | ||
} | ||
|
||
static func didMessageChanged() { | ||
CFNotificationCenterPostNotification(center, .init(eventName), nil, nil, true) | ||
} | ||
} | ||
|
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