Merge pull request #205 from toasted-nutbread/settings-improvements2

Settings improvements part 2
This commit is contained in:
Alex Yatskov 2019-09-12 19:47:37 -07:00 committed by GitHub
commit 1fea1184bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 362 additions and 226 deletions

View File

@ -17,16 +17,19 @@
*/ */
function apiOptionsGetSync() { function apiOptionsGet(optionsContext) {
return utilBackend().options; return utilBackend().getOptions(optionsContext);
} }
async function apiOptionsGet() { async function apiOptionsSave(source) {
return apiOptionsGetSync(); const backend = utilBackend();
const options = await backend.getFullOptions();
await optionsSave(options);
backend.onOptionsUpdated(source);
} }
async function apiTermsFind(text) { async function apiTermsFind(text, optionsContext) {
const options = apiOptionsGetSync(); const options = await apiOptionsGet(optionsContext);
const translator = utilBackend().translator; const translator = utilBackend().translator;
const searcher = { const searcher = {
@ -38,7 +41,8 @@ async function apiTermsFind(text) {
const {definitions, length} = await searcher( const {definitions, length} = await searcher(
text, text,
dictEnabledSet(options), dictEnabledSet(options),
options.scanning.alphanumeric options.scanning.alphanumeric,
options
); );
return { return {
@ -47,14 +51,14 @@ async function apiTermsFind(text) {
}; };
} }
async function apiKanjiFind(text) { async function apiKanjiFind(text, optionsContext) {
const options = apiOptionsGetSync(); const options = await apiOptionsGet(optionsContext);
const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options)); const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options));
return definitions.slice(0, options.general.maxResults); return definitions.slice(0, options.general.maxResults);
} }
async function apiDefinitionAdd(definition, mode, context) { async function apiDefinitionAdd(definition, mode, context, optionsContext) {
const options = apiOptionsGetSync(); const options = await apiOptionsGet(optionsContext);
if (mode !== 'kanji') { if (mode !== 'kanji') {
await audioInject( await audioInject(
@ -76,14 +80,15 @@ async function apiDefinitionAdd(definition, mode, context) {
return utilBackend().anki.addNote(note); return utilBackend().anki.addNote(note);
} }
async function apiDefinitionsAddable(definitions, modes) { async function apiDefinitionsAddable(definitions, modes, optionsContext) {
const options = await apiOptionsGet(optionsContext);
const states = []; const states = [];
try { try {
const notes = []; const notes = [];
for (const definition of definitions) { for (const definition of definitions) {
for (const mode of modes) { for (const mode of modes) {
const note = await dictNoteFormat(definition, mode, apiOptionsGetSync()); const note = await dictNoteFormat(definition, mode, options);
notes.push(note); notes.push(note);
} }
} }
@ -131,9 +136,10 @@ async function apiCommandExec(command) {
}, },
toggle: async () => { toggle: async () => {
const options = apiOptionsGetSync(); const optionsContext = {depth: 0};
const options = await apiOptionsGet(optionsContext);
options.general.enable = !options.general.enable; options.general.enable = !options.general.enable;
await optionsSave(options); await apiOptionsSave('popup');
} }
}; };

View File

@ -22,44 +22,43 @@ class Backend {
this.translator = new Translator(); this.translator = new Translator();
this.anki = new AnkiNull(); this.anki = new AnkiNull();
this.options = null; this.options = null;
this.optionsContext = {
depth: 0
};
this.isPreparedResolve = null;
this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve));
this.apiForwarder = new BackendApiForwarder(); this.apiForwarder = new BackendApiForwarder();
} }
async prepare() { async prepare() {
await this.translator.prepare(); await this.translator.prepare();
this.onOptionsUpdated(await optionsLoad()); this.options = await optionsLoad();
this.onOptionsUpdated('background');
if (chrome.commands !== null && typeof chrome.commands === 'object') { if (chrome.commands !== null && typeof chrome.commands === 'object') {
chrome.commands.onCommand.addListener(this.onCommand.bind(this)); chrome.commands.onCommand.addListener(this.onCommand.bind(this));
} }
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
if (this.options.general.showGuide) { const options = this.getOptionsSync(this.optionsContext);
if (options.general.showGuide) {
chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')});
} }
this.isPreparedResolve();
this.isPreparedResolve = null;
this.isPreparedPromise = null;
} }
onOptionsUpdated(options) { onOptionsUpdated(source) {
options = utilIsolate(options); this.applyOptions();
this.options = options;
if (!options.general.enable) {
this.setExtensionBadgeBackgroundColor('#555555');
this.setExtensionBadgeText('off');
} else if (!dictConfigured(options)) {
this.setExtensionBadgeBackgroundColor('#f0ad4e');
this.setExtensionBadgeText('!');
} else {
this.setExtensionBadgeText('');
}
this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull();
const callback = () => this.checkLastError(chrome.runtime.lastError); const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.query({}, tabs => { chrome.tabs.query({}, tabs => {
for (const tab of tabs) { for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: {options}}, callback); chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {source}}, callback);
} }
}); });
} }
@ -78,24 +77,24 @@ class Backend {
}; };
const handlers = { const handlers = {
optionsGet: ({callback}) => { optionsGet: ({optionsContext, callback}) => {
forward(apiOptionsGet(), callback); forward(apiOptionsGet(optionsContext), callback);
}, },
kanjiFind: ({text, callback}) => { kanjiFind: ({text, optionsContext, callback}) => {
forward(apiKanjiFind(text), callback); forward(apiKanjiFind(text, optionsContext), callback);
}, },
termsFind: ({text, callback}) => { termsFind: ({text, optionsContext, callback}) => {
forward(apiTermsFind(text), callback); forward(apiTermsFind(text, optionsContext), callback);
}, },
definitionAdd: ({definition, mode, context, callback}) => { definitionAdd: ({definition, mode, context, optionsContext, callback}) => {
forward(apiDefinitionAdd(definition, mode, context), callback); forward(apiDefinitionAdd(definition, mode, context, optionsContext), callback);
}, },
definitionsAddable: ({definitions, modes, callback}) => { definitionsAddable: ({definitions, modes, optionsContext, callback}) => {
forward(apiDefinitionsAddable(definitions, modes), callback); forward(apiDefinitionsAddable(definitions, modes, optionsContext), callback);
}, },
noteView: ({noteId}) => { noteView: ({noteId}) => {
@ -136,6 +135,39 @@ class Backend {
return true; return true;
} }
applyOptions() {
const options = this.getOptionsSync(this.optionsContext);
if (!options.general.enable) {
this.setExtensionBadgeBackgroundColor('#555555');
this.setExtensionBadgeText('off');
} else if (!dictConfigured(options)) {
this.setExtensionBadgeBackgroundColor('#f0ad4e');
this.setExtensionBadgeText('!');
} else {
this.setExtensionBadgeText('');
}
this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull();
}
async getFullOptions() {
if (this.isPreparedPromise !== null) {
await this.isPreparedPromise;
}
return this.options;
}
async getOptions(optionsContext) {
if (this.isPreparedPromise !== null) {
await this.isPreparedPromise;
}
return this.getOptionsSync(optionsContext);
}
getOptionsSync(optionsContext) {
return this.options;
}
setExtensionBadgeBackgroundColor(color) { setExtensionBadgeBackgroundColor(color) {
if (typeof chrome.browserAction.setBadgeBackgroundColor === 'function') { if (typeof chrome.browserAction.setBadgeBackgroundColor === 'function') {
chrome.browserAction.setBadgeBackgroundColor({color}); chrome.browserAction.setBadgeBackgroundColor({color});

View File

@ -22,7 +22,8 @@ $(document).ready(utilAsync(() => {
$('#open-options').click(() => apiCommandExec('options')); $('#open-options').click(() => apiCommandExec('options'));
$('#open-help').click(() => apiCommandExec('help')); $('#open-help').click(() => apiCommandExec('help'));
optionsLoad().then(options => { const optionsContext = {depth: 0};
apiOptionsGet(optionsContext).then(options => {
const toggle = $('#enable-search'); const toggle = $('#enable-search');
toggle.prop('checked', options.general.enable).change(); toggle.prop('checked', options.general.enable).change();
toggle.bootstrapToggle(); toggle.bootstrapToggle();

View File

@ -330,7 +330,7 @@ function optionsLoad() {
}).then(optionsStr => { }).then(optionsStr => {
if (typeof optionsStr === 'string') { if (typeof optionsStr === 'string') {
const options = JSON.parse(optionsStr); const options = JSON.parse(optionsStr);
if (typeof options === 'object' && options !== null && !Array.isArray(options)) { if (utilIsObject(options)) {
return options; return options;
} }
} }
@ -343,9 +343,14 @@ function optionsLoad() {
} }
function optionsSave(options) { function optionsSave(options) {
return new Promise((resolve) => { return new Promise((resolve, reject) => {
chrome.storage.local.set({options: JSON.stringify(options)}, resolve); chrome.storage.local.set({options: JSON.stringify(options)}, () => {
}).then(() => { const error = chrome.runtime.lastError;
utilBackend().onOptionsUpdated(options); if (error) {
reject(error);
} else {
resolve();
}
});
}); });
} }

View File

@ -18,7 +18,8 @@
async function searchFrontendSetup() { async function searchFrontendSetup() {
const options = await apiOptionsGet(); const optionsContext = {depth: 0};
const options = await apiOptionsGet(optionsContext);
if (!options.scanning.enableOnSearchPage) { return; } if (!options.scanning.enableOnSearchPage) { return; }
const scriptSrcs = [ const scriptSrcs = [

View File

@ -21,6 +21,10 @@ class DisplaySearch extends Display {
constructor() { constructor() {
super($('#spinner'), $('#content')); super($('#spinner'), $('#content'));
this.optionsContext = {
depth: 0
};
this.search = $('#search').click(this.onSearch.bind(this)); this.search = $('#search').click(this.onSearch.bind(this));
this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.query = $('#query').on('input', this.onSearchInput.bind(this));
this.intro = $('#intro'); this.intro = $('#intro');
@ -46,8 +50,8 @@ class DisplaySearch extends Display {
try { try {
e.preventDefault(); e.preventDefault();
this.intro.slideUp(); this.intro.slideUp();
const {length, definitions} = await apiTermsFind(this.query.val()); const {length, definitions} = await apiTermsFind(this.query.val(), this.optionsContext);
super.termsShow(definitions, await apiOptionsGet()); super.termsShow(definitions, await apiOptionsGet(this.optionsContext));
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);
} }

View File

@ -16,73 +16,144 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
function getOptionsContext() {
async function formRead() { return {
const optionsOld = await optionsLoad(); depth: 0
const optionsNew = $.extend(true, {}, optionsOld); };
optionsNew.general.showGuide = $('#show-usage-guide').prop('checked');
optionsNew.general.compactTags = $('#compact-tags').prop('checked');
optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked');
optionsNew.general.autoPlayAudio = $('#auto-play-audio').prop('checked');
optionsNew.general.resultOutputMode = $('#result-output-mode').val();
optionsNew.general.audioSource = $('#audio-playback-source').val();
optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val());
optionsNew.general.debugInfo = $('#show-debug-info').prop('checked');
optionsNew.general.showAdvanced = $('#show-advanced-options').prop('checked');
optionsNew.general.maxResults = parseInt($('#max-displayed-results').val(), 10);
optionsNew.general.popupDisplayMode = $('#popup-display-mode').val();
optionsNew.general.popupHorizontalTextPosition = $('#popup-horizontal-text-position').val();
optionsNew.general.popupVerticalTextPosition = $('#popup-vertical-text-position').val();
optionsNew.general.popupWidth = parseInt($('#popup-width').val(), 10);
optionsNew.general.popupHeight = parseInt($('#popup-height').val(), 10);
optionsNew.general.popupHorizontalOffset = parseInt($('#popup-horizontal-offset').val(), 0);
optionsNew.general.popupVerticalOffset = parseInt($('#popup-vertical-offset').val(), 10);
optionsNew.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0);
optionsNew.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10);
optionsNew.general.customPopupCss = $('#custom-popup-css').val();
optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked');
optionsNew.scanning.touchInputEnabled = $('#touch-input-enabled').prop('checked');
optionsNew.scanning.selectText = $('#select-matched-text').prop('checked');
optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked');
optionsNew.scanning.autoHideResults = $('#auto-hide-results').prop('checked');
optionsNew.scanning.deepDomScan = $('#deep-dom-scan').prop('checked');
optionsNew.scanning.enableOnPopupExpressions = $('#enable-scanning-of-popup-expressions').prop('checked');
optionsNew.scanning.enableOnSearchPage = $('#enable-scanning-on-search-page').prop('checked');
optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10);
optionsNew.scanning.length = parseInt($('#scan-length').val(), 10);
optionsNew.scanning.modifier = $('#scan-modifier-key').val();
optionsNew.scanning.popupNestingMaxDepth = parseInt($('#popup-nesting-max-depth').val(), 10);
optionsNew.anki.enable = $('#anki-enable').prop('checked');
optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/);
optionsNew.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10);
optionsNew.anki.server = $('#interface-server').val();
optionsNew.anki.screenshot.format = $('#screenshot-format').val();
optionsNew.anki.screenshot.quality = parseInt($('#screenshot-quality').val(), 10);
optionsNew.anki.fieldTemplates = $('#field-templates').val();
if (optionsOld.anki.enable && !ankiErrorShown()) {
optionsNew.anki.terms.deck = $('#anki-terms-deck').val();
optionsNew.anki.terms.model = $('#anki-terms-model').val();
optionsNew.anki.terms.fields = ankiFieldsToDict($('#terms .anki-field-value'));
optionsNew.anki.kanji.deck = $('#anki-kanji-deck').val();
optionsNew.anki.kanji.model = $('#anki-kanji-model').val();
optionsNew.anki.kanji.fields = ankiFieldsToDict($('#kanji .anki-field-value'));
} }
optionsNew.general.mainDictionary = $('#dict-main').val(); 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.autoPlayAudio = $('#auto-play-audio').prop('checked');
options.general.resultOutputMode = $('#result-output-mode').val();
options.general.audioSource = $('#audio-playback-source').val();
options.general.audioVolume = parseFloat($('#audio-playback-volume').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.customPopupCss = $('#custom-popup-css').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.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 = $('#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 = 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 = ankiFieldsToDict($('#kanji .anki-field-value'));
}
options.general.mainDictionary = $('#dict-main').val();
$('.dict-group').each((index, element) => { $('.dict-group').each((index, element) => {
const dictionary = $(element); const dictionary = $(element);
optionsNew.dictionaries[dictionary.data('title')] = { options.dictionaries[dictionary.data('title')] = {
priority: parseInt(dictionary.find('.dict-priority').val(), 10), priority: parseInt(dictionary.find('.dict-priority').val(), 10),
enabled: dictionary.find('.dict-enabled').prop('checked'), enabled: dictionary.find('.dict-enabled').prop('checked'),
allowSecondarySearches: dictionary.find('.dict-allow-secondary-searches').prop('checked') allowSecondarySearches: dictionary.find('.dict-allow-secondary-searches').prop('checked')
}; };
}); });
}
return {optionsNew, optionsOld}; 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);
$('#auto-play-audio').prop('checked', options.general.autoPlayAudio);
$('#result-output-mode').val(options.general.resultOutputMode);
$('#audio-playback-source').val(options.general.audioSource);
$('#audio-playback-volume').val(options.general.audioVolume);
$('#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);
$('#custom-popup-css').val(options.general.customPopupCss);
$('#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-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));
$('#field-templates-reset').click(utilAsync(onAnkiFieldTemplatesReset));
$('input, select, textarea').not('.anki-model').not('.profile-form *').change(utilAsync(onFormOptionsChanged));
$('.anki-model').change(utilAsync(onAnkiModelChanged));
} }
function formUpdateVisibility(options) { function formUpdateVisibility(options) {
@ -141,18 +212,23 @@ async function onFormOptionsChanged(e) {
return; return;
} }
const {optionsNew, optionsOld} = await formRead(); const optionsContext = getOptionsContext();
await optionsSave(optionsNew); const options = await apiOptionsGet(optionsContext);
formUpdateVisibility(optionsNew); const optionsAnkiEnableOld = options.anki.enable;
const optionsAnkiServerOld = options.anki.server;
await formRead(options);
await settingsSaveOptions();
formUpdateVisibility(options);
try { try {
const ankiUpdated = const ankiUpdated =
optionsNew.anki.enable !== optionsOld.anki.enable || options.anki.enable !== optionsAnkiEnableOld ||
optionsNew.anki.server !== optionsOld.anki.server; options.anki.server !== optionsAnkiServerOld;
if (ankiUpdated) { if (ankiUpdated) {
ankiSpinnerShow(true); ankiSpinnerShow(true);
await ankiDeckAndModelPopulate(optionsNew); await ankiDeckAndModelPopulate(options);
ankiErrorShow(); ankiErrorShow();
} }
} catch (e) { } catch (e) {
@ -163,77 +239,51 @@ async function onFormOptionsChanged(e) {
} }
async function onReady() { async function onReady() {
const options = await optionsLoad(); const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
$('#show-usage-guide').prop('checked', options.general.showGuide); formSetupEventListeners();
$('#compact-tags').prop('checked', options.general.compactTags); await formWrite(options);
$('#compact-glossaries').prop('checked', options.general.compactGlossaries);
$('#auto-play-audio').prop('checked', options.general.autoPlayAudio);
$('#result-output-mode').val(options.general.resultOutputMode);
$('#audio-playback-source').val(options.general.audioSource);
$('#audio-playback-volume').val(options.general.audioVolume);
$('#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);
$('#custom-popup-css').val(options.general.customPopupCss);
$('#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-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);
$('#dict-purge-link').click(utilAsync(onDictionaryPurge));
$('#dict-file').change(utilAsync(onDictionaryImport));
$('#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);
$('#field-templates-reset').click(utilAsync(onAnkiFieldTemplatesReset));
$('input, select, textarea').not('.anki-model').change(utilAsync(onFormOptionsChanged));
$('.anki-model').change(utilAsync(onAnkiModelChanged));
try {
await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
} catch (e) {
dictionaryErrorsShow([e]);
}
try {
await ankiDeckAndModelPopulate(options);
} catch (e) {
ankiErrorShow(e);
}
formUpdateVisibility(options);
storageInfoInitialize(); storageInfoInitialize();
chrome.runtime.onMessage.addListener(onMessage);
} }
$(document).ready(utilAsync(onReady)); $(document).ready(utilAsync(onReady));
/*
* 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);
}
function onMessage({action, params}) {
if (action === 'optionsUpdate') {
onOptionsUpdate(params);
}
}
/* /*
* Dictionary * Dictionary
*/ */
@ -374,10 +424,11 @@ async function onDictionaryPurge(e) {
dictionarySpinnerShow(true); dictionarySpinnerShow(true);
await utilDatabasePurge(); await utilDatabasePurge();
const options = await optionsLoad(); const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
options.dictionaries = {}; options.dictionaries = {};
options.general.mainDictionary = ''; options.general.mainDictionary = '';
await optionsSave(options); await settingsSaveOptions();
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options); await formMainDictionaryOptionsPopulate(options);
@ -414,8 +465,9 @@ async function onDictionaryImport(e) {
setProgress(0.0); setProgress(0.0);
const exceptions = []; const exceptions = [];
const options = await optionsLoad();
const summary = await utilDatabaseImport(e.target.files[0], updateProgress, exceptions); const summary = await utilDatabaseImport(e.target.files[0], updateProgress, exceptions);
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
options.dictionaries[summary.title] = {enabled: true, priority: 0, allowSecondarySearches: false}; options.dictionaries[summary.title] = {enabled: true, priority: 0, allowSecondarySearches: false};
if (summary.sequenced && options.general.mainDictionary === '') { if (summary.sequenced && options.general.mainDictionary === '') {
options.general.mainDictionary = summary.title; options.general.mainDictionary = summary.title;
@ -426,7 +478,7 @@ async function onDictionaryImport(e) {
dictionaryErrorsShow(exceptions); dictionaryErrorsShow(exceptions);
} }
await optionsSave(options); await settingsSaveOptions();
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options); await formMainDictionaryOptionsPopulate(options);
@ -566,12 +618,14 @@ async function onAnkiModelChanged(e) {
const tab = element.closest('.tab-pane'); const tab = element.closest('.tab-pane');
const tabId = tab.attr('id'); const tabId = tab.attr('id');
const {optionsNew, optionsOld} = await formRead(); const optionsContext = getOptionsContext();
optionsNew.anki[tabId].fields = {}; const options = await apiOptionsGet(optionsContext);
await optionsSave(optionsNew); await formRead(options);
options.anki[tabId].fields = {};
await settingsSaveOptions();
ankiSpinnerShow(true); ankiSpinnerShow(true);
await ankiFieldsPopulate(element, optionsNew); await ankiFieldsPopulate(element, options);
ankiErrorShow(); ankiErrorShow();
} catch (e) { } catch (e) {
ankiErrorShow(e); ankiErrorShow(e);
@ -583,9 +637,12 @@ async function onAnkiModelChanged(e) {
async function onAnkiFieldTemplatesReset(e) { async function onAnkiFieldTemplatesReset(e) {
try { try {
e.preventDefault(); e.preventDefault();
const options = await optionsLoad(); const optionsContext = getOptionsContext();
$('#field-templates').val(options.anki.fieldTemplates = optionsFieldTemplates()); const options = await apiOptionsGet(optionsContext);
await optionsSave(options); const fieldTemplates = optionsFieldTemplates();
options.anki.fieldTemplates = fieldTemplates;
$('#field-templates').val(fieldTemplates);
await settingsSaveOptions();
} catch (e) { } catch (e) {
ankiErrorShow(e); ankiErrorShow(e);
} }

View File

@ -36,8 +36,7 @@ class Translator {
} }
} }
async findTermsGrouped(text, dictionaries, alphanumeric) { async findTermsGrouped(text, dictionaries, alphanumeric, options) {
const options = await apiOptionsGet();
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
@ -55,8 +54,7 @@ class Translator {
return {length, definitions: definitionsGrouped}; return {length, definitions: definitionsGrouped};
} }
async findTermsMerged(text, dictionaries, alphanumeric) { async findTermsMerged(text, dictionaries, alphanumeric, options) {
const options = await apiOptionsGet();
const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches); const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches);
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);

View File

@ -104,3 +104,7 @@ function utilReadFile(file) {
reader.readAsBinaryString(file); reader.readAsBinaryString(file);
}); });
} }
function utilIsObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}

View File

@ -70,6 +70,10 @@
<div> <div>
<h3>General Options</h3> <h3>General Options</h3>
<div class="checkbox">
<label><input type="checkbox" id="enable"> Enable content scanning</label>
</div>
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" id="show-usage-guide"> Show usage guide on startup</label> <label><input type="checkbox" id="show-usage-guide"> Show usage guide on startup</label>
</div> </div>
@ -221,14 +225,6 @@
<label><input type="checkbox" id="auto-hide-results"> Automatically hide results</label> <label><input type="checkbox" id="auto-hide-results"> Automatically hide results</label>
</div> </div>
<div class="checkbox options-advanced">
<label><input type="checkbox" id="enable-scanning-of-popup-expressions"> Enable scanning of popup expressions</label>
</div>
<div class="checkbox">
<label><input type="checkbox" id="enable-scanning-on-search-page"> Enable scanning on search page</label>
</div>
<div class="checkbox options-advanced"> <div class="checkbox options-advanced">
<label><input type="checkbox" id="deep-dom-scan"> Deep DOM scan</label> <label><input type="checkbox" id="deep-dom-scan"> Deep DOM scan</label>
</div> </div>
@ -252,9 +248,26 @@
<option value="shift">Shift</option> <option value="shift">Shift</option>
</select> </select>
</div> </div>
</div>
<div class="form-group options-advanced"> <div id="popup-content-scanning">
<label for="popup-nesting-max-depth">Maximum nested popup depth</label> <h3>Popup Content Scanning Options</h3>
<p class="help-block">
Yomichan is able to create additional popups in order to scan the content of other popups.
This feature can be enabled if the <strong>Maximum number of additional popups</strong> option is set to a value greater than 0.
</p>
<div class="checkbox">
<label><input type="checkbox" id="enable-scanning-on-search-page"> Enable scanning on search page</label>
</div>
<div class="checkbox">
<label><input type="checkbox" id="enable-scanning-of-popup-expressions"> Enable scanning of popup expressions</label>
</div>
<div class="form-group">
<label for="popup-nesting-max-depth">Maximum number of additional popups</label>
<input type="number" min="0" id="popup-nesting-max-depth" class="form-control"> <input type="number" min="0" id="popup-nesting-max-depth" class="form-control">
</div> </div>
</div> </div>

View File

@ -17,24 +17,24 @@
*/ */
function apiOptionsGet() { function apiOptionsGet(optionsContext) {
return utilInvoke('optionsGet'); return utilInvoke('optionsGet', {optionsContext});
} }
function apiTermsFind(text) { function apiTermsFind(text, optionsContext) {
return utilInvoke('termsFind', {text}); return utilInvoke('termsFind', {text, optionsContext});
} }
function apiKanjiFind(text) { function apiKanjiFind(text, optionsContext) {
return utilInvoke('kanjiFind', {text}); return utilInvoke('kanjiFind', {text, optionsContext});
} }
function apiDefinitionAdd(definition, mode, context) { function apiDefinitionAdd(definition, mode, context, optionsContext) {
return utilInvoke('definitionAdd', {definition, mode, context}); return utilInvoke('definitionAdd', {definition, mode, context, optionsContext});
} }
function apiDefinitionsAddable(definitions, modes) { function apiDefinitionsAddable(definitions, modes, optionsContext) {
return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null); return utilInvoke('definitionsAddable', {definitions, modes, optionsContext}).catch(() => null);
} }
function apiNoteView(noteId) { function apiNoteView(noteId) {

View File

@ -23,6 +23,10 @@ class DisplayFloat extends Display {
this.autoPlayAudioTimer = null; this.autoPlayAudioTimer = null;
this.styleNode = null; this.styleNode = null;
this.optionsContext = {
depth: 0
};
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
$(window).on('message', utilAsync(this.onMessage.bind(this))); $(window).on('message', utilAsync(this.onMessage.bind(this)));
@ -75,6 +79,7 @@ class DisplayFloat extends Display {
}, },
popupNestedInitialize: ({id, depth, parentFrameId}) => { popupNestedInitialize: ({id, depth, parentFrameId}) => {
this.optionsContext.depth = depth;
popupNestedInitialize(id, depth, parentFrameId); popupNestedInitialize(id, depth, parentFrameId);
} }
}; };

View File

@ -28,6 +28,10 @@ class Frontend {
this.options = null; this.options = null;
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null); this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
this.optionsContext = {
depth: popup.depth
};
this.primaryTouchIdentifier = null; this.primaryTouchIdentifier = null;
this.contextMenuChecking = false; this.contextMenuChecking = false;
this.contextMenuPrevent = false; this.contextMenuPrevent = false;
@ -40,9 +44,9 @@ class Frontend {
static create() { static create() {
const initializationData = window.frontendInitializationData; const initializationData = window.frontendInitializationData;
const isNested = (initializationData !== null && typeof initializationData === 'object'); const isNested = (initializationData !== null && typeof initializationData === 'object');
const {id, parentFrameId, ignoreNodes} = isNested ? initializationData : {}; const {id, depth, parentFrameId, ignoreNodes} = isNested ? initializationData : {};
const popup = isNested ? new PopupProxy(id, parentFrameId) : PopupProxyHost.instance.createPopup(null); const popup = isNested ? new PopupProxy(depth + 1, id, parentFrameId) : PopupProxyHost.instance.createPopup(null);
const frontend = new Frontend(popup, ignoreNodes); const frontend = new Frontend(popup, ignoreNodes);
frontend.prepare(); frontend.prepare();
return frontend; return frontend;
@ -50,7 +54,7 @@ class Frontend {
async prepare() { async prepare() {
try { try {
this.options = await apiOptionsGet(); this.options = await apiOptionsGet(this.optionsContext);
window.addEventListener('message', this.onFrameMessage.bind(this)); window.addEventListener('message', this.onFrameMessage.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this));
@ -261,11 +265,8 @@ class Frontend {
onBgMessage({action, params}, sender, callback) { onBgMessage({action, params}, sender, callback) {
const handlers = { const handlers = {
optionsSet: ({options}) => { optionsUpdate: () => {
this.options = options; this.updateOptions();
if (!this.options.enable) {
this.searchClear();
}
}, },
popupSetVisible: ({visible}) => { popupSetVisible: ({visible}) => {
@ -284,6 +285,13 @@ class Frontend {
console.log(error); console.log(error);
} }
async updateOptions() {
this.options = await apiOptionsGet(this.optionsContext);
if (!this.options.enable) {
this.searchClear();
}
}
popupTimerSet(callback) { popupTimerSet(callback) {
this.popupTimerClear(); this.popupTimerClear();
this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); this.popupTimer = window.setTimeout(callback, this.options.scanning.delay);
@ -347,7 +355,7 @@ class Frontend {
return; return;
} }
const {definitions, length} = await apiTermsFind(searchText); const {definitions, length} = await apiTermsFind(searchText, this.optionsContext);
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }
@ -380,7 +388,7 @@ class Frontend {
return; return;
} }
const definitions = await apiKanjiFind(searchText); const definitions = await apiKanjiFind(searchText, this.optionsContext);
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }

View File

@ -25,7 +25,8 @@ async function popupNestedInitialize(id, depth, parentFrameId) {
} }
popupNestedInitialized = true; popupNestedInitialized = true;
const options = await apiOptionsGet(); const optionsContext = {depth};
const options = await apiOptionsGet(optionsContext);
const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth;
if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) {

View File

@ -18,14 +18,14 @@
class PopupProxy { class PopupProxy {
constructor(parentId, parentFrameId) { constructor(depth, parentId, parentFrameId) {
this.parentId = parentId; this.parentId = parentId;
this.parentFrameId = parentFrameId; this.parentFrameId = parentFrameId;
this.id = null; this.id = null;
this.idPromise = null; this.idPromise = null;
this.parent = null; this.parent = null;
this.child = null; this.child = null;
this.depth = 0; this.depth = depth;
this.container = null; this.container = null;

View File

@ -27,6 +27,7 @@ class Display {
this.sequence = 0; this.sequence = 0;
this.index = 0; this.index = 0;
this.audioCache = {}; this.audioCache = {};
this.optionsContext = {};
this.dependencies = {}; this.dependencies = {};
@ -66,7 +67,7 @@ class Display {
context.source.source = this.context.source; context.source.source = this.context.source;
} }
const kanjiDefs = await apiKanjiFind(link.text()); const kanjiDefs = await apiKanjiFind(link.text(), this.optionsContext);
this.kanjiShow(kanjiDefs, this.options, context); this.kanjiShow(kanjiDefs, this.options, context);
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);
@ -89,7 +90,7 @@ class Display {
try { try {
textSource.setEndOffset(this.options.scanning.length); textSource.setEndOffset(this.options.scanning.length);
({definitions, length} = await apiTermsFind(textSource.text())); ({definitions, length} = await apiTermsFind(textSource.text(), this.optionsContext));
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }
@ -379,7 +380,7 @@ class Display {
async adderButtonUpdate(modes, sequence) { async adderButtonUpdate(modes, sequence) {
try { try {
const states = await apiDefinitionsAddable(this.definitions, modes); const states = await apiDefinitionsAddable(this.definitions, modes, this.optionsContext);
if (!states || sequence !== this.sequence) { if (!states || sequence !== this.sequence) {
return; return;
} }
@ -453,7 +454,7 @@ class Display {
} }
} }
const noteId = await apiDefinitionAdd(definition, mode, context); const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext);
if (noteId) { if (noteId) {
const index = this.definitions.indexOf(definition); const index = this.definitions.indexOf(definition);
Display.adderButtonFind(index, mode).addClass('disabled'); Display.adderButtonFind(index, mode).addClass('disabled');