diff options
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r-- | ext/bg/js/backend.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index c7131728..690f6a3c 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -1655,9 +1655,11 @@ class Backend { convertAlphabeticCharacters, convertHiraganaToKatakana, convertKatakanaToHiragana, - collapseEmphaticSequences + collapseEmphaticSequences, + textReplacements: textReplacementsOptions } } = options; + const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions); return { wildcard, mainDictionary, @@ -1668,6 +1670,7 @@ class Backend { convertHiraganaToKatakana, convertKatakanaToHiragana, collapseEmphaticSequences, + textReplacements, enabledDictionaryMap }; } @@ -1686,6 +1689,29 @@ class Backend { return enabledDictionaryMap; } + _getTranslatorTextReplacements(textReplacementsOptions) { + const textReplacements = []; + for (const group of textReplacementsOptions.groups) { + const textReplacementsEntries = []; + for (let {pattern, ignoreCase, replacement} of group) { + try { + pattern = new RegExp(pattern, ignoreCase ? 'gi' : 'g'); + } catch (e) { + // Invalid pattern + continue; + } + textReplacementsEntries.push({pattern, replacement}); + } + if (textReplacementsEntries.length > 0) { + textReplacements.push(textReplacementsEntries); + } + } + if (textReplacements.length === 0 || textReplacementsOptions.searchOriginal) { + textReplacements.unshift(null); + } + return textReplacements; + } + async _openWelcomeGuidePage() { await this._createTab(chrome.runtime.getURL('/bg/welcome.html')); } |