Skip to content

Commit

Permalink
Speak selected text on PageWebView by any key.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Sep 22, 2019
1 parent a16dba3 commit 48208f7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ReaderTranslator/Components/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ struct WebView: UIViewRepresentable {
}
}


//This hack to make PageWebView the first responder but the selection won't work
//extension UIView {
// public override func becomeFirstResponder() -> Bool {
// // Actual view is instance of private class UIWebBrowserView, its parent parent view is UIWebView
// if self.superview?.superview is PageWebView {
// print(1)
// return false
// } else {
// return super.becomeFirstResponder()
// }
// }
//}

class PageWebView: WKWebView {
@Published private var selectedText = ""
@Published var newUrl = ""
Expand All @@ -47,6 +61,9 @@ class PageWebView: WKWebView {
document.body.onload = function() {
webkit.messageHandlers.onBodyLoaded.postMessage("txt")
}
document.body.onkeydown = function(event) {
webkit.messageHandlers.onKeyPress.postMessage(event.code)
}
"""

init() {
Expand All @@ -70,6 +87,7 @@ class PageWebView: WKWebView {
contentController.add(self, name: "onSelectionChange")
contentController.add(self, name: "onContextMenu")
contentController.add(self, name: "onBodyLoaded")
contentController.add(self, name: "onKeyPress")


_ = $selectedText
Expand Down Expand Up @@ -110,6 +128,18 @@ class PageWebView: WKWebView {
}
}

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.speech()
return [.init(input: "1", modifierFlags: .command, action: #selector(performCommand))]
}

@objc func performCommand(sender: UIKeyCommand) {
print(sender)
}
}

extension PageWebView: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {

Expand All @@ -122,6 +152,10 @@ extension PageWebView: WKScriptMessageHandler {
print("onContextMenu")
case "onBodyLoaded":
print("onBodyLoaded")
case "onKeyPress":
if let code = message.body as? String {
if code == "Space" { SpeechSynthesizer.speech() }
}
default:
print("webkit.messageHandlers.\(message.name).postMessage() isn't found")
}
Expand Down

0 comments on commit 48208f7

Please sign in to comment.