2016-04-04 03:05:22 +00:00
|
|
|
/*
|
2020-01-01 17:00:00 +00:00
|
|
|
* Copyright (C) 2016-2020 Alex Yatskov <alex@foosoft.net>
|
2016-04-04 03:05:22 +00:00
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-04-04 03:05:22 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-12 02:11:07 +00:00
|
|
|
function getOptionsMutable(optionsContext) {
|
2019-12-14 04:23:15 +00:00
|
|
|
return utilBackend().getOptions(
|
|
|
|
utilBackgroundIsolate(optionsContext)
|
|
|
|
);
|
2019-12-12 02:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionsFullMutable() {
|
|
|
|
return utilBackend().getFullOptions();
|
|
|
|
}
|
|
|
|
|
2019-09-07 19:54:00 +00:00
|
|
|
async function formRead(options) {
|
2019-09-11 00:49:17 +00:00
|
|
|
options.general.enable = $('#enable').prop('checked');
|
2019-09-07 19:54:00 +00:00
|
|
|
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);
|
2019-12-23 22:39:00 +00:00
|
|
|
options.general.popupScalingFactor = parseInt($('#popup-scaling-factor').val(), 10);
|
2019-12-30 17:42:12 +00:00
|
|
|
options.general.popupScaleRelativeToPageZoom = $('#popup-scale-relative-to-page-zoom').prop('checked');
|
|
|
|
options.general.popupScaleRelativeToVisualViewport = $('#popup-scale-relative-to-visual-viewport').prop('checked');
|
2019-10-12 17:48:23 +00:00
|
|
|
options.general.popupTheme = $('#popup-theme').val();
|
2019-10-12 21:59:56 +00:00
|
|
|
options.general.popupOuterTheme = $('#popup-outer-theme').val();
|
2019-09-07 19:54:00 +00:00
|
|
|
options.general.customPopupCss = $('#custom-popup-css').val();
|
2019-10-13 15:05:21 +00:00
|
|
|
options.general.customPopupOuterCss = $('#custom-popup-outer-css').val();
|
2019-09-07 19:54:00 +00:00
|
|
|
|
2019-10-10 02:33:35 +00:00
|
|
|
options.audio.enabled = $('#audio-playback-enabled').prop('checked');
|
|
|
|
options.audio.autoPlay = $('#auto-play-audio').prop('checked');
|
|
|
|
options.audio.volume = parseFloat($('#audio-playback-volume').val());
|
2019-10-11 00:26:31 +00:00
|
|
|
options.audio.customSourceUrl = $('#audio-custom-source').val();
|
2019-10-13 02:50:22 +00:00
|
|
|
options.audio.textToSpeechVoice = $('#text-to-speech-voice').val();
|
2019-10-10 02:33:35 +00:00
|
|
|
|
2019-09-07 19:54:00 +00:00
|
|
|
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');
|
2019-09-27 04:33:33 +00:00
|
|
|
options.scanning.enablePopupSearch = $('#enable-search-within-first-popup').prop('checked');
|
2019-09-07 19:54:00 +00:00
|
|
|
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);
|
|
|
|
|
2019-12-22 18:50:30 +00:00
|
|
|
options.translation.convertKatakanaToHiragana = $('#translation-convert-katakana-to-hiragana').val();
|
|
|
|
options.translation.convertHalfWidthCharacters = $('#translation-convert-half-width-characters').val();
|
|
|
|
options.translation.convertNumericCharacters = $('#translation-convert-numeric-characters').val();
|
|
|
|
options.translation.convertAlphabeticCharacters = $('#translation-convert-alphabetic-characters').val();
|
|
|
|
|
2019-11-05 13:56:45 +00:00
|
|
|
options.parsing.enableScanningParser = $('#parsing-scan-enable').prop('checked');
|
|
|
|
options.parsing.enableMecabParser = $('#parsing-mecab-enable').prop('checked');
|
2019-11-13 18:24:11 +00:00
|
|
|
options.parsing.readingMode = $('#parsing-reading-mode').val();
|
2019-11-05 13:56:45 +00:00
|
|
|
|
2019-09-07 19:54:00 +00:00
|
|
|
const optionsAnkiEnableOld = options.anki.enable;
|
|
|
|
options.anki.enable = $('#anki-enable').prop('checked');
|
2019-09-14 20:21:41 +00:00
|
|
|
options.anki.tags = utilBackgroundIsolate($('#card-tags').val().split(/[,; ]+/));
|
2019-09-07 19:54:00 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (optionsAnkiEnableOld && !ankiErrorShown()) {
|
|
|
|
options.anki.terms.deck = $('#anki-terms-deck').val();
|
|
|
|
options.anki.terms.model = $('#anki-terms-model').val();
|
2019-12-03 03:17:45 +00:00
|
|
|
options.anki.terms.fields = utilBackgroundIsolate(ankiFieldsToDict(document.querySelectorAll('#terms .anki-field-value')));
|
2019-09-07 19:54:00 +00:00
|
|
|
options.anki.kanji.deck = $('#anki-kanji-deck').val();
|
|
|
|
options.anki.kanji.model = $('#anki-kanji-model').val();
|
2019-12-03 03:17:45 +00:00
|
|
|
options.anki.kanji.fields = utilBackgroundIsolate(ankiFieldsToDict(document.querySelectorAll('#kanji .anki-field-value')));
|
2017-07-17 02:29:43 +00:00
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 15:23:04 +00:00
|
|
|
async function formWrite(options) {
|
2019-09-11 00:49:17 +00:00
|
|
|
$('#enable').prop('checked', options.general.enable);
|
2019-09-08 15:23:04 +00:00
|
|
|
$('#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);
|
2019-12-23 22:39:00 +00:00
|
|
|
$('#popup-scaling-factor').val(options.general.popupScalingFactor);
|
2019-12-30 17:42:12 +00:00
|
|
|
$('#popup-scale-relative-to-page-zoom').prop('checked', options.general.popupScaleRelativeToPageZoom);
|
|
|
|
$('#popup-scale-relative-to-visual-viewport').prop('checked', options.general.popupScaleRelativeToVisualViewport);
|
2019-10-12 17:48:23 +00:00
|
|
|
$('#popup-theme').val(options.general.popupTheme);
|
2019-10-12 21:59:56 +00:00
|
|
|
$('#popup-outer-theme').val(options.general.popupOuterTheme);
|
2019-09-08 15:23:04 +00:00
|
|
|
$('#custom-popup-css').val(options.general.customPopupCss);
|
2019-10-13 15:05:21 +00:00
|
|
|
$('#custom-popup-outer-css').val(options.general.customPopupOuterCss);
|
2019-09-08 15:23:04 +00:00
|
|
|
|
2019-10-10 02:33:35 +00:00
|
|
|
$('#audio-playback-enabled').prop('checked', options.audio.enabled);
|
|
|
|
$('#auto-play-audio').prop('checked', options.audio.autoPlay);
|
|
|
|
$('#audio-playback-volume').val(options.audio.volume);
|
2019-10-11 00:26:31 +00:00
|
|
|
$('#audio-custom-source').val(options.audio.customSourceUrl);
|
2019-10-13 02:50:22 +00:00
|
|
|
$('#text-to-speech-voice').val(options.audio.textToSpeechVoice).attr('data-value', options.audio.textToSpeechVoice);
|
2019-10-10 02:33:35 +00:00
|
|
|
|
2019-09-08 15:23:04 +00:00
|
|
|
$('#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);
|
2019-09-27 04:33:33 +00:00
|
|
|
$('#enable-search-within-first-popup').prop('checked', options.scanning.enablePopupSearch);
|
2019-09-08 15:23:04 +00:00
|
|
|
$('#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);
|
|
|
|
|
2019-12-22 18:50:30 +00:00
|
|
|
$('#translation-convert-katakana-to-hiragana').val(options.translation.convertKatakanaToHiragana);
|
|
|
|
$('#translation-convert-half-width-characters').val(options.translation.convertHalfWidthCharacters);
|
|
|
|
$('#translation-convert-numeric-characters').val(options.translation.convertNumericCharacters);
|
|
|
|
$('#translation-convert-alphabetic-characters').val(options.translation.convertAlphabeticCharacters);
|
|
|
|
|
2019-11-05 13:56:45 +00:00
|
|
|
$('#parsing-scan-enable').prop('checked', options.parsing.enableScanningParser);
|
|
|
|
$('#parsing-mecab-enable').prop('checked', options.parsing.enableMecabParser);
|
2019-11-13 18:24:11 +00:00
|
|
|
$('#parsing-reading-mode').val(options.parsing.readingMode);
|
2019-11-05 13:56:45 +00:00
|
|
|
|
2019-09-08 15:23:04 +00:00
|
|
|
$('#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);
|
2019-12-14 21:59:44 +00:00
|
|
|
|
2019-12-15 05:02:52 +00:00
|
|
|
await ankiTemplatesUpdateValue();
|
2019-12-02 03:16:58 +00:00
|
|
|
await onAnkiOptionsChanged(options);
|
2019-11-13 01:13:25 +00:00
|
|
|
await onDictionaryOptionsChanged(options);
|
2019-11-09 21:34:39 +00:00
|
|
|
|
2019-09-08 15:23:04 +00:00
|
|
|
formUpdateVisibility(options);
|
|
|
|
}
|
|
|
|
|
2019-09-08 16:16:12 +00:00
|
|
|
function formSetupEventListeners() {
|
2019-11-26 20:07:11 +00:00
|
|
|
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change((e) => onFormOptionsChanged(e));
|
2019-09-08 16:16:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 02:55:33 +00:00
|
|
|
function formUpdateVisibility(options) {
|
2019-10-29 20:31:27 +00:00
|
|
|
document.documentElement.dataset.optionsAnkiEnable = `${!!options.anki.enable}`;
|
|
|
|
document.documentElement.dataset.optionsGeneralDebugInfo = `${!!options.general.debugInfo}`;
|
|
|
|
document.documentElement.dataset.optionsGeneralShowAdvanced = `${!!options.general.showAdvanced}`;
|
|
|
|
document.documentElement.dataset.optionsGeneralResultOutputMode = `${options.general.resultOutputMode}`;
|
2017-10-24 13:23:13 +00:00
|
|
|
|
2017-05-25 02:13:56 +00:00
|
|
|
if (options.general.debugInfo) {
|
2017-09-13 03:20:03 +00:00
|
|
|
const temp = utilIsolate(options);
|
2019-12-14 21:59:44 +00:00
|
|
|
if (typeof temp.anki.fieldTemplates === 'string') {
|
|
|
|
temp.anki.fieldTemplates = '...';
|
|
|
|
}
|
2017-09-13 03:20:03 +00:00
|
|
|
const text = JSON.stringify(temp, null, 4);
|
2019-10-29 20:31:27 +00:00
|
|
|
$('#debug').text(text);
|
2017-05-25 02:13:56 +00:00
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:17:45 +00:00
|
|
|
async function onFormOptionsChanged() {
|
2019-09-07 19:59:10 +00:00
|
|
|
const optionsContext = getOptionsContext();
|
2019-12-12 02:11:07 +00:00
|
|
|
const options = await getOptionsMutable(optionsContext);
|
2019-09-07 19:54:00 +00:00
|
|
|
|
|
|
|
await formRead(options);
|
2019-09-11 00:46:30 +00:00
|
|
|
await settingsSaveOptions();
|
2019-09-07 19:54:00 +00:00
|
|
|
formUpdateVisibility(options);
|
2017-03-05 23:54:03 +00:00
|
|
|
|
2019-12-02 03:16:58 +00:00
|
|
|
await onAnkiOptionsChanged(options);
|
2017-08-15 04:43:09 +00:00
|
|
|
}
|
2017-07-17 02:29:43 +00:00
|
|
|
|
2019-09-11 00:46:30 +00:00
|
|
|
|
|
|
|
function settingsGetSource() {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.tabs.getCurrent((tab) => resolve(`settings${tab ? tab.id : ''}`));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function settingsSaveOptions() {
|
|
|
|
const source = await settingsGetSource();
|
|
|
|
await apiOptionsSave(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onOptionsUpdate({source}) {
|
|
|
|
const thisSource = await settingsGetSource();
|
|
|
|
if (source === thisSource) { return; }
|
|
|
|
|
|
|
|
const optionsContext = getOptionsContext();
|
2019-12-12 02:11:07 +00:00
|
|
|
const options = await getOptionsMutable(optionsContext);
|
2019-09-11 00:46:30 +00:00
|
|
|
await formWrite(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-28 17:42:48 +00:00
|
|
|
function showExtensionInformation() {
|
|
|
|
const node = document.getElementById('extension-info');
|
|
|
|
if (node === null) { return; }
|
|
|
|
|
|
|
|
const manifest = chrome.runtime.getManifest();
|
|
|
|
node.textContent = `${manifest.name} v${manifest.version}`;
|
|
|
|
}
|
2019-12-01 21:18:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async function onReady() {
|
|
|
|
showExtensionInformation();
|
|
|
|
|
|
|
|
formSetupEventListeners();
|
|
|
|
appearanceInitialize();
|
|
|
|
await audioSettingsInitialize();
|
|
|
|
await profileOptionsSetup();
|
|
|
|
await dictSettingsInitialize();
|
2019-12-02 03:16:58 +00:00
|
|
|
ankiInitialize();
|
2019-12-01 21:18:29 +00:00
|
|
|
ankiTemplatesInitialize();
|
|
|
|
|
|
|
|
storageInfoInitialize();
|
|
|
|
|
2019-12-20 18:36:54 +00:00
|
|
|
yomichan.on('optionsUpdate', onOptionsUpdate);
|
2019-12-01 21:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(() => onReady());
|