/* * Copyright (C) 2016-2017 Alex Yatskov * Author: Alex Yatskov * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ async function getOptionsArray() { const optionsFull = await apiOptionsGetFull(); return optionsFull.profiles.map(profile => profile.options); } async function formRead(options) { options.general.enable = $('#enable').prop('checked'); options.general.showGuide = $('#show-usage-guide').prop('checked'); options.general.compactTags = $('#compact-tags').prop('checked'); options.general.compactGlossaries = $('#compact-glossaries').prop('checked'); options.general.resultOutputMode = $('#result-output-mode').val(); options.general.debugInfo = $('#show-debug-info').prop('checked'); options.general.showAdvanced = $('#show-advanced-options').prop('checked'); options.general.maxResults = parseInt($('#max-displayed-results').val(), 10); options.general.popupDisplayMode = $('#popup-display-mode').val(); options.general.popupHorizontalTextPosition = $('#popup-horizontal-text-position').val(); options.general.popupVerticalTextPosition = $('#popup-vertical-text-position').val(); options.general.popupWidth = parseInt($('#popup-width').val(), 10); options.general.popupHeight = parseInt($('#popup-height').val(), 10); options.general.popupHorizontalOffset = parseInt($('#popup-horizontal-offset').val(), 0); options.general.popupVerticalOffset = parseInt($('#popup-vertical-offset').val(), 10); options.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0); options.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10); options.general.popupTheme = $('#popup-theme').val(); options.general.popupOuterTheme = $('#popup-outer-theme').val(); options.general.customPopupCss = $('#custom-popup-css').val(); options.general.customPopupOuterCss = $('#custom-popup-outer-css').val(); options.audio.enabled = $('#audio-playback-enabled').prop('checked'); options.audio.autoPlay = $('#auto-play-audio').prop('checked'); options.audio.volume = parseFloat($('#audio-playback-volume').val()); options.audio.customSourceUrl = $('#audio-custom-source').val(); options.audio.textToSpeechVoice = $('#text-to-speech-voice').val(); options.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked'); options.scanning.touchInputEnabled = $('#touch-input-enabled').prop('checked'); options.scanning.selectText = $('#select-matched-text').prop('checked'); options.scanning.alphanumeric = $('#search-alphanumeric').prop('checked'); options.scanning.autoHideResults = $('#auto-hide-results').prop('checked'); options.scanning.deepDomScan = $('#deep-dom-scan').prop('checked'); options.scanning.enablePopupSearch = $('#enable-search-within-first-popup').prop('checked'); options.scanning.enableOnPopupExpressions = $('#enable-scanning-of-popup-expressions').prop('checked'); options.scanning.enableOnSearchPage = $('#enable-scanning-on-search-page').prop('checked'); options.scanning.delay = parseInt($('#scan-delay').val(), 10); options.scanning.length = parseInt($('#scan-length').val(), 10); options.scanning.modifier = $('#scan-modifier-key').val(); options.scanning.popupNestingMaxDepth = parseInt($('#popup-nesting-max-depth').val(), 10); const optionsAnkiEnableOld = options.anki.enable; options.anki.enable = $('#anki-enable').prop('checked'); options.anki.tags = utilBackgroundIsolate($('#card-tags').val().split(/[,; ]+/)); options.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10); options.anki.server = $('#interface-server').val(); options.anki.screenshot.format = $('#screenshot-format').val(); options.anki.screenshot.quality = parseInt($('#screenshot-quality').val(), 10); options.anki.fieldTemplates = $('#field-templates').val(); if (optionsAnkiEnableOld && !ankiErrorShown()) { options.anki.terms.deck = $('#anki-terms-deck').val(); options.anki.terms.model = $('#anki-terms-model').val(); options.anki.terms.fields = utilBackgroundIsolate(ankiFieldsToDict($('#terms .anki-field-value'))); options.anki.kanji.deck = $('#anki-kanji-deck').val(); options.anki.kanji.model = $('#anki-kanji-model').val(); options.anki.kanji.fields = utilBackgroundIsolate(ankiFieldsToDict($('#kanji .anki-field-value'))); } options.general.mainDictionary = $('#dict-main').val(); $('.dict-group').each((index, element) => { const dictionary = $(element); options.dictionaries[dictionary.data('title')] = utilBackgroundIsolate({ priority: parseInt(dictionary.find('.dict-priority').val(), 10), enabled: dictionary.find('.dict-enabled').prop('checked'), allowSecondarySearches: dictionary.find('.dict-allow-secondary-searches').prop('checked') }); }); } async function formWrite(options) { $('#enable').prop('checked', options.general.enable); $('#show-usage-guide').prop('checked', options.general.showGuide); $('#compact-tags').prop('checked', options.general.compactTags); $('#compact-glossaries').prop('checked', options.general.compactGlossaries); $('#result-output-mode').val(options.general.resultOutputMode); $('#show-debug-info').prop('checked', options.general.debugInfo); $('#show-advanced-options').prop('checked', options.general.showAdvanced); $('#max-displayed-results').val(options.general.maxResults); $('#popup-display-mode').val(options.general.popupDisplayMode); $('#popup-horizontal-text-position').val(options.general.popupHorizontalTextPosition); $('#popup-vertical-text-position').val(options.general.popupVerticalTextPosition); $('#popup-width').val(options.general.popupWidth); $('#popup-height').val(options.general.popupHeight); $('#popup-horizontal-offset').val(options.general.popupHorizontalOffset); $('#popup-vertical-offset').val(options.general.popupVerticalOffset); $('#popup-horizontal-offset2').val(options.general.popupHorizontalOffset2); $('#popup-vertical-offset2').val(options.general.popupVerticalOffset2); $('#popup-theme').val(options.general.popupTheme); $('#popup-outer-theme').val(options.general.popupOuterTheme); $('#custom-popup-css').val(options.general.customPopupCss); $('#custom-popup-outer-css').val(options.general.customPopupOuterCss); $('#audio-playback-enabled').prop('checked', options.audio.enabled); $('#auto-play-audio').prop('checked', options.audio.autoPlay); $('#audio-playback-volume').val(options.audio.volume); $('#audio-custom-source').val(options.audio.customSourceUrl); $('#text-to-speech-voice').val(options.audio.textToSpeechVoice).attr('data-value', options.audio.textToSpeechVoice); $('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse); $('#touch-input-enabled').prop('checked', options.scanning.touchInputEnabled); $('#select-matched-text').prop('checked', options.scanning.selectText); $('#search-alphanumeric').prop('checked', options.scanning.alphanumeric); $('#auto-hide-results').prop('checked', options.scanning.autoHideResults); $('#deep-dom-scan').prop('checked', options.scanning.deepDomScan); $('#enable-search-within-first-popup').prop('checked', options.scanning.enablePopupSearch); $('#enable-scanning-of-popup-expressions').prop('checked', options.scanning.enableOnPopupExpressions); $('#enable-scanning-on-search-page').prop('checked', options.scanning.enableOnSearchPage); $('#scan-delay').val(options.scanning.delay); $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); $('#popup-nesting-max-depth').val(options.scanning.popupNestingMaxDepth); $('#anki-enable').prop('checked', options.anki.enable); $('#card-tags').val(options.anki.tags.join(' ')); $('#sentence-detection-extent').val(options.anki.sentenceExt); $('#interface-server').val(options.anki.server); $('#screenshot-format').val(options.anki.screenshot.format); $('#screenshot-quality').val(options.anki.screenshot.quality); $('#field-templates').val(options.anki.fieldTemplates); try { await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); } catch (e) { dictionaryErrorsShow([e]); } try { await ankiDeckAndModelPopulate(options); } catch (e) { ankiErrorShow(e); } formUpdateVisibility(options); } function formSetupEventListeners() { $('#dict-purge-link').click(utilAsync(onDictionaryPurge)); $('#dict-file').change(utilAsync(onDictionaryImport)); $('#dict-file-button').click(onDictionaryImportButtonClick); $('#field-templates-reset').click(utilAsync(onAnkiFieldTemplatesReset)); $('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged)); $('.anki-model').change(utilAsync(onAnkiModelChanged)); } function formUpdateVisibility(options) { const general = $('#anki-general'); if (options.anki.enable) { general.show(); } else { general.hide(); } const advanced = $('.options-advanced'); if (options.general.showAdvanced) { advanced.show(); } else { advanced.hide(); } const mainGroup = $('#dict-main-group'); if (options.general.resultOutputMode === 'merge') { mainGroup.show(); } else { mainGroup.hide(); } const debug = $('#debug'); if (options.general.debugInfo) { const temp = utilIsolate(options); temp.anki.fieldTemplates = '...'; const text = JSON.stringify(temp, null, 4); debug.html(handlebarsEscape(text)); debug.show(); } else { debug.hide(); } } async function formMainDictionaryOptionsPopulate(options) { const select = $('#dict-main').empty(); select.append($('')); let mainDictionary = ''; for (const dictRow of toIterable(await utilDatabaseSummarize())) { if (dictRow.sequenced) { select.append($(``)); if (dictRow.title === options.general.mainDictionary) { mainDictionary = dictRow.title; } } } select.val(mainDictionary); } async function onFormOptionsChanged(e) { if (!e.originalEvent && !e.isTrigger) { return; } const optionsContext = getOptionsContext(); const options = await apiOptionsGet(optionsContext); const optionsAnkiEnableOld = options.anki.enable; const optionsAnkiServerOld = options.anki.server; await formRead(options); await settingsSaveOptions(); formUpdateVisibility(options); try { const ankiUpdated = options.anki.enable !== optionsAnkiEnableOld || options.anki.server !== optionsAnkiServerOld; if (ankiUpdated) { ankiSpinnerShow(true); await ankiDeckAndModelPopulate(options); ankiErrorShow(); } } catch (e) { ankiErrorShow(e); } finally { ankiSpinnerShow(false); } } async function onReady() { showExtensionInformation(); formSetupEventListeners(); appearanceInitialize(); await audioSettingsInitialize(); await profileOptionsSetup(); storageInfoInitialize(); chrome.runtime.onMessage.addListener(onMessage); } $(document).ready(utilAsync(onReady)); /* * Appearance */ function appearanceInitialize() { let previewVisible = false; $('#settings-popup-preview-button').on('click', () => { if (previewVisible) { return; } showAppearancePreview(); previewVisible = true; }); } function showAppearancePreview() { const container = $('#settings-popup-preview-container'); const buttonContainer = $('#settings-popup-preview-button-container'); const settings = $('#settings-popup-preview-settings'); const text = $('#settings-popup-preview-text'); const customCss = $('#custom-popup-css'); const customOuterCss = $('#custom-popup-outer-css'); const frame = document.createElement('iframe'); frame.src = '/bg/settings-popup-preview.html'; frame.id = 'settings-popup-preview-frame'; window.wanakana.bind(text[0]); text.on('input', () => { const action = 'setText'; const params = {text: text.val()}; frame.contentWindow.postMessage({action, params}, '*'); }); customCss.on('input', () => { const action = 'setCustomCss'; const params = {css: customCss.val()}; frame.contentWindow.postMessage({action, params}, '*'); }); customOuterCss.on('input', () => { const action = 'setCustomOuterCss'; const params = {css: customOuterCss.val()}; frame.contentWindow.postMessage({action, params}, '*'); }); container.append(frame); buttonContainer.remove(); settings.css('display', ''); } /* * Audio */ let audioSourceUI = null; async function audioSettingsInitialize() { const optionsContext = getOptionsContext(); const options = await apiOptionsGet(optionsContext); audioSourceUI = new AudioSourceUI.Container(options.audio.sources, $('.audio-source-list'), $('.audio-source-add')); audioSourceUI.save = () => apiOptionsSave(); textToSpeechInitialize(); } function textToSpeechInitialize() { if (typeof speechSynthesis === 'undefined') { return; } speechSynthesis.addEventListener('voiceschanged', () => updateTextToSpeechVoices(), false); updateTextToSpeechVoices(); $('#text-to-speech-voice-test').on('click', () => textToSpeechTest()); } function updateTextToSpeechVoices() { const voices = Array.prototype.map.call(speechSynthesis.getVoices(), (voice, index) => ({voice, index})); voices.sort(textToSpeechVoiceCompare); if (voices.length > 0) { $('#text-to-speech-voice-container').css('display', ''); } const select = $('#text-to-speech-voice'); select.empty(); select.append($('