Fix several bugs
- Fix: when installing the extension, the default language was added
even if it didn't exist in isoLangs.
- Also, the default language now considers 'zh-CN' and 'zh-TW' as
possible candidates.
- Fix: minor CSS issue in the options page.
- Unknown languages are deleted (or replaced with a known replacement)
when updating the extension.
- Other minor changes.
Change-Id: Id6fc8233255cef2fc67185e1114ca86fdc56bc5a
diff --git a/src/background.js b/src/background.js
index b7857c8..8fed6f6 100644
--- a/src/background.js
+++ b/src/background.js
@@ -4,6 +4,13 @@
return Object.keys(obj).length === 0;
}
+function inObject(hayStack, el) {
+ for (var i of Object.keys(hayStack)) {
+ if (hayStack[i] == el) return true;
+ }
+ return false;
+}
+
function getTranslationUrl(lang, text) {
var params = new URLSearchParams({
sl: 'auto',
@@ -15,7 +22,6 @@
}
function translationClick(info, tab) {
- console.log(info.selectionText);
chrome.storage.sync.get('uniquetab', items => {
var url = getTranslationUrl(
array_elements[info.menuItemId]['langCode'], info.selectionText);
@@ -102,6 +108,10 @@
for (var language_id of Object.keys(items.translateinto)) {
var language = items.translateinto[language_id];
var languagem = isoLangs[language];
+ if (languagem === undefined) {
+ console.error(language + ' doesn\'t exist!');
+ continue;
+ }
var id = chrome.contextMenus.create({
'id': 'tr_single_parent',
'title': chrome.i18n.getMessage('contextmenu_title2', languagem.name),
@@ -119,6 +129,10 @@
for (var language_id of Object.keys(items.translateinto)) {
var language = items.translateinto[language_id];
var languagem = isoLangs[language];
+ if (languagem === undefined) {
+ console.error(language + ' doesn\'t exist!');
+ continue;
+ }
var title = languagem.name + ' (' + languagem.nativeName + ')';
var id = chrome.contextMenus.create({
'id': 'tr_language_' + language,
@@ -150,12 +164,15 @@
if (details.reason == 'install') {
if (isEmpty(items)) {
var settings = {'translateinto': {}, 'uniquetab': ''},
- default_language =
+ default_language_1 =
+ chrome.i18n.getMessage('@@ui_locale').replace('_', '-'),
+ default_language_2 =
chrome.i18n.getMessage('@@ui_locale').split('_')[0];
- if (isoLangs[default_language] != 'undefined') {
- settings.translateinto[default_language] = default_language;
- }
+ if (isoLangs[default_language_1] != undefined)
+ settings.translateinto['0'] = default_language_1;
+ else if (isoLangs[default_language_2] != undefined)
+ settings.translateinto['0'] = default_language_2;
chrome.storage.sync.set(settings, function() {
chrome.notifications.create('install', {
@@ -180,7 +197,7 @@
var default_language =
chrome.i18n.getMessage('@@ui_locale').split('_')[0];
- if (isoLangs[default_language] != 'undefined')
+ if (isoLangs[default_language] != undefined)
settings.languages[default_language] = default_language;
chrome.storage.sync.set(settings, function() {
@@ -215,6 +232,40 @@
});
});
}
+
+ // Remove non-existent languages or change with correct language code
+ if (items.translateinto) {
+ var modified = false;
+ for (var language_id of Object.keys(items.translateinto)) {
+ var language = items.translateinto[language_id];
+ if (isoLangs[language] === undefined) {
+ if (convertLanguages[language] === undefined) {
+ // The language doesn't exist
+ console.log(
+ 'Deleting ' + language +
+ ' from items.translateinto because it doesn\'t exist.');
+ delete items.translateinto[language_id];
+ } else {
+ // The language doesn't exist but a known replacement is known
+ var newLanguage = convertLanguages[language];
+ console.log('Replacing ' + language + ' with ' + newLanguage);
+
+ // If the converted language is already on the list, just remove
+ // the wrong language, otherwise convert the language
+ if (inObject(items.translateinto, newLanguage))
+ delete items.translateinto[language_id];
+ else
+ items.translateinto[language_id] = newLanguage;
+ }
+ modified = true;
+ }
+ }
+ if (modified) chrome.storage.sync.set(items);
+ } else {
+ console.log('items.translateinto doesn\'t exist: let\'s create it.');
+ items['translateinto'] = {};
+ chrome.storage.sync.set(items);
+ }
}
});
});
diff --git a/src/css/options.css b/src/css/options.css
index 9aa7b75..7190881 100644
--- a/src/css/options.css
+++ b/src/css/options.css
@@ -22,7 +22,7 @@
#languages_container {
width: 300px;
- height: 365px;
+ height: 250px;
border: 1px solid #ccc;
background-color: #E3F2FD;
overflow: auto;
@@ -185,7 +185,7 @@
font-weight: bold;
}
-dialog .action_buttons {
+dialog#credits_dialog .action_buttons {
border-top: 1px solid #ccc;
padding: 1em;
text-align: right;
diff --git a/src/js/common.js b/src/js/common.js
index 31914e8..fa8445c 100644
--- a/src/js/common.js
+++ b/src/js/common.js
@@ -107,5 +107,12 @@
'yo': {'name': 'Yoruba', 'nativeName': 'Yorùbá'},
'zh-CN': {'name': 'Chinese (Simplified)', 'nativeName': '汉语'},
'zh-TW': {'name': 'Chinese (Traditional)', 'nativeName': '漢語'},
- 'zu': {'name': 'Zulu', 'nativeName': 'isiZulu'}
+ 'zu': {'name': 'Zulu', 'nativeName': 'isiZulu'},
+};
+
+// Some languages were incorrectly set. This map serves as a conversion between
+// the previous wrong languages and the correct code.
+var convertLanguages = {
+ 'jv': 'jw',
+ 'zh': 'zh-CN',
};