Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
Sends selected text to the translation popup via context menu (#615)
Browse files Browse the repository at this point in the history
* Sends selected text to the translation popup via context menu

* Clear preload text and use localization

* Adapt for pontoon

* More pontoon
  • Loading branch information
andrenatal committed Dec 21, 2022
1 parent 467878f commit af29a6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
24 changes: 20 additions & 4 deletions extension/controller/backgroundScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -473,8 +474,10 @@ const messageListener = function(message, sender) {
}
browser.runtime.sendMessage({
command: "responseLocalizedLanguages",
localizedLanguages: mapLangs
});
localizedLanguages: mapLangs,
popupPreLoadText
});
popupPreLoadText = "";
break;
default:
// ignore
Expand Down Expand Up @@ -851,4 +854,17 @@ const getItemFromWeb = async (tabId, itemURL, fileSize, fileChecksum) => {
return null;
}
return fetchResponse;
};
};

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();
}
});
4 changes: 3 additions & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion extension/view/popup/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const translateCall = () => {
}
});


document.querySelector("#input").value = message.popupPreLoadText;
document.getElementById("status").innerText = browser.i18n.getMessage("translationBarTranslateButton");
}
}
Expand Down

0 comments on commit af29a6c

Please sign in to comment.