From af29a6ca83c4e98566d510c0f150667a43e506d2 Mon Sep 17 00:00:00 2001 From: Andre Natal Date: Wed, 21 Dec 2022 15:57:27 -0800 Subject: [PATCH] Sends selected text to the translation popup via context menu (#615) * Sends selected text to the translation popup via context menu * Clear preload text and use localization * Adapt for pontoon * More pontoon --- extension/controller/backgroundScript.js | 24 ++++++++++++++++++++---- extension/manifest.json | 4 +++- extension/view/popup/js/index.js | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/extension/controller/backgroundScript.js b/extension/controller/backgroundScript.js index 9b8669f6..ac2fd364 100644 --- a/extension/controller/backgroundScript.js +++ b/extension/controller/backgroundScript.js @@ -2,7 +2,7 @@ /* eslint-disable max-lines */ /* global LanguageDetection, browser, PingSender, BERGAMOT_VERSION_FULL, Telemetry, loadFastText, FastText, Sentry, settings, deserializeError, -modelRegistryRootURL, modelRegistryRootURLTest, modelRegistry */ +modelRegistryRootURL, modelRegistryRootURLTest, modelRegistry, DOMPurify */ /* * we need the background script in order to have full access to the @@ -76,6 +76,7 @@ let isMochitest = false; const languageModelFileTypes = ["model", "lex", "vocab", "qualityModel", "srcvocab", "trgvocab"]; const CACHE_NAME = "fxtranslations"; const FT_SCORE_THRESHOLD = 0.75; +let popupPreLoadText = null; const init = () => { Sentry.wrap(async () => { @@ -473,8 +474,10 @@ const messageListener = function(message, sender) { } browser.runtime.sendMessage({ command: "responseLocalizedLanguages", - localizedLanguages: mapLangs - }); + localizedLanguages: mapLangs, + popupPreLoadText + }); + popupPreLoadText = ""; break; default: // ignore @@ -851,4 +854,17 @@ const getItemFromWeb = async (tabId, itemURL, fileSize, fileChecksum) => { return null; } return fetchResponse; -}; \ No newline at end of file +}; + +browser.contextMenus.create({ + id: "firefox-translations", + title: browser.i18n.getMessage("translateWith", "Firefox Translations"), + contexts: ["selection"], +}); + +browser.contextMenus.onClicked.addListener(info => { + if (info.menuItemId === "firefox-translations") { + popupPreLoadText = DOMPurify.sanitize(info.selectionText, { USE_PROFILES: { html: true } }); + browser.browserAction.openPopup(); + } +}); \ No newline at end of file diff --git a/extension/manifest.json b/extension/manifest.json index e1fba145..105bda57 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -25,11 +25,13 @@ "tabs", "webNavigation", "storage", - "mozillaAddons" + "mozillaAddons", + "contextMenus" ], "background": { "persistent": true, "scripts": [ + "model/static/DOMPurify/purify.min.js", "model/static/errorReporting/serializeError.js", "model/modelRegistry.js", "model/static/errorReporting/sentry.js", diff --git a/extension/view/popup/js/index.js b/extension/view/popup/js/index.js index cfa30ee5..1529cc8f 100644 --- a/extension/view/popup/js/index.js +++ b/extension/view/popup/js/index.js @@ -140,7 +140,7 @@ const translateCall = () => { } }); - + document.querySelector("#input").value = message.popupPreLoadText; document.getElementById("status").innerText = browser.i18n.getMessage("translationBarTranslateButton"); } }