-
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.
Merriam-Webster support #75: Add it to macOS.
- Loading branch information
Showing
11 changed files
with
174 additions
and
2 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
78 changes: 78 additions & 0 deletions
78
ReaderTranslator/Components/ViewRepresentable/MerriamWebsterRepresenter.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,78 @@ | ||
// | ||
// MerriamWebsterRepresenter.swift | ||
// ReaderTranslator | ||
// | ||
// Created by Viktor Kushnerov on 20/1/20. | ||
// Copyright © 2020 Viktor Kushnerov. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import WebKit | ||
|
||
struct MerriamWebsterRepresenter: ViewRepresentable, WKScriptsSetup { | ||
@Binding var selectedText: TranslateAction | ||
private let defaultURL = "https://www.merriam-webster.com/dictionary/" | ||
|
||
static var coorinator: Coordinator? | ||
static var pageView: WKPageView? | ||
|
||
class Coordinator: WKCoordinator { | ||
var selectedText = "" | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
makeCoordinator(coordinator: Coordinator(self)) | ||
} | ||
|
||
func makeView(context: Context) -> WKPageView { | ||
if let view = Self.pageView { return view } | ||
|
||
let view = WKPageView() | ||
loadWithRuleList(urlString: defaultURL, view: view, file: "merriam-webster") | ||
Self.pageView = view | ||
|
||
setupScriptCoordinator(view: view, coordinator: context.coordinator) | ||
|
||
return view | ||
} | ||
|
||
func updateView(_ view: WKPageView, context _: Context) { | ||
guard case var .merriamWebster(text) = selectedText else { return } | ||
text = text.replacingOccurrences(of: "\n", with: " ") | ||
Store.shared.translateAction.next() | ||
|
||
print("\(theClassName)_updateView_update", text) | ||
|
||
let search = text.replacingOccurrences(of: " ", with: "%20") | ||
let urlString = "\(defaultURL)\(search)" | ||
|
||
if view.url?.absoluteString == urlString { return } | ||
|
||
if URL(string: urlString.encodeUrl) != nil { | ||
print("\(theClassName)_updateView_reload", urlString) | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { | ||
self.loadWithRuleList(urlString: urlString.encodeUrl, view: view, file: "merriam-webster") | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension MerriamWebsterRepresenter.Coordinator: WKScriptMessageHandler { | ||
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) { | ||
guard let event = getEvent(data: message.body) else { return } | ||
var text: String { event.extra?.selectedText ?? "" } | ||
|
||
switch event.name { | ||
case "selectionchange": | ||
guard let text = event.extra?.selectedText else { return } | ||
selectedText = text | ||
store.translateAction.addAll(text: text, except: .macmillan) | ||
case "keydown": | ||
if event.extra?.keyCode == 18 { // Alt | ||
SpeechSynthesizer.speak(text: text, stopSpeaking: true, isVoiceEnabled: true) | ||
} | ||
default: | ||
print("webkit.messageHandlers.\(event.name).postMessage() isn't found") | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
ReaderTranslator/Components/ViewRepresentable/Scripts/merriam-webster.json
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,34 @@ | ||
[ | ||
{ | ||
"trigger": { | ||
"url-filter": "https://.*.google.*.com/.*" | ||
}, | ||
"action": { | ||
"type": "block" | ||
} | ||
}, | ||
{ | ||
"trigger": { | ||
"url-filter": "//.*amazon-adsystem.com/.*" | ||
}, | ||
"action": { | ||
"type": "block" | ||
} | ||
}, | ||
{ | ||
"trigger": { | ||
"url-filter": "https://.*doubleclick.net/.*" | ||
}, | ||
"action": { | ||
"type": "block" | ||
} | ||
}, | ||
{ | ||
"trigger": { | ||
"url-filter": "https://.*heapanalytics.com" | ||
}, | ||
"action": { | ||
"type": "block" | ||
} | ||
} | ||
] |
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
20 changes: 20 additions & 0 deletions
20
ReaderTranslator/Views/ReaderView/Modes/MerriamWebsterView.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,20 @@ | ||
// | ||
// MerriamWebsterView.swift | ||
// ReaderTranslator | ||
// | ||
// Created by Viktor Kushnerov on 20/1/20. | ||
// Copyright © 2020 Viktor Kushnerov. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct MerriamWebsterView: View { | ||
@ObservedObject private var store = Store.shared | ||
@ObservedObject private var viewsStore = ViewsStore.shared | ||
|
||
var body: some View { | ||
WebViewContainer { | ||
MerriamWebsterRepresenter(selectedText: self.$store.translateAction) | ||
}.frame(width: viewsStore.viewWidth[.merriamWebster] ?? ViewsStore.defaultWidth) | ||
} | ||
} |
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
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.