Skip to content

Commit 6beb8d8

Browse files
committed
Add all functional in one commit =)
- default language in options - contextMenu item
1 parent 120671c commit 6beb8d8

File tree

7 files changed

+130
-1
lines changed

7 files changed

+130
-1
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 translate-by-extension
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# Translate by extension for Firefox
1+
# To DeepL
2+
3+
Firefox extension. Highlight text on a web page and send it to DeepL.
4+
5+
This extension is inspired by recommended extension "To Google Translate"

src/background.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const deeplURL = "https://www.deepl.com/translator#en/"
2+
let defaultLang = "en"
3+
4+
function getDefaultLang() {
5+
browser.storage.local.get('settings').then((store) => {
6+
if (store.settings && store.settings.defaultLang) {
7+
defaultLang = store.settings.defaultLang
8+
}
9+
});
10+
}
11+
12+
browser.contextMenus.create({
13+
id: "translate-text",
14+
title: "To DeepL",
15+
contexts: ["selection"]
16+
});
17+
18+
browser.contextMenus.onClicked.addListener((info, tab) => {
19+
switch (info.menuItemId) {
20+
case 'translate-text':
21+
const translateURL = `${deeplURL + defaultLang}/${encodeURI(info.selectionText)}`;
22+
browser.tabs.create({ url: translateURL });
23+
break;
24+
}
25+
});
26+
27+
getDefaultLang();
28+
29+
browser.storage.onChanged.addListener(getDefaultLang);

src/icons/icon.svg

+3
Loading

src/manifest.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"manifest_version": 2,
3+
"name": "To DeepL",
4+
"version": "0.1.0",
5+
"description": "Translate selected text in new tab with DeepL",
6+
"homepage_url": "https://github.com/rewkha/translate-by-extension",
7+
"icons": {
8+
"48": "icons/icon.svg"
9+
},
10+
"permissions": ["contextMenus", "storage"],
11+
"background": {
12+
"scripts": ["background.js"]
13+
},
14+
"options_ui": {
15+
"browser_style": true,
16+
"page": "options/options.html",
17+
"open_in_tab": false
18+
}
19+
}

src/options/options.html

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<style>
6+
#options-content {
7+
padding: 10px;
8+
}
9+
</style>
10+
</head>
11+
<body>
12+
<div id="options-content">
13+
<label for="default_lang">
14+
<span>Default language:</span>
15+
<select class="browser-style" name="default_lang" id="defaultLang">
16+
<option value="en" selected="">English</option>
17+
<option value="de">Deutsch</option>
18+
<option value="fr">Français</option>
19+
<option value="es">Español</option>
20+
<option value="ja">日本語 (Japanese)</option>
21+
<option value="pt-PT">Português</option>
22+
<option value="pt-BR">Português (Brasil)</option>
23+
<option value="it">Italiano</option>
24+
<option value="nl">Nederlands</option>
25+
<option value="pl">Polski</option>
26+
<option value="ru">Русский (Russian)</option>
27+
<option value="zh">简体中文 (simplified Chinese)</option>
28+
</select>
29+
</label>
30+
</div>
31+
<script type="module" src="options.js"></script>
32+
</body>
33+
34+
</html>

src/options/options.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function renderOptions() {
2+
return browser.storage.local.get('settings').then((store) => {
3+
const defaultLang = (store.settings && store.settings.defaultLang) ? store.settings.defaultLang : 'en';
4+
document.getElementById('defaultLang').value = defaultLang;
5+
});
6+
}
7+
8+
document.getElementById('defaultLang').addEventListener('click', (e) => {
9+
console.log(e.target.value);
10+
if (['en', 'de', 'fr', 'es', 'ja', 'pt-PT', 'pt-BR', 'it', 'nl', 'pl', 'ru', 'zh'].indexOf(e.target.value) !== -1) {
11+
browser.storage.local.set({
12+
settings: {
13+
defaultLang: e.target.value
14+
}
15+
});
16+
}
17+
});
18+
19+
renderOptions();

0 commit comments

Comments
 (0)