2016-04-04 03:05:22 +00:00
|
|
|
/*
|
2017-08-15 04:43:09 +00:00
|
|
|
* Copyright (C) 2016-2017 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-09-08 17:27:32 +00:00
|
|
|
async function getOptionsArray() {
|
|
|
|
const optionsFull = await apiOptionsGetFull();
|
|
|
|
return optionsFull.profiles.map(profile => profile.options);
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
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();
|
2019-09-14 20:21:41 +00:00
|
|
|
options.anki.terms.fields = utilBackgroundIsolate(ankiFieldsToDict($('#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-09-14 20:21:41 +00:00
|
|
|
options.anki.kanji.fields = utilBackgroundIsolate(ankiFieldsToDict($('#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-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);
|
|
|
|
|
|
|
|
$('#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 ankiDeckAndModelPopulate(options);
|
|
|
|
} catch (e) {
|
|
|
|
ankiErrorShow(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
formUpdateVisibility(options);
|
|
|
|
}
|
|
|
|
|
2019-09-08 16:16:12 +00:00
|
|
|
function formSetupEventListeners() {
|
|
|
|
$('#field-templates-reset').click(utilAsync(onAnkiFieldTemplatesReset));
|
2019-10-11 01:28:27 +00:00
|
|
|
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged));
|
2019-09-08 16:16:12 +00:00
|
|
|
$('.anki-model').change(utilAsync(onAnkiModelChanged));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
temp.anki.fieldTemplates = '...';
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-15 04:43:09 +00:00
|
|
|
async function onFormOptionsChanged(e) {
|
2017-10-29 17:18:15 +00:00
|
|
|
if (!e.originalEvent && !e.isTrigger) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 23:54:03 +00:00
|
|
|
|
2019-09-07 19:59:10 +00:00
|
|
|
const optionsContext = getOptionsContext();
|
|
|
|
const options = await apiOptionsGet(optionsContext);
|
2019-09-07 19:54:00 +00:00
|
|
|
const optionsAnkiEnableOld = options.anki.enable;
|
|
|
|
const optionsAnkiServerOld = options.anki.server;
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-10-29 17:18:15 +00:00
|
|
|
try {
|
2017-07-17 02:29:43 +00:00
|
|
|
const ankiUpdated =
|
2019-09-07 19:54:00 +00:00
|
|
|
options.anki.enable !== optionsAnkiEnableOld ||
|
|
|
|
options.anki.server !== optionsAnkiServerOld;
|
2017-07-17 02:29:43 +00:00
|
|
|
|
|
|
|
if (ankiUpdated) {
|
2017-08-16 03:58:50 +00:00
|
|
|
ankiSpinnerShow(true);
|
2019-09-07 19:54:00 +00:00
|
|
|
await ankiDeckAndModelPopulate(options);
|
2017-08-16 03:58:50 +00:00
|
|
|
ankiErrorShow();
|
2017-07-17 02:29:43 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
ankiErrorShow(e);
|
|
|
|
} finally {
|
|
|
|
ankiSpinnerShow(false);
|
|
|
|
}
|
2017-08-15 04:43:09 +00:00
|
|
|
}
|
2017-07-17 02:29:43 +00:00
|
|
|
|
2017-08-15 04:43:09 +00:00
|
|
|
async function onReady() {
|
2019-09-28 17:42:48 +00:00
|
|
|
showExtensionInformation();
|
|
|
|
|
2019-09-08 16:16:12 +00:00
|
|
|
formSetupEventListeners();
|
2019-10-12 16:59:51 +00:00
|
|
|
appearanceInitialize();
|
2019-10-11 01:28:27 +00:00
|
|
|
await audioSettingsInitialize();
|
2019-09-08 17:16:05 +00:00
|
|
|
await profileOptionsSetup();
|
2019-11-02 18:06:16 +00:00
|
|
|
await dictSettingsInitialize();
|
2019-05-04 16:57:55 +00:00
|
|
|
|
|
|
|
storageInfoInitialize();
|
2019-09-11 00:46:30 +00:00
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(onMessage);
|
2017-08-15 04:43:09 +00:00
|
|
|
}
|
2017-07-17 02:29:43 +00:00
|
|
|
|
2017-08-15 04:43:09 +00:00
|
|
|
$(document).ready(utilAsync(onReady));
|
2017-03-03 05:01:49 +00:00
|
|
|
|
|
|
|
|
2019-11-02 21:37:53 +00:00
|
|
|
/*
|
|
|
|
* Page exit prevention
|
|
|
|
*/
|
|
|
|
|
|
|
|
class PageExitPrevention {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
PageExitPrevention._addInstance(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
PageExitPrevention._removeInstance(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static _addInstance(instance) {
|
|
|
|
const size = PageExitPrevention._instances.size;
|
|
|
|
PageExitPrevention._instances.set(instance, true);
|
|
|
|
if (size === 0) {
|
|
|
|
window.addEventListener('beforeunload', PageExitPrevention._onBeforeUnload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static _removeInstance(instance) {
|
|
|
|
if (
|
|
|
|
PageExitPrevention._instances.delete(instance) &&
|
|
|
|
PageExitPrevention._instances.size === 0
|
|
|
|
) {
|
|
|
|
window.removeEventListener('beforeunload', PageExitPrevention._onBeforeUnload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static _onBeforeUnload(e) {
|
|
|
|
if (PageExitPrevention._instances.size === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.returnValue = '';
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PageExitPrevention._instances = new Map();
|
|
|
|
|
|
|
|
|
2019-10-12 16:59:51 +00:00
|
|
|
/*
|
|
|
|
* 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');
|
2019-10-12 21:21:36 +00:00
|
|
|
const customCss = $('#custom-popup-css');
|
2019-10-13 15:51:59 +00:00
|
|
|
const customOuterCss = $('#custom-popup-outer-css');
|
2019-10-12 16:59:51 +00:00
|
|
|
|
|
|
|
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}, '*');
|
2019-10-12 21:21:36 +00:00
|
|
|
});
|
|
|
|
customCss.on('input', () => {
|
|
|
|
const action = 'setCustomCss';
|
|
|
|
const params = {css: customCss.val()};
|
|
|
|
frame.contentWindow.postMessage({action, params}, '*');
|
2019-10-13 15:51:59 +00:00
|
|
|
});
|
|
|
|
customOuterCss.on('input', () => {
|
|
|
|
const action = 'setCustomOuterCss';
|
|
|
|
const params = {css: customOuterCss.val()};
|
|
|
|
frame.contentWindow.postMessage({action, params}, '*');
|
2019-10-12 16:59:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
container.append(frame);
|
|
|
|
buttonContainer.remove();
|
|
|
|
settings.css('display', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-11 01:28:27 +00:00
|
|
|
/*
|
|
|
|
* 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();
|
2019-10-13 02:50:22 +00:00
|
|
|
|
|
|
|
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($('<option>').val('').text('None'));
|
|
|
|
for (const {voice} of voices) {
|
|
|
|
select.append($('<option>').val(voice.voiceURI).text(`${voice.name} (${voice.lang})`));
|
|
|
|
}
|
|
|
|
|
|
|
|
select.val(select.attr('data-value'));
|
|
|
|
}
|
|
|
|
|
2019-10-13 20:30:31 +00:00
|
|
|
function languageTagIsJapanese(languageTag) {
|
|
|
|
return (
|
|
|
|
languageTag.startsWith('ja-') ||
|
|
|
|
languageTag.startsWith('jpn-')
|
|
|
|
);
|
2019-10-13 02:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function textToSpeechVoiceCompare(a, b) {
|
2019-10-13 20:30:31 +00:00
|
|
|
const aIsJapanese = languageTagIsJapanese(a.voice.lang);
|
|
|
|
const bIsJapanese = languageTagIsJapanese(b.voice.lang);
|
|
|
|
if (aIsJapanese) {
|
|
|
|
if (!bIsJapanese) { return -1; }
|
|
|
|
} else {
|
|
|
|
if (bIsJapanese) { return 1; }
|
|
|
|
}
|
2019-10-13 02:50:22 +00:00
|
|
|
|
2019-10-13 20:30:31 +00:00
|
|
|
const aIsDefault = a.voice.default;
|
|
|
|
const bIsDefault = b.voice.default;
|
|
|
|
if (aIsDefault) {
|
|
|
|
if (!bIsDefault) { return -1; }
|
|
|
|
} else {
|
|
|
|
if (bIsDefault) { return 1; }
|
2019-10-13 02:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a.index < b.index) { return -1; }
|
|
|
|
if (a.index > b.index) { return 1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function textToSpeechTest() {
|
|
|
|
try {
|
|
|
|
const text = $('#text-to-speech-voice-test').attr('data-speech-text') || '';
|
|
|
|
const voiceURI = $('#text-to-speech-voice').val();
|
|
|
|
const voice = audioGetTextToSpeechVoice(voiceURI);
|
|
|
|
if (voice === null) { return; }
|
|
|
|
|
|
|
|
const utterance = new SpeechSynthesisUtterance(text);
|
|
|
|
utterance.lang = 'ja-JP';
|
|
|
|
utterance.voice = voice;
|
|
|
|
utterance.volume = 1.0;
|
|
|
|
|
|
|
|
speechSynthesis.speak(utterance);
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
2019-10-11 01:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-11 00:46:30 +00:00
|
|
|
/*
|
|
|
|
* Remote options updates
|
|
|
|
*/
|
|
|
|
|
|
|
|
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();
|
|
|
|
const options = await apiOptionsGet(optionsContext);
|
|
|
|
await formWrite(options);
|
|
|
|
}
|
|
|
|
|
2019-10-20 02:28:23 +00:00
|
|
|
function onMessage({action, params}, sender, callback) {
|
|
|
|
switch (action) {
|
|
|
|
case 'optionsUpdate':
|
|
|
|
onOptionsUpdate(params);
|
|
|
|
break;
|
|
|
|
case 'getUrl':
|
|
|
|
callback({url: window.location.href});
|
|
|
|
break;
|
2019-09-11 00:46:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
/*
|
|
|
|
* Anki
|
|
|
|
*/
|
|
|
|
|
2017-03-05 23:54:03 +00:00
|
|
|
function ankiSpinnerShow(show) {
|
2017-03-03 05:01:49 +00:00
|
|
|
const spinner = $('#anki-spinner');
|
|
|
|
if (show) {
|
|
|
|
spinner.show();
|
|
|
|
} else {
|
|
|
|
spinner.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:54:03 +00:00
|
|
|
function ankiErrorShow(error) {
|
2017-03-03 05:01:49 +00:00
|
|
|
const dialog = $('#anki-error');
|
|
|
|
if (error) {
|
2017-09-23 02:39:05 +00:00
|
|
|
dialog.show().text(error);
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
dialog.hide();
|
2016-04-04 03:05:22 +00:00
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 02:55:33 +00:00
|
|
|
function ankiErrorShown() {
|
|
|
|
return $('#anki-error').is(':visible');
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function ankiFieldsToDict(selection) {
|
|
|
|
const result = {};
|
|
|
|
selection.each((index, element) => {
|
|
|
|
result[$(element).data('field')] = $(element).val();
|
|
|
|
});
|
2016-04-04 03:05:22 +00:00
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
return result;
|
2016-04-04 03:05:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 02:29:43 +00:00
|
|
|
async function ankiDeckAndModelPopulate(options) {
|
2017-03-03 05:01:49 +00:00
|
|
|
const ankiFormat = $('#anki-format').hide();
|
|
|
|
|
2017-07-28 04:42:14 +00:00
|
|
|
const deckNames = await utilAnkiGetDeckNames();
|
2017-07-17 02:29:43 +00:00
|
|
|
const ankiDeck = $('.anki-deck');
|
|
|
|
ankiDeck.find('option').remove();
|
|
|
|
deckNames.sort().forEach(name => ankiDeck.append($('<option/>', {value: name, text: name})));
|
|
|
|
|
2017-07-28 04:42:14 +00:00
|
|
|
const modelNames = await utilAnkiGetModelNames();
|
2017-07-17 02:29:43 +00:00
|
|
|
const ankiModel = $('.anki-model');
|
|
|
|
ankiModel.find('option').remove();
|
|
|
|
modelNames.sort().forEach(name => ankiModel.append($('<option/>', {value: name, text: name})));
|
2017-03-03 05:01:49 +00:00
|
|
|
|
2017-08-19 18:23:19 +00:00
|
|
|
$('#anki-terms-deck').val(options.anki.terms.deck);
|
|
|
|
await ankiFieldsPopulate($('#anki-terms-model').val(options.anki.terms.model), options);
|
|
|
|
|
|
|
|
$('#anki-kanji-deck').val(options.anki.kanji.deck);
|
|
|
|
await ankiFieldsPopulate($('#anki-kanji-model').val(options.anki.kanji.model), options);
|
2017-07-17 02:29:43 +00:00
|
|
|
|
|
|
|
ankiFormat.show();
|
|
|
|
}
|
|
|
|
|
2019-11-09 19:24:36 +00:00
|
|
|
function ankiCreateFieldTemplate(name, value, markers) {
|
|
|
|
const template = document.querySelector('#anki-field-template').content;
|
|
|
|
const content = document.importNode(template, true).firstChild;
|
|
|
|
|
|
|
|
content.querySelector('.anki-field-name').textContent = name;
|
|
|
|
|
|
|
|
const field = content.querySelector('.anki-field-value');
|
|
|
|
field.dataset.field = name;
|
|
|
|
field.value = value;
|
|
|
|
|
|
|
|
content.querySelector('.anki-field-marker-list').appendChild(ankiGetFieldMarkersHtml(markers));
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ankiGetFieldMarkersHtml(markers, fragment) {
|
|
|
|
const template = document.querySelector('#anki-field-marker-template').content;
|
|
|
|
if (!fragment) {
|
|
|
|
fragment = new DocumentFragment();
|
|
|
|
}
|
|
|
|
for (const marker of markers) {
|
|
|
|
const markerNode = document.importNode(template, true).firstChild;
|
|
|
|
markerNode.querySelector('.marker-link').textContent = marker;
|
|
|
|
fragment.appendChild(markerNode);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ankiGetFieldMarkers(type) {
|
|
|
|
switch (type) {
|
|
|
|
case 'terms':
|
|
|
|
return [
|
|
|
|
'audio',
|
|
|
|
'cloze-body',
|
|
|
|
'cloze-prefix',
|
|
|
|
'cloze-suffix',
|
|
|
|
'dictionary',
|
|
|
|
'expression',
|
|
|
|
'furigana',
|
|
|
|
'furigana-plain',
|
|
|
|
'glossary',
|
|
|
|
'glossary-brief',
|
|
|
|
'reading',
|
|
|
|
'screenshot',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
case 'kanji':
|
|
|
|
return [
|
|
|
|
'character',
|
|
|
|
'dictionary',
|
|
|
|
'glossary',
|
|
|
|
'kunyomi',
|
|
|
|
'onyomi',
|
|
|
|
'screenshot',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
|
|
|
'url'
|
|
|
|
];
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 02:29:43 +00:00
|
|
|
async function ankiFieldsPopulate(element, options) {
|
2017-03-03 05:01:49 +00:00
|
|
|
const modelName = element.val();
|
2017-07-09 22:23:11 +00:00
|
|
|
if (!modelName) {
|
2017-07-17 02:29:43 +00:00
|
|
|
return;
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 02:29:43 +00:00
|
|
|
const tab = element.closest('.tab-pane');
|
|
|
|
const tabId = tab.attr('id');
|
|
|
|
const container = tab.find('tbody').empty();
|
2019-11-09 19:24:36 +00:00
|
|
|
const markers = ankiGetFieldMarkers(tabId);
|
2017-03-03 05:01:49 +00:00
|
|
|
|
2017-07-28 04:42:14 +00:00
|
|
|
for (const name of await utilAnkiGetModelFieldNames(modelName)) {
|
2017-07-17 02:29:43 +00:00
|
|
|
const value = options.anki[tabId].fields[name] || '';
|
2019-11-09 19:24:36 +00:00
|
|
|
const html = ankiCreateFieldTemplate(name, value, markers);
|
2017-07-17 02:29:43 +00:00
|
|
|
container.append($(html));
|
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
|
2017-08-15 04:43:09 +00:00
|
|
|
tab.find('.anki-field-value').change(utilAsync(onFormOptionsChanged));
|
2017-07-17 02:55:33 +00:00
|
|
|
tab.find('.marker-link').click(onAnkiMarkerClicked);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAnkiMarkerClicked(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const link = e.target;
|
|
|
|
$(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change');
|
2016-04-04 03:05:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 04:43:09 +00:00
|
|
|
async function onAnkiModelChanged(e) {
|
2017-07-17 02:29:43 +00:00
|
|
|
try {
|
2017-08-15 04:43:09 +00:00
|
|
|
if (!e.originalEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 02:29:43 +00:00
|
|
|
const element = $(this);
|
2017-03-03 05:01:49 +00:00
|
|
|
const tab = element.closest('.tab-pane');
|
|
|
|
const tabId = tab.attr('id');
|
2017-07-17 02:29:43 +00:00
|
|
|
|
2019-09-07 19:59:10 +00:00
|
|
|
const optionsContext = getOptionsContext();
|
|
|
|
const options = await apiOptionsGet(optionsContext);
|
2019-09-07 19:54:00 +00:00
|
|
|
await formRead(options);
|
2019-09-14 20:21:41 +00:00
|
|
|
options.anki[tabId].fields = utilBackgroundIsolate({});
|
2019-09-11 00:46:30 +00:00
|
|
|
await settingsSaveOptions();
|
2017-07-17 02:29:43 +00:00
|
|
|
|
2017-08-16 03:58:50 +00:00
|
|
|
ankiSpinnerShow(true);
|
2019-09-07 19:54:00 +00:00
|
|
|
await ankiFieldsPopulate(element, options);
|
2017-08-16 03:58:50 +00:00
|
|
|
ankiErrorShow();
|
2017-07-17 02:29:43 +00:00
|
|
|
} catch (e) {
|
|
|
|
ankiErrorShow(e);
|
|
|
|
} finally {
|
|
|
|
ankiSpinnerShow(false);
|
|
|
|
}
|
2017-08-15 04:43:09 +00:00
|
|
|
}
|
2017-09-06 20:18:06 +00:00
|
|
|
|
|
|
|
async function onAnkiFieldTemplatesReset(e) {
|
|
|
|
try {
|
|
|
|
e.preventDefault();
|
2019-09-07 19:59:10 +00:00
|
|
|
const optionsContext = getOptionsContext();
|
|
|
|
const options = await apiOptionsGet(optionsContext);
|
2019-09-07 23:50:58 +00:00
|
|
|
const fieldTemplates = profileOptionsGetDefaultFieldTemplates();
|
2019-09-07 19:59:10 +00:00
|
|
|
options.anki.fieldTemplates = fieldTemplates;
|
|
|
|
$('#field-templates').val(fieldTemplates);
|
2019-09-11 00:46:30 +00:00
|
|
|
await settingsSaveOptions();
|
2017-09-06 20:18:06 +00:00
|
|
|
} catch (e) {
|
|
|
|
ankiErrorShow(e);
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 16:57:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Storage
|
|
|
|
*/
|
|
|
|
|
|
|
|
function storageBytesToLabeledString(size) {
|
|
|
|
const base = 1000;
|
2019-10-11 23:22:46 +00:00
|
|
|
const labels = [' bytes', 'KB', 'MB', 'GB'];
|
2019-05-04 16:57:55 +00:00
|
|
|
let labelIndex = 0;
|
|
|
|
while (size >= base) {
|
|
|
|
size /= base;
|
|
|
|
++labelIndex;
|
|
|
|
}
|
2019-10-11 23:22:46 +00:00
|
|
|
const label = labelIndex === 0 ? `${size}` : size.toFixed(1);
|
2019-05-04 16:57:55 +00:00
|
|
|
return `${label}${labels[labelIndex]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function storageEstimate() {
|
|
|
|
try {
|
2019-02-27 02:03:34 +00:00
|
|
|
return (storageEstimate.mostRecent = await navigator.storage.estimate());
|
2019-05-04 16:57:55 +00:00
|
|
|
} catch (e) { }
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-27 02:03:34 +00:00
|
|
|
storageEstimate.mostRecent = null;
|
2019-05-04 16:57:55 +00:00
|
|
|
|
2019-10-11 23:22:46 +00:00
|
|
|
async function isStoragePeristent() {
|
|
|
|
try {
|
|
|
|
return await navigator.storage.persisted();
|
|
|
|
} catch (e) { }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:57:55 +00:00
|
|
|
async function storageInfoInitialize() {
|
2019-10-11 23:08:54 +00:00
|
|
|
storagePersistInitialize();
|
2019-10-13 21:32:28 +00:00
|
|
|
const {browser, platform} = await apiGetEnvironmentInfo();
|
|
|
|
document.documentElement.dataset.browser = browser;
|
|
|
|
document.documentElement.dataset.operatingSystem = platform.os;
|
2019-05-04 16:57:55 +00:00
|
|
|
|
|
|
|
await storageShowInfo();
|
|
|
|
|
2019-08-30 00:45:07 +00:00
|
|
|
document.querySelector('#storage-refresh').addEventListener('click', () => storageShowInfo(), false);
|
2019-05-04 16:57:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 02:03:34 +00:00
|
|
|
async function storageUpdateStats() {
|
|
|
|
storageUpdateStats.isUpdating = true;
|
2019-05-04 16:57:55 +00:00
|
|
|
|
|
|
|
const estimate = await storageEstimate();
|
|
|
|
const valid = (estimate !== null);
|
|
|
|
|
|
|
|
if (valid) {
|
2019-10-11 23:22:46 +00:00
|
|
|
// Firefox reports usage as 0 when persistent storage is enabled.
|
|
|
|
const finite = (estimate.usage > 0 || !(await isStoragePeristent()));
|
|
|
|
if (finite) {
|
|
|
|
document.querySelector('#storage-usage').textContent = storageBytesToLabeledString(estimate.usage);
|
|
|
|
document.querySelector('#storage-quota').textContent = storageBytesToLabeledString(estimate.quota);
|
|
|
|
}
|
|
|
|
document.querySelector('#storage-use-finite').classList.toggle('storage-hidden', !finite);
|
|
|
|
document.querySelector('#storage-use-infinite').classList.toggle('storage-hidden', finite);
|
2019-05-04 16:57:55 +00:00
|
|
|
}
|
2019-02-27 02:03:34 +00:00
|
|
|
|
|
|
|
storageUpdateStats.isUpdating = false;
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
storageUpdateStats.isUpdating = false;
|
|
|
|
|
|
|
|
async function storageShowInfo() {
|
|
|
|
storageSpinnerShow(true);
|
|
|
|
|
|
|
|
const valid = await storageUpdateStats();
|
2019-08-30 00:45:07 +00:00
|
|
|
document.querySelector('#storage-use').classList.toggle('storage-hidden', !valid);
|
|
|
|
document.querySelector('#storage-error').classList.toggle('storage-hidden', valid);
|
2019-05-04 16:57:55 +00:00
|
|
|
|
|
|
|
storageSpinnerShow(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function storageSpinnerShow(show) {
|
|
|
|
const spinner = $('#storage-spinner');
|
|
|
|
if (show) {
|
|
|
|
spinner.show();
|
|
|
|
} else {
|
|
|
|
spinner.hide();
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 17:42:48 +00:00
|
|
|
|
2019-10-11 23:08:54 +00:00
|
|
|
async function storagePersistInitialize() {
|
|
|
|
if (!(navigator.storage && navigator.storage.persist)) {
|
|
|
|
// Not supported
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const info = document.querySelector('#storage-persist-info');
|
|
|
|
const button = document.querySelector('#storage-persist-button');
|
|
|
|
const checkbox = document.querySelector('#storage-persist-button-checkbox');
|
|
|
|
|
|
|
|
info.classList.remove('storage-hidden');
|
|
|
|
button.classList.remove('storage-hidden');
|
|
|
|
|
2019-10-11 23:22:46 +00:00
|
|
|
let persisted = await isStoragePeristent();
|
2019-10-15 23:22:30 +00:00
|
|
|
checkbox.checked = persisted;
|
2019-10-11 23:08:54 +00:00
|
|
|
|
|
|
|
button.addEventListener('click', async () => {
|
|
|
|
if (persisted) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-13 22:41:16 +00:00
|
|
|
let result = false;
|
|
|
|
try {
|
|
|
|
result = await navigator.storage.persist();
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
2019-10-11 23:08:54 +00:00
|
|
|
persisted = true;
|
|
|
|
checkbox.checked = true;
|
2019-10-11 23:22:46 +00:00
|
|
|
storageShowInfo();
|
2019-10-15 23:22:30 +00:00
|
|
|
} else {
|
|
|
|
$('.storage-persist-fail-warning').removeClass('storage-hidden');
|
2019-10-11 23:08:54 +00:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
2019-09-28 17:42:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Information
|
|
|
|
*/
|
|
|
|
|
|
|
|
function showExtensionInformation() {
|
|
|
|
const node = document.getElementById('extension-info');
|
|
|
|
if (node === null) { return; }
|
|
|
|
|
|
|
|
const manifest = chrome.runtime.getManifest();
|
|
|
|
node.textContent = `${manifest.name} v${manifest.version}`;
|
|
|
|
}
|