yomichan/ext/bg/js/settings/main.js

164 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-04-04 03:05:22 +00:00
/*
* Copyright (C) 2016-2020 Yomichan Authors
2016-04-04 03:05:22 +00:00
*
* 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
*/
2020-03-11 02:30:36 +00:00
/* global
* AnkiController
* AnkiTemplatesController
* AudioController
* ClipboardPopupsController
* DictionaryController
* GenericSettingController
* PopupPreviewController
* ProfileController
* SettingsBackup
* SettingsController
* StorageController
* api
2020-03-11 02:30:36 +00:00
* utilBackend
* utilBackgroundIsolate
*/
2020-02-01 20:00:34 +00:00
let profileIndex = 0;
function getOptionsContext() {
return {index: getProfileIndex()};
}
function getProfileIndex() {
return profileIndex;
}
function setProfileIndex(value) {
profileIndex = value;
}
function getOptionsMutable(optionsContext) {
return utilBackend().getOptions(
utilBackgroundIsolate(optionsContext)
);
}
function getOptionsFullMutable() {
return utilBackend().getFullOptions();
}
function settingsGetSource() {
return new Promise((resolve) => {
chrome.tabs.getCurrent((tab) => resolve(`settings${tab ? tab.id : ''}`));
});
}
async function settingsSaveOptions() {
const source = await settingsGetSource();
await api.optionsSave(source);
}
async function onOptionsUpdated({source}) {
const thisSource = await settingsGetSource();
if (source === thisSource) { return; }
const optionsContext = getOptionsContext();
const options = await getOptionsMutable(optionsContext);
document.querySelector('#enable-clipboard-popups').checked = options.general.enableClipboardPopups;
if (ankiTemplatesController !== null) {
ankiTemplatesController.updateValue();
}
if (dictionaryController !== null) {
dictionaryController.optionsChanged();
}
if (ankiController !== null) {
ankiController.optionsChanged();
}
if (genericSettingController !== null) {
genericSettingController.optionsChanged(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 settingsPopulateModifierKeys() {
const scanModifierKeySelect = document.querySelector('#scan-modifier-key');
scanModifierKeySelect.textContent = '';
const environment = await api.getEnvironmentInfo();
const modifierKeys = [
{value: 'none', name: 'None'},
...environment.modifiers.keys
];
for (const {value, name} of modifierKeys) {
const option = document.createElement('option');
option.value = value;
option.textContent = name;
scanModifierKeySelect.appendChild(option);
}
}
async function setupEnvironmentInfo() {
const {browser, platform} = await api.getEnvironmentInfo();
document.documentElement.dataset.browser = browser;
document.documentElement.dataset.operatingSystem = platform.os;
}
let ankiController = null;
let ankiTemplatesController = null;
let dictionaryController = null;
let genericSettingController = null;
2019-12-01 21:18:29 +00:00
async function onReady() {
api.forwardLogsToBackend();
2020-03-02 09:18:09 +00:00
await yomichan.prepare();
const settingsController = new SettingsController();
settingsController.prepare();
setupEnvironmentInfo();
2019-12-01 21:18:29 +00:00
showExtensionInformation();
const storageController = new StorageController();
storageController.prepare();
await settingsPopulateModifierKeys();
genericSettingController = new GenericSettingController();
genericSettingController.prepare();
new ClipboardPopupsController().prepare();
new PopupPreviewController().prepare();
new AudioController().prepare();
await (new ProfileController()).prepare();
dictionaryController = new DictionaryController(storageController);
dictionaryController.prepare();
ankiController = new AnkiController();
ankiController.prepare();
ankiTemplatesController = new AnkiTemplatesController(ankiController);
ankiTemplatesController.prepare();
new SettingsBackup().prepare();
2019-12-01 21:18:29 +00:00
yomichan.on('optionsUpdated', onOptionsUpdated);
2019-12-01 21:18:29 +00:00
}
$(document).ready(() => onReady());