|
1 | 1 | // @name Add Translation Buttons |
2 | | -// @version 2.1.6 |
| 2 | +// @version 2.2.0 |
3 | 3 | // @description Adds a button to translate the text associated with a wayspot |
4 | 4 | // @author AlterTobi |
| 5 | +// @match https://wayfarer.nianticlabs.com/* |
| 6 | +// @match https://www.deepl.com/* |
5 | 7 |
|
6 | 8 | (function() { |
7 | 9 | "use strict"; |
8 | 10 |
|
| 11 | + const ORIGIN_WAYFARER = "https://wayfarer.nianticlabs.com"; |
| 12 | + const ORIGIN_DEEPL = "https://www.deepl.com"; |
| 13 | + const ORIGIN_GOOGLE = "https://translate.google.com"; |
| 14 | + |
| 15 | + // ----- BEGIN - the Wayfarer part ------ |
9 | 16 | const myCSSId = "wfesTranslateCSS"; |
10 | 17 | const myStyle = `.wfesTranslate { |
11 | 18 | color: #333; |
|
24 | 31 | display: block; /* Button wird unterhalb des Selects angezeigt */ |
25 | 32 | text-decoration: none; |
26 | 33 | color: #20B8E3; |
| 34 | + margin: 0 auto; |
27 | 35 | } |
28 | 36 | .dark .wfesTranslate select, |
29 | 37 | .dark .wfesTranslate option { |
30 | 38 | background: #000; |
31 | 39 | }`; |
32 | 40 |
|
33 | 41 | const engines ={ |
34 | | - Google: {name: "Google", title: "Google translate", url: "https://translate.google.com/?sl=auto&q=", target: "wfesTranslateGoogle"}, |
35 | | - Deepl: {name: "Deepl", title: "DeepL translate", url: "https://www.deepl.com/translator#auto/"+navigator.language+"/", target: "wfesTranslateDeepl"} |
| 42 | + Google: {name: "Google", title: "Google translate", url: "https://translate.google.com/?sl=auto&q=", target: "wfesTranslateGoogle", twindow: null, origin: ORIGIN_GOOGLE}, |
| 43 | + Deepl: {name: "Deepl", title: "DeepL translate", url: "https://www.deepl.com/translator#auto/"+navigator.language+"/", target: "wfesTranslateDeepl", twindow: null, origin: ORIGIN_DEEPL} |
36 | 44 | }; |
37 | 45 |
|
38 | 46 | const buttonID = "wfesTranslateButton"; |
39 | 47 | const storageName = "wfes_translateEngine"; |
40 | 48 | let currentEngine; |
41 | 49 |
|
42 | | - |
43 | 50 | function removeButton() { |
44 | 51 | const button = document.getElementById(buttonID); |
45 | 52 | if (button !== null) { |
46 | 53 | button.remove(); |
47 | 54 | } |
48 | 55 | } |
49 | 56 |
|
50 | | - function init() { |
51 | | - window.wfes.f.addCSS(myCSSId, myStyle); |
52 | | - window.wfes.f.localGet(storageName, "Deepl").then(e => { |
53 | | - currentEngine = e; |
54 | | - }); |
| 57 | + function sendTextToTranslateWindow(fenster, text, origin) { |
| 58 | + try { |
| 59 | + fenster.postMessage({ |
| 60 | + type: "translate", |
| 61 | + payload: text |
| 62 | + }, origin); // "*" = alle Ursprünge erlauben (für Google Translate nötig) |
| 63 | + } catch (e) { |
| 64 | + console.warn("Nachricht konnte nicht gesendet werden:", e.message); |
| 65 | + } |
55 | 66 | } |
56 | 67 |
|
57 | | - function createButton(text) { |
58 | | - window.wfes.f.waitForElem("wf-logo").then(elem=>{ |
59 | | - const buttonEl = document.getElementById(buttonID); |
60 | | - if (null === buttonEl) { |
61 | | - const div = document.createElement("div"); |
62 | | - div.className = "wfesTranslate"; |
63 | | - div.id = buttonID; |
64 | | - const link = document.createElement("a"); |
65 | | - link.title = "Translate nomination"; |
66 | | - link.className = "wfesTranslateButton"; |
67 | | - link.innerHTML = '<span class="material-icons">translate</span>'; |
68 | | - |
69 | | - const select = document.createElement("select"); |
70 | | - select.title = "Select translation engine"; |
71 | | - |
72 | | - for (const engineName of Object.keys(engines)) { |
73 | | - const engine = engines[engineName]; |
74 | | - const option = document.createElement("option"); |
75 | | - option.value = engine.name; |
76 | | - |
77 | | - if (engine.name === currentEngine) { |
78 | | - option.setAttribute("selected", "true"); |
79 | | - link.target = engine.target; |
80 | | - link.href = engine.url + encodeURIComponent(text); |
81 | | - } |
82 | | - option.innerText = engine.title; |
83 | | - select.appendChild(option); |
| 68 | + function onTranslateButtonClick(text) { |
| 69 | + const engine = engines[currentEngine]; |
| 70 | + let url; |
| 71 | + const target = engine.target; |
| 72 | + |
| 73 | + // Google und Deepl unterscheiden |
| 74 | + switch(currentEngine) { |
| 75 | + case "Google": |
| 76 | + url = engine.url + encodeURIComponent(text); |
| 77 | + // fenster per Link öffnen |
| 78 | + if (engine.twindow && !engine.twindow.closed) { |
| 79 | + engine.twindow.location.href = url; |
| 80 | + } else { |
| 81 | + engine.twindow = window.open(url, target); // neues Tab/Fenster |
84 | 82 | } |
| 83 | + break; |
| 84 | + case "Deepl": |
| 85 | + url = engine.url; |
| 86 | + // fenster öffnen und nachricht senden |
| 87 | + if (engine.twindow && !engine.twindow.closed) { |
| 88 | + engine.twindow.focus(); |
| 89 | + sendTextToTranslateWindow(engine.twindow, text, engine.origin); |
| 90 | + } else { |
| 91 | + window.addEventListener("message", (event) => { |
| 92 | + const dtype = engine.name + "-ready"; |
| 93 | + if (dtype === event.data?.type && engine.twindow && !engine.twindow.closed) { |
| 94 | + sendTextToTranslateWindow(engine.twindow, text, engine.origin); |
| 95 | + } |
| 96 | + }); |
| 97 | + engine.twindow = window.open(url, target); // öffne neues Tab/Fenster |
| 98 | + } |
| 99 | + break; |
| 100 | + default: |
| 101 | + console.warn("unbekannte Engine:", currentEngine, "not handeled"); |
| 102 | + } |
| 103 | + } |
85 | 104 |
|
86 | | - select.addEventListener("change", function() { |
87 | | - currentEngine = select.value; |
88 | | - window.wfes.f.localSave(storageName, currentEngine); |
89 | | - link.href = engines[currentEngine].url + encodeURIComponent(text); |
90 | | - link.target = engines[currentEngine].target; |
91 | | - }); |
92 | | - div.appendChild(select); |
93 | | - div.appendChild(link); |
94 | | - const container = elem.parentNode.parentNode; |
95 | | - container.appendChild(div); |
96 | | - } else { |
97 | | - const a = buttonEl.querySelector("a"); |
98 | | - a.href = engines[currentEngine].url + encodeURIComponent(text); |
99 | | - a.target = engines[currentEngine].url; |
| 105 | + function createButton(text) { |
| 106 | + window.wfes.f.waitForElem("wf-logo").then(elem => { |
| 107 | + // remove if exist |
| 108 | + removeButton(); |
| 109 | + const div = document.createElement("div"); |
| 110 | + div.className = "wfesTranslate"; |
| 111 | + div.id = buttonID; |
| 112 | + |
| 113 | + const select = document.createElement("select"); |
| 114 | + select.title = "Select translation engine"; |
| 115 | + |
| 116 | + for (const engineName of Object.keys(engines)) { |
| 117 | + const engine = engines[engineName]; |
| 118 | + const option = document.createElement("option"); |
| 119 | + option.value = engine.name; |
100 | 120 |
|
| 121 | + if (engine.name === currentEngine) { |
| 122 | + option.setAttribute("selected", "true"); |
| 123 | + } |
| 124 | + |
| 125 | + option.innerText = engine.title; |
| 126 | + select.appendChild(option); |
101 | 127 | } |
| 128 | + |
| 129 | + select.addEventListener("change", function() { |
| 130 | + currentEngine = select.value; |
| 131 | + window.wfes.f.localSave(storageName, currentEngine); |
| 132 | + }); |
| 133 | + |
| 134 | + const button = document.createElement("button"); |
| 135 | + button.title = "Translate nomination"; |
| 136 | + button.className = "wfesTranslateButton"; |
| 137 | + button.innerHTML = '<span class="material-icons">translate</span>'; |
| 138 | + button.addEventListener("click", function() { |
| 139 | + onTranslateButtonClick(text); |
| 140 | + }); |
| 141 | + |
| 142 | + div.appendChild(select); |
| 143 | + div.appendChild(button); |
| 144 | + |
| 145 | + const container = elem.parentNode.parentNode; |
| 146 | + container.appendChild(div); |
102 | 147 | }) |
103 | | - .catch((e) => {console.warn(GM_info.script.name, ": ", e);}); |
| 148 | + .catch((e) => { |
| 149 | + console.warn(GM_info.script.name, ": ", e); |
| 150 | + }); |
104 | 151 | } |
105 | 152 |
|
106 | 153 | function addTranslationButtonsNew() { |
|
162 | 209 | createButton(allText); |
163 | 210 | } |
164 | 211 |
|
165 | | - init(); |
166 | | - window.addEventListener("WFESReviewPageNewLoaded", addTranslationButtonsNew); |
167 | | - window.addEventListener("WFESReviewPageEditLoaded", addTranslationButtonsEdit); |
168 | | - window.addEventListener("WFESReviewPagePhotoLoaded", addTranslationButtonsPhoto); |
169 | | - window.addEventListener("WFESReviewDecisionSent", removeButton); |
| 212 | + function initWF() { |
| 213 | + window.addEventListener("WFESReviewPageNewLoaded", addTranslationButtonsNew); |
| 214 | + window.addEventListener("WFESReviewPageEditLoaded", addTranslationButtonsEdit); |
| 215 | + window.addEventListener("WFESReviewPagePhotoLoaded", addTranslationButtonsPhoto); |
| 216 | + window.addEventListener("WFESReviewDecisionSent", removeButton); |
| 217 | + |
| 218 | + window.wfes.f.addCSS(myCSSId, myStyle); |
| 219 | + window.wfes.f.localGet(storageName, "Deepl").then(e => { |
| 220 | + currentEngine = e; |
| 221 | + }); |
| 222 | + } |
| 223 | + // ----- END - the Wayfarer part ------ |
| 224 | + |
| 225 | + // common functions (can't use wfes functions in translation windows) |
| 226 | + function waitForElem(selector, maxWaitTime = 5000) { |
| 227 | + const startTime = Date.now(); |
| 228 | + return new Promise((resolve, reject) => { |
| 229 | + const checkForElement = () => { |
| 230 | + const elem = document.querySelector(selector); |
| 231 | + if (elem) { |
| 232 | + resolve(elem); |
| 233 | + } else if (Date.now() - startTime >= maxWaitTime) { |
| 234 | + reject(new Error(`Timeout waiting for element with selector ${selector} after ${maxWaitTime/1000} seconds`)); |
| 235 | + } else { |
| 236 | + setTimeout(checkForElement, 200); |
| 237 | + } |
| 238 | + }; |
| 239 | + checkForElement(); |
| 240 | + }); |
| 241 | + } |
| 242 | + |
| 243 | + // ----- BEGIN - the Deepl part ------ |
| 244 | + const deeplInputArea = 'd-textarea[name="source"] div[contenteditable="true"]'; |
| 245 | + |
| 246 | + function initD() { |
| 247 | + waitForElem(deeplInputArea) |
| 248 | + .then( () => { |
| 249 | + console.log("readyCheck -> post"); |
| 250 | + window.opener?.postMessage({ type: "Deepl-ready" }, ORIGIN_WAYFARER); |
| 251 | + }) |
| 252 | + .catch((e) => {console.warn(GM_info.script.name, ": ", e);}); |
| 253 | + |
| 254 | + function setDeepLText(text) { |
| 255 | + waitForElem(deeplInputArea) |
| 256 | + .then( elem => { |
| 257 | + elem.innerHTML = `<p>${text.replace(/\n/g, "<br>")}</p>`; |
| 258 | + // DeepL über Änderungen informieren |
| 259 | + elem.dispatchEvent(new InputEvent("input", { bubbles: true })); |
| 260 | + }) |
| 261 | + .catch((e) => {console.warn(GM_info.script.name, ": ", e);}); |
| 262 | + } |
| 263 | + |
| 264 | + window.addEventListener("message", (event) => { |
| 265 | + const msg = event.data; |
| 266 | + |
| 267 | + if ("translate" === msg?.type && "string" === typeof msg.payload) { |
| 268 | + console.log("DeepL: Text erhalten:", msg.payload); |
| 269 | + setDeepLText(msg.payload); |
| 270 | + } |
| 271 | + }); |
| 272 | + } |
| 273 | + // ----- END - the Deepl part ------ |
| 274 | + |
| 275 | + // ----- BEGIN - general instructions ------ |
| 276 | + |
| 277 | + switch(window.origin) { |
| 278 | + case ORIGIN_WAYFARER: |
| 279 | + console.log("Init Script loading:", GM_info.script.name, " - Wayfarer"); |
| 280 | + initWF(); |
| 281 | + break; |
| 282 | + case ORIGIN_DEEPL: |
| 283 | + console.log("Init Script loading:", GM_info.script.name, " - Deepl"); |
| 284 | + initD(); |
| 285 | + break; |
| 286 | + default: |
| 287 | + console.warn("unknown origin".window.origin, "not handled"); |
| 288 | + } |
170 | 289 |
|
171 | 290 | /* we are done :-) */ |
172 | 291 | console.log("Script loaded:", GM_info.script.name, "v" + GM_info.script.version); |
|
0 commit comments