From 5635fb75eb2479cc75f927ce80ea8863e444ea0a Mon Sep 17 00:00:00 2001 From: Viktor Kushnerov Date: Sat, 19 Oct 2019 11:26:50 +0300 Subject: [PATCH] Fixes #36: Use one JavaScript file for all targets --- ReaderTranslator.xcodeproj/project.pbxproj | 60 ++++++++------- ReaderTranslator/Components/GTranslator.swift | 24 +++--- ReaderTranslator/Components/Reverso.swift | 30 ++++---- .../gtranslator-reverso-speaker.js | 0 .../Components/Scripts/reader-translator.js | 12 ++- .../reverso-reverso-speaker.js | 0 .../Components/WebKit/WKCoordinator.swift | 11 +++ .../Components/WebKit/WKRepresenter.swift | 24 +++--- .../Components/WebKit/WKScriptsSetup.swift | 7 +- .../Components/WebKit/reader-translator.js | 75 ------------------- ReaderTranslatorSafari/Info.plist | 2 +- 11 files changed, 90 insertions(+), 155 deletions(-) rename ReaderTranslator/Components/{WebKit => Scripts}/gtranslator-reverso-speaker.js (100%) rename ReaderTranslatorSafari/reader-translator-extension.js => ReaderTranslator/Components/Scripts/reader-translator.js (86%) rename ReaderTranslator/Components/{WebKit => Scripts}/reverso-reverso-speaker.js (100%) delete mode 100644 ReaderTranslator/Components/WebKit/reader-translator.js diff --git a/ReaderTranslator.xcodeproj/project.pbxproj b/ReaderTranslator.xcodeproj/project.pbxproj index cbc6662..1b422a1 100644 --- a/ReaderTranslator.xcodeproj/project.pbxproj +++ b/ReaderTranslator.xcodeproj/project.pbxproj @@ -38,7 +38,7 @@ F075445223447A2800E1D88E /* SafariExtensionHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = F075445123447A2800E1D88E /* SafariExtensionHandler.swift */; }; F075445423447A2800E1D88E /* SafariExtensionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F075445323447A2800E1D88E /* SafariExtensionViewController.swift */; }; F075445723447A2800E1D88E /* SafariExtensionViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F075445523447A2800E1D88E /* SafariExtensionViewController.xib */; }; - F075445A23447A2800E1D88E /* reader-translator-extension.js in Resources */ = {isa = PBXBuildFile; fileRef = F075445923447A2800E1D88E /* reader-translator-extension.js */; }; + F075445A23447A2800E1D88E /* reader-translator.js in Resources */ = {isa = PBXBuildFile; fileRef = F075445923447A2800E1D88E /* reader-translator.js */; }; F075445C23447A2800E1D88E /* ToolbarItemIcon.pdf in Resources */ = {isa = PBXBuildFile; fileRef = F075445B23447A2800E1D88E /* ToolbarItemIcon.pdf */; }; F075446023447A2800E1D88E /* ReaderTranslatorSafari.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = F075444E23447A2700E1D88E /* ReaderTranslatorSafari.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; F0814BA223562AF300212F52 /* Set.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0814BA123562AF300212F52 /* Set.swift */; }; @@ -46,8 +46,6 @@ F0814BA423562AF300212F52 /* Set.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0814BA123562AF300212F52 /* Set.swift */; }; F0A001C623587121009E9CD6 /* reverso-reverso-speaker.js in Resources */ = {isa = PBXBuildFile; fileRef = F0A001C523587121009E9CD6 /* reverso-reverso-speaker.js */; }; F0A001C723587121009E9CD6 /* reverso-reverso-speaker.js in Resources */ = {isa = PBXBuildFile; fileRef = F0A001C523587121009E9CD6 /* reverso-reverso-speaker.js */; }; - F0A10C8523537D38002908D5 /* reader-translator.js in Resources */ = {isa = PBXBuildFile; fileRef = F0A10C8423537D38002908D5 /* reader-translator.js */; }; - F0A10C8623537D38002908D5 /* reader-translator.js in Resources */ = {isa = PBXBuildFile; fileRef = F0A10C8423537D38002908D5 /* reader-translator.js */; }; F0AA69A1232E9710007CC07B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69A0232E9710007CC07B /* AppDelegate.swift */; }; F0AA69A3232E9710007CC07B /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69A2232E9710007CC07B /* SceneDelegate.swift */; }; F0AA69A5232E9710007CC07B /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69A4232E9710007CC07B /* ContentView.swift */; }; @@ -99,6 +97,8 @@ F0C36A9F2359C78A001E396C /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C36A9C2359C78A001E396C /* Array.swift */; }; F0C36AA12359D50A001E396C /* Clipboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C36AA02359D50A001E396C /* Clipboard.swift */; }; F0C36AA22359D50A001E396C /* Clipboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C36AA02359D50A001E396C /* Clipboard.swift */; }; + F0C36AA7235ADF5E001E396C /* reader-translator.js in Resources */ = {isa = PBXBuildFile; fileRef = F075445923447A2800E1D88E /* reader-translator.js */; }; + F0C36AA8235ADF5E001E396C /* reader-translator.js in Resources */ = {isa = PBXBuildFile; fileRef = F075445923447A2800E1D88E /* reader-translator.js */; }; F0C4EDA42349260000CCD97A /* ReversoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F01A50E823492398001DCC11 /* ReversoView.swift */; }; F0C4EDA52349260000CCD97A /* ReversoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F01A50E823492398001DCC11 /* ReversoView.swift */; }; F0C4EDA6234926D400CCD97A /* Reverso.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0A9F35A2349228A00970C97 /* Reverso.swift */; }; @@ -182,12 +182,11 @@ F075445323447A2800E1D88E /* SafariExtensionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariExtensionViewController.swift; sourceTree = ""; }; F075445623447A2800E1D88E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/SafariExtensionViewController.xib; sourceTree = ""; }; F075445823447A2800E1D88E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - F075445923447A2800E1D88E /* reader-translator-extension.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "reader-translator-extension.js"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.javascript; }; + F075445923447A2800E1D88E /* reader-translator.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "reader-translator.js"; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.javascript; }; F075445B23447A2800E1D88E /* ToolbarItemIcon.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = ToolbarItemIcon.pdf; sourceTree = ""; }; F075445D23447A2800E1D88E /* ReaderTranslatorSafari.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ReaderTranslatorSafari.entitlements; sourceTree = ""; }; F0814BA123562AF300212F52 /* Set.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Set.swift; sourceTree = ""; }; F0A001C523587121009E9CD6 /* reverso-reverso-speaker.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "reverso-reverso-speaker.js"; sourceTree = ""; }; - F0A10C8423537D38002908D5 /* reader-translator.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "reader-translator.js"; sourceTree = ""; }; F0A9F35A2349228A00970C97 /* Reverso.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reverso.swift; sourceTree = ""; }; F0AA699D232E9710007CC07B /* ReaderTranslator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ReaderTranslator.app; sourceTree = BUILT_PRODUCTS_DIR; }; F0AA69A0232E9710007CC07B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -315,7 +314,6 @@ F075445323447A2800E1D88E /* SafariExtensionViewController.swift */, F075445523447A2800E1D88E /* SafariExtensionViewController.xib */, F075445823447A2800E1D88E /* Info.plist */, - F075445923447A2800E1D88E /* reader-translator-extension.js */, F075445B23447A2800E1D88E /* ToolbarItemIcon.pdf */, F075445D23447A2800E1D88E /* ReaderTranslatorSafari.entitlements */, ); @@ -407,14 +405,15 @@ F0AA69D9232E979B007CC07B /* Components */ = { isa = PBXGroup; children = ( + F0C36AA32359FA13001E396C /* Scripts */, F0EE0A1B2348A645004A5EAD /* PDFKitView */, F0EE0A142347BEDB004A5EAD /* WebKit */, F0A9F35A2349228A00970C97 /* Reverso.swift */, - F0C36AA02359D50A001E396C /* Clipboard.swift */, F0AA69DB232E97AF007CC07B /* GTranslator.swift */, - F0AA69DA232E97AF007CC07B /* SpeechSynthesizer.swift */, - F06DB109234504FD00C2DE90 /* EditorNSTextView.swift */, F0D2E333234BA49000D95994 /* Safari.swift */, + F06DB109234504FD00C2DE90 /* EditorNSTextView.swift */, + F0AA69DA232E97AF007CC07B /* SpeechSynthesizer.swift */, + F0C36AA02359D50A001E396C /* Clipboard.swift */, ); path = Components; sourceTree = ""; @@ -452,6 +451,16 @@ path = StatusBarView; sourceTree = ""; }; + F0C36AA32359FA13001E396C /* Scripts */ = { + isa = PBXGroup; + children = ( + F075445923447A2800E1D88E /* reader-translator.js */, + F00C7C86235783FF003F6D28 /* gtranslator-reverso-speaker.js */, + F0A001C523587121009E9CD6 /* reverso-reverso-speaker.js */, + ); + path = Scripts; + sourceTree = ""; + }; F0C4EDA3234925B800CCD97A /* Modes */ = { isa = PBXGroup; children = ( @@ -467,9 +476,6 @@ F0EE0A142347BEDB004A5EAD /* WebKit */ = { isa = PBXGroup; children = ( - F0A10C8423537D38002908D5 /* reader-translator.js */, - F00C7C86235783FF003F6D28 /* gtranslator-reverso-speaker.js */, - F0A001C523587121009E9CD6 /* reverso-reverso-speaker.js */, F0AA69DC232E97B0007CC07B /* WKRepresenter.swift */, F0C53177234B3EE1003174B5 /* WKCoordinator.swift */, F0C53173234B06D3003174B5 /* WKScriptsSetup.swift */, @@ -613,8 +619,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + F0C36AA7235ADF5E001E396C /* reader-translator.js in Resources */, F00C7C88235783FF003F6D28 /* gtranslator-reverso-speaker.js in Resources */, - F0A10C8623537D38002908D5 /* reader-translator.js in Resources */, F0754444234479DB00E1D88E /* Main.storyboard in Resources */, F04FC5D8234CA63E002EFC0A /* Functional-Swift.pdf in Resources */, F0754441234479DB00E1D88E /* Preview Assets.xcassets in Resources */, @@ -629,7 +635,7 @@ files = ( F075445C23447A2800E1D88E /* ToolbarItemIcon.pdf in Resources */, F075445723447A2800E1D88E /* SafariExtensionViewController.xib in Resources */, - F075445A23447A2800E1D88E /* reader-translator-extension.js in Resources */, + F075445A23447A2800E1D88E /* reader-translator.js in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -638,11 +644,11 @@ buildActionMask = 2147483647; files = ( F040D57C2336BD97004567B8 /* README.md in Resources */, + F0C36AA8235ADF5E001E396C /* reader-translator.js in Resources */, F0C4EDAF234940D300CCD97A /* Functional-Swift.pdf in Resources */, F0AA69AD232E9712007CC07B /* LaunchScreen.storyboard in Resources */, F0A001C623587121009E9CD6 /* reverso-reverso-speaker.js in Resources */, F00C7C87235783FF003F6D28 /* gtranslator-reverso-speaker.js in Resources */, - F0A10C8523537D38002908D5 /* reader-translator.js in Resources */, F0AA69AA232E9712007CC07B /* Preview Assets.xcassets in Resources */, F0AA69A7232E9712007CC07B /* Assets.xcassets in Resources */, ); @@ -818,7 +824,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1.3.5; + CURRENT_PROJECT_VERSION = 1.3.6; DEVELOPMENT_ASSET_PATHS = "\"ReaderTranslatorMac/Preview Content\""; DEVELOPMENT_TEAM = B6QRXW5YF2; ENABLE_HARDENED_RUNTIME = YES; @@ -829,7 +835,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = by.filimo.ReaderTranslatorMac; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; @@ -846,7 +852,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1.3.5; + CURRENT_PROJECT_VERSION = 1.3.6; DEVELOPMENT_ASSET_PATHS = "\"ReaderTranslatorMac/Preview Content\""; DEVELOPMENT_TEAM = B6QRXW5YF2; ENABLE_HARDENED_RUNTIME = YES; @@ -857,7 +863,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = by.filimo.ReaderTranslatorMac; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; @@ -871,7 +877,7 @@ CODE_SIGN_ENTITLEMENTS = ReaderTranslatorSafari/ReaderTranslatorSafari.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1.3.5; + CURRENT_PROJECT_VERSION = 1.3.6; DEVELOPMENT_TEAM = B6QRXW5YF2; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = ReaderTranslatorSafari/Info.plist; @@ -881,7 +887,7 @@ "@executable_path/../../../../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = by.filimo.ReaderTranslatorMac.ReaderTranslatorSafari; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; @@ -896,7 +902,7 @@ CODE_SIGN_ENTITLEMENTS = ReaderTranslatorSafari/ReaderTranslatorSafari.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1.3.5; + CURRENT_PROJECT_VERSION = 1.3.6; DEVELOPMENT_TEAM = B6QRXW5YF2; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = ReaderTranslatorSafari/Info.plist; @@ -906,7 +912,7 @@ "@executable_path/../../../../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = by.filimo.ReaderTranslatorMac.ReaderTranslatorSafari; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; @@ -1035,7 +1041,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = ReaderTranslator/ReaderTranslator.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1.3.5; + CURRENT_PROJECT_VERSION = 1.3.6; DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES; DEVELOPMENT_ASSET_PATHS = "\"ReaderTranslator/Preview Content\""; DEVELOPMENT_TEAM = B6QRXW5YF2; @@ -1045,7 +1051,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = by.filimo.ReaderTranslator; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = YES; @@ -1060,7 +1066,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = ReaderTranslator/ReaderTranslator.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 1.3.5; + CURRENT_PROJECT_VERSION = 1.3.6; DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES; DEVELOPMENT_ASSET_PATHS = "\"ReaderTranslator/Preview Content\""; DEVELOPMENT_TEAM = B6QRXW5YF2; @@ -1070,7 +1076,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = by.filimo.ReaderTranslator; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = YES; diff --git a/ReaderTranslator/Components/GTranslator.swift b/ReaderTranslator/Components/GTranslator.swift index 9bfc670..4fb0357 100644 --- a/ReaderTranslator/Components/GTranslator.swift +++ b/ReaderTranslator/Components/GTranslator.swift @@ -101,21 +101,19 @@ struct GTranslator : ViewRepresentable, WKScriptsSetup { extension GTranslator.Coordinator: WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { - switch message.name { - case "onSelectionChange": - if let text = message.body as? String { - selectedText = .reverso(text: text) - } - case "onContextMenu": - print("onContextMenu") - case "onBodyLoaded": - print("onBodyLoaded") - case "onKeyDown": - if let code = message.body as? Int { - if code == 18 { SpeechSynthesizer.speak(text: selectedText.getText(), stopSpeaking: true, isVoiceEnabled: true) } + 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 = .reverso(text: text) + case "keydown": + if event.extra?.keyCode == 18 { //Alt + SpeechSynthesizer.speak(text: text, stopSpeaking: true, isVoiceEnabled: true) } default: - print("webkit.messageHandlers.\(message.name).postMessage() isn't found") + print("webkit.messageHandlers.\(event.name).postMessage() isn't found") } } } diff --git a/ReaderTranslator/Components/Reverso.swift b/ReaderTranslator/Components/Reverso.swift index 1d25422..f0f2752 100644 --- a/ReaderTranslator/Components/Reverso.swift +++ b/ReaderTranslator/Components/Reverso.swift @@ -61,26 +61,22 @@ struct Reverso : ViewRepresentable, WKScriptsSetup { extension Reverso.Coordinator: WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { - switch message.name { - case "onSelectionChange": - if let value = message.body as? String { - self.selectedText = value + 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 } + self.selectedText = text + case "keydown": + if event.extra?.keyCode == 17 { //Ctrl + store.translateAction = .translator(text: text, noReverso: true) } - case "onContextMenu": - print("onContextMenu") - case "onBodyLoaded": - print("onBodyLoaded") - case "onKeyDown": - if let code = message.body as? Int { - if code == 17 { //Ctrl - store.translateAction = .translator(text: self.selectedText, noReverso: true) - } - if code == 18 { //Alt - SpeechSynthesizer.speak(text: self.selectedText, stopSpeaking: true, isVoiceEnabled: true) - } + if event.extra?.keyCode == 18 { //Alt + SpeechSynthesizer.speak(text: text, stopSpeaking: true, isVoiceEnabled: true) } default: - print("webkit.messageHandlers.\(message.name).postMessage() isn't found") + print("webkit.messageHandlers.\(event.name).postMessage() isn't found") } } } diff --git a/ReaderTranslator/Components/WebKit/gtranslator-reverso-speaker.js b/ReaderTranslator/Components/Scripts/gtranslator-reverso-speaker.js similarity index 100% rename from ReaderTranslator/Components/WebKit/gtranslator-reverso-speaker.js rename to ReaderTranslator/Components/Scripts/gtranslator-reverso-speaker.js diff --git a/ReaderTranslatorSafari/reader-translator-extension.js b/ReaderTranslator/Components/Scripts/reader-translator.js similarity index 86% rename from ReaderTranslatorSafari/reader-translator-extension.js rename to ReaderTranslator/Components/Scripts/reader-translator.js index 0ad7a67..f8d2d5b 100644 --- a/ReaderTranslatorSafari/reader-translator-extension.js +++ b/ReaderTranslator/Components/Scripts/reader-translator.js @@ -45,6 +45,9 @@ } function _send(name, source, e) { + let text = document.getSelection().toString() + let sourceValue = (document.querySelector("#source") || {}).value + let entryValue = (document.querySelector("#entry") || {}).value let event = { time: Date(), // to prevent removing duplicate events name: name, @@ -56,10 +59,15 @@ shiftKey: e.shiftKey || keysStatus.shiftKey, which: e.which || keysStatus.which, keyCode: e.keyCode || keysStatus.keyCode, - selectedText: document.getSelection().toString(), + selectedText: text || sourceValue || entryValue, } } - safari.extension.dispatchMessage(JSON.stringify(event)) + + if(window.safari && safari.extension) { + safari.extension.dispatchMessage(JSON.stringify(event)) + }else{ + webkit.messageHandlers.send.postMessage(JSON.stringify(event)) + } } var sendIn500 = debounce(_send, 500) diff --git a/ReaderTranslator/Components/WebKit/reverso-reverso-speaker.js b/ReaderTranslator/Components/Scripts/reverso-reverso-speaker.js similarity index 100% rename from ReaderTranslator/Components/WebKit/reverso-reverso-speaker.js rename to ReaderTranslator/Components/Scripts/reverso-reverso-speaker.js diff --git a/ReaderTranslator/Components/WebKit/WKCoordinator.swift b/ReaderTranslator/Components/WebKit/WKCoordinator.swift index 6c49cd2..1fb6342 100644 --- a/ReaderTranslator/Components/WebKit/WKCoordinator.swift +++ b/ReaderTranslator/Components/WebKit/WKCoordinator.swift @@ -23,6 +23,17 @@ class WKCoordinator: NSObject { deinit { cancellableSet.cancelAndRemoveAll() } + + func getEvent(data: Any) -> DOMEvent? { + if let string = data as? String { + do { + return try JSONDecoder().decode(DOMEvent.self, from: Data(string.utf8)) + }catch{ + print(error.localizedDescription) + } + } + return nil + } } extension WKCoordinator: WKNavigationDelegate { diff --git a/ReaderTranslator/Components/WebKit/WKRepresenter.swift b/ReaderTranslator/Components/WebKit/WKRepresenter.swift index 77a08d6..8f161c6 100644 --- a/ReaderTranslator/Components/WebKit/WKRepresenter.swift +++ b/ReaderTranslator/Components/WebKit/WKRepresenter.swift @@ -80,23 +80,17 @@ struct WKRepresenter: ViewRepresentable, WKScriptsSetup { extension WKRepresenter.Coordinator: WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { - switch message.name { - case "onSelectionChange": - if let text = message.body as? String { - selectedText = text - } - print("onSelectionChange is not implemented") - case "onContextMenu": - print("onContextMenu") - case "onBodyLoaded": - print("onBodyLoaded") - case "onKeyDown": - if let code = message.body as? Int { - if code == 18 { SpeechSynthesizer.speak(text: selectedText, stopSpeaking: true, isVoiceEnabled: true) } + guard let event = getEvent(data: message.body) else { return } + + switch event.name { + case "selectionchange": + self.selectedText = event.extra?.selectedText ?? "" + case "keydown": + if event.extra?.keyCode == 18 { //Alt + SpeechSynthesizer.speak(text: self.selectedText, stopSpeaking: true, isVoiceEnabled: true) } - print("onKeyDown is not implemented") default: - print("webkit.messageHandlers.\(message.name).postMessage() isn't found") + print("webkit.messageHandlers.\(event.name).postMessage() isn't found") } } } diff --git a/ReaderTranslator/Components/WebKit/WKScriptsSetup.swift b/ReaderTranslator/Components/WebKit/WKScriptsSetup.swift index ef41717..b81ed5e 100644 --- a/ReaderTranslator/Components/WebKit/WKScriptsSetup.swift +++ b/ReaderTranslator/Components/WebKit/WKScriptsSetup.swift @@ -22,10 +22,7 @@ extension WKScriptsSetup { do { let script = try String(contentsOf: scriptUrl) - userContentController.add(coordinator, name: "onSelectionChange") - userContentController.add(coordinator, name: "onContextMenu") - userContentController.add(coordinator, name: "onBodyLoaded") - userContentController.add(coordinator, name: "onKeyDown") + userContentController.add(coordinator, name: "send") #if os(macOS) view.allowsMagnification = true @@ -60,7 +57,7 @@ extension WKScriptsSetup { print(error.localizedDescription) } } - + func webView(_ webView: WKPageView, didFinish navigation: WKNavigation!) {} func goBack(_ webView: WKPageView) {} } diff --git a/ReaderTranslator/Components/WebKit/reader-translator.js b/ReaderTranslator/Components/WebKit/reader-translator.js deleted file mode 100644 index 097a0b0..0000000 --- a/ReaderTranslator/Components/WebKit/reader-translator.js +++ /dev/null @@ -1,75 +0,0 @@ -/* - reader-translator.js - ReaderTranslator - - Created by Viktor Kushnerov on 13/10/19. - Copyright © 2019 Viktor Kushnerov. All rights reserved. -*/ -(function() { - function debounce(func, wait, immediate) { - // 'private' variable for instance - // The returned function will be able to reference this due to closure. - // Each call to the returned function will share this common timer. - var timeout; - - // Calling debounce returns a new anonymous function - return function() { - // reference the context and args for the setTimeout function - var context = this, - args = arguments; - - // Should the function be called now? If immediate is true - // and not already in a timeout then the answer is: Yes - var callNow = immediate && !timeout; - - // This is the basic debounce behaviour where you can call this - // function several times, but it will only execute once - // [before or after imposing a delay]. - // Each time the returned function is called, the timer starts over. - clearTimeout(timeout); - - // Set the new timeout - timeout = setTimeout(function() { - - // Inside the timeout function, clear the timeout variable - // which will let the next execution run when in 'immediate' mode - timeout = null; - - // Check if the function already ran with the immediate flag - if (!immediate) { - // Call the original function with apply - // apply lets you define the 'this' object as well as the arguments - // (both captured before setTimeout) - func.apply(context, args); - } - }, wait); - - // Immediate mode and no wait timer? Execute the function.. - if (callNow) func.apply(context, args); - } - } - - function _send(method, value) { - webkit.messageHandlers[method].postMessage(value) - } - - var sendIn100 = debounce(_send, 100) - var sendIn1000 = debounce(_send, 500) - - document.onselectionchange = function() { - var txt = document.getSelection().toString() - - sendIn1000('onSelectionChange', txt) - } - window.oncontextmenu = function() { - var txt = document.getSelection().toString() - - sendIn100('onContextMenu', txt) - } - document.body.onload = function() { - sendIn100('onBodyLoaded', '') - } - document.body.onkeydown = function(event) { - sendIn100('onKeyDown', event.keyCode) - } -})() diff --git a/ReaderTranslatorSafari/Info.plist b/ReaderTranslatorSafari/Info.plist index e13933b..4479800 100644 --- a/ReaderTranslatorSafari/Info.plist +++ b/ReaderTranslatorSafari/Info.plist @@ -36,7 +36,7 @@ Script - reader-translator-extension.js + reader-translator.js SFSafariToolbarItem