Skip to content

Commit

Permalink
Safari plugin #15: send selected text from Safari to text field in th…
Browse files Browse the repository at this point in the history
…e app
  • Loading branch information
filimo committed Oct 3, 2019
1 parent 43bc87b commit cba6550
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
6 changes: 6 additions & 0 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
F06DB10E23450B6000C2DE90 /* View.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06DB10C23450B6000C2DE90 /* View.swift */; };
F06DB1122345ECC000C2DE90 /* StatusBarView_Voice_Toggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06DB1112345ECC000C2DE90 /* StatusBarView_Voice_Toggle.swift */; };
F06DB1132345ECC000C2DE90 /* StatusBarView_Voice_Toggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06DB1112345ECC000C2DE90 /* StatusBarView_Voice_Toggle.swift */; };
F06DB117234611BB00C2DE90 /* ExtensionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06DB115234611BB00C2DE90 /* ExtensionManager.swift */; };
F06DB1182346172D00C2DE90 /* ExtensionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06DB115234611BB00C2DE90 /* ExtensionManager.swift */; };
F075443A234479DA00E1D88E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0754439234479DA00E1D88E /* AppDelegate.swift */; };
F075443E234479DB00E1D88E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F075443D234479DB00E1D88E /* Assets.xcassets */; };
F0754441234479DB00E1D88E /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F0754440234479DB00E1D88E /* Preview Assets.xcassets */; };
Expand Down Expand Up @@ -138,6 +140,7 @@
F06DB109234504FD00C2DE90 /* EditorNSTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorNSTextView.swift; sourceTree = "<group>"; };
F06DB10C23450B6000C2DE90 /* View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = View.swift; sourceTree = "<group>"; };
F06DB1112345ECC000C2DE90 /* StatusBarView_Voice_Toggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_Voice_Toggle.swift; sourceTree = "<group>"; };
F06DB115234611BB00C2DE90 /* ExtensionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionManager.swift; sourceTree = "<group>"; };
F0754437234479DA00E1D88E /* ReaderTranslatorMac.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ReaderTranslatorMac.app; sourceTree = BUILT_PRODUCTS_DIR; };
F0754439234479DA00E1D88E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F075443D234479DB00E1D88E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -391,6 +394,7 @@
children = (
F0AA69D7232E978E007CC07B /* Store.swift */,
F0039EC223447E24002F3F95 /* SharedContainer.swift */,
F06DB115234611BB00C2DE90 /* ExtensionManager.swift */,
);
path = Stores;
sourceTree = "<group>";
Expand Down Expand Up @@ -681,6 +685,7 @@
F0BB436C2344842200ADBEF1 /* ReaderView.swift in Sources */,
F0BB437A2344845000ADBEF1 /* StatusBarView_Zoom.swift in Sources */,
F0BB43812344846200ADBEF1 /* ReaderView_PDF.swift in Sources */,
F06DB117234611BB00C2DE90 /* ExtensionManager.swift in Sources */,
F0BB43722344843D00ADBEF1 /* FavoriteVoiceName.swift in Sources */,
F0BB43712344843900ADBEF1 /* UserDefault.swift in Sources */,
F0BB437C2344845000ADBEF1 /* StatusBarView_PdfPage.swift in Sources */,
Expand All @@ -695,6 +700,7 @@
buildActionMask = 2147483647;
files = (
F075445423447A2800E1D88E /* SafariExtensionViewController.swift in Sources */,
F06DB1182346172D00C2DE90 /* ExtensionManager.swift in Sources */,
F075445223447A2800E1D88E /* SafariExtensionHandler.swift in Sources */,
F0039EC423447E24002F3F95 /* SharedContainer.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
<key>ReaderTranslator.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
<integer>0</integer>
</dict>
<key>ReaderTranslatorMac.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>ReaderTranslatorSafari.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslator/Components/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ extension PageWebView {
override public var keyCommands: [UIKeyCommand]? {
//Voice selected text with any key since performCommand isn't fired because PageWebView isn't the first responder.
SpeechSynthesizer.speak(stopSpeaking: true, isVoiceEnabled: true)
return [.init(input: "1", modifierFlags: .command, action: #selector(performCommand))]
return [.init(input: "1", modifierFlags: .command, eventName: #selector(performCommand))]
}

@objc func performCommand(sender: UIKeyCommand) {
Expand Down
42 changes: 42 additions & 0 deletions ReaderTranslator/Stores/ExtensionManager.swift
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)
}
}

21 changes: 5 additions & 16 deletions ReaderTranslator/Views/ReaderView/ReaderView_Safari.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,20 @@ import SwiftUI

struct ReaderView_Safari: View {
@ObservedObject private var store = Store.shared
static private var timer = Timer()

var body: some View {
Group {
#if os(macOS)
if store.viewMode == .safari {
// EditorNSTextView(text: $text)
Text(store.selectedText)
.onAppear {
print("ReaderView_Safari_onAppear")
ReaderView_Safari.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
print("ReaderView_Safari_timer", self.store.selectedText)
if self.store.selectedText == SharedContainer.string() { return }
self.store.selectedText = SharedContainer.string()
SpeechSynthesizer.speak()
}
}
.onDisappear {
print("ReaderView_Safari_onDisappear")
ReaderView_Safari.timer.invalidate()
ReaderView_Safari.timer = Timer()
}
}
#endif
}
.onAppear {
SafariExtensionManager().start(onMessageChanged: { notificationName in
self.store.selectedText = SharedContainer.string()
})
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions ReaderTranslatorSafari/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0001</string>
<string>1.1</string>
<key>CFBundleVersion</key>
<string>1.0001</string>
<string>1.1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSExtension</key>
Expand Down
1 change: 1 addition & 0 deletions ReaderTranslatorSafari/SafariExtensionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
.removeDuplicates()
.sink { message in
SharedContainer.set(value: message)
SafariExtensionManager.didMessageChanged()
}

return pub
Expand Down

0 comments on commit cba6550

Please sign in to comment.