2017-03-02 05:35:29 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2017-2021 Yomichan Authors
|
2017-03-02 05:35:29 +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/>.
|
2017-03-02 05:35:29 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2017-03-03 05:01:49 +00:00
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
class DisplayController {
|
|
|
|
constructor() {
|
|
|
|
this._optionsFull = null;
|
|
|
|
}
|
2019-10-13 22:23:59 +00:00
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
async prepare() {
|
|
|
|
const manifest = chrome.runtime.getManifest();
|
|
|
|
|
|
|
|
this._showExtensionInfo(manifest);
|
|
|
|
this._setupEnvironment();
|
2021-01-16 16:33:34 +00:00
|
|
|
this._setupButtonEvents('.action-open-search', 'openSearchPage', chrome.runtime.getURL('/bg/search.html'));
|
|
|
|
this._setupButtonEvents('.action-open-help', 'openHelpPage', chrome.runtime.getURL('/bg/info.html'));
|
2020-11-19 23:37:02 +00:00
|
|
|
|
|
|
|
const optionsFull = await api.optionsGetFull();
|
|
|
|
this._optionsFull = optionsFull;
|
2019-10-13 22:23:59 +00:00
|
|
|
|
2020-12-07 02:17:05 +00:00
|
|
|
const optionsPageUrl = optionsFull.global.useSettingsV2 ? '/bg/settings2.html' : manifest.options_ui.page;
|
2021-01-16 16:33:34 +00:00
|
|
|
this._setupButtonEvents('.action-open-options', 'openSettingsPage', chrome.runtime.getURL(optionsPageUrl));
|
2020-12-07 02:17:05 +00:00
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
const {profiles, profileCurrent} = optionsFull;
|
|
|
|
const primaryProfile = (profileCurrent >= 0 && profileCurrent < profiles.length) ? profiles[profileCurrent] : null;
|
|
|
|
if (primaryProfile !== null) {
|
|
|
|
this._setupOptions(primaryProfile);
|
2019-11-29 00:58:17 +00:00
|
|
|
}
|
2020-11-19 23:37:02 +00:00
|
|
|
|
2020-11-19 23:49:30 +00:00
|
|
|
document.querySelector('.action-select-profile').hidden = (profiles.length <= 1);
|
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
this._updateProfileSelect(profiles, profileCurrent);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
document.body.dataset.loaded = 'true';
|
|
|
|
}, 10);
|
2019-10-20 02:54:58 +00:00
|
|
|
}
|
2019-10-20 02:30:16 +00:00
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_showExtensionInfo(manifest) {
|
|
|
|
const node = document.getElementById('extension-info');
|
|
|
|
if (node === null) { return; }
|
|
|
|
|
|
|
|
node.textContent = `${manifest.name} v${manifest.version}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setupButtonEvents(selector, command, url) {
|
|
|
|
const nodes = document.querySelectorAll(selector);
|
|
|
|
for (const node of nodes) {
|
|
|
|
node.addEventListener('click', (e) => {
|
|
|
|
if (e.button !== 0) { return; }
|
|
|
|
api.commandExec(command, {mode: e.ctrlKey ? 'newTab' : 'existingOrNewTab'});
|
|
|
|
e.preventDefault();
|
|
|
|
}, false);
|
|
|
|
node.addEventListener('auxclick', (e) => {
|
|
|
|
if (e.button !== 1) { return; }
|
|
|
|
api.commandExec(command, {mode: 'newTab'});
|
|
|
|
e.preventDefault();
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
if (typeof url === 'string') {
|
|
|
|
node.href = url;
|
|
|
|
node.target = '_blank';
|
|
|
|
node.rel = 'noopener';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setupEnvironment() {
|
|
|
|
// Firefox mobile opens this page as a full webpage.
|
|
|
|
const {browser} = await api.getEnvironmentInfo();
|
|
|
|
document.documentElement.dataset.mode = (browser === 'firefox-mobile' ? 'full' : 'mini');
|
|
|
|
}
|
|
|
|
|
|
|
|
_setupOptions({options}) {
|
|
|
|
const extensionEnabled = options.general.enable;
|
2021-01-16 16:33:34 +00:00
|
|
|
const onToggleChanged = () => api.commandExec('toggleTextScanning');
|
2020-11-19 23:37:02 +00:00
|
|
|
for (const toggle of document.querySelectorAll('#enable-search,#enable-search2')) {
|
|
|
|
toggle.checked = extensionEnabled;
|
|
|
|
toggle.addEventListener('change', onToggleChanged, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateProfileSelect(profiles, profileCurrent) {
|
|
|
|
const select = document.querySelector('#profile-select');
|
|
|
|
const optionGroup = document.querySelector('#profile-select-option-group');
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (let i = 0, ii = profiles.length; i < ii; ++i) {
|
|
|
|
const {name} = profiles[i];
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.textContent = name;
|
|
|
|
option.value = `${i}`;
|
|
|
|
fragment.appendChild(option);
|
|
|
|
}
|
|
|
|
optionGroup.textContent = '';
|
|
|
|
optionGroup.appendChild(fragment);
|
|
|
|
select.value = `${profileCurrent}`;
|
|
|
|
|
|
|
|
select.addEventListener('change', this._onProfileSelectChange.bind(this), false);
|
|
|
|
}
|
2017-03-02 05:35:29 +00:00
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
_onProfileSelectChange(e) {
|
|
|
|
const value = parseInt(e.currentTarget.value, 10);
|
|
|
|
if (typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= this._optionsFull.profiles.length) {
|
|
|
|
this._setPrimaryProfileIndex(value);
|
|
|
|
}
|
2020-07-26 20:53:24 +00:00
|
|
|
}
|
2019-11-24 16:42:27 +00:00
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
async _setPrimaryProfileIndex(value) {
|
|
|
|
return await api.modifySettings(
|
|
|
|
[{
|
|
|
|
action: 'set',
|
|
|
|
path: 'profileCurrent',
|
|
|
|
value,
|
|
|
|
scope: 'global'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
2020-04-19 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 01:42:20 +00:00
|
|
|
(async () => {
|
2020-07-18 20:45:57 +00:00
|
|
|
api.forwardLogsToBackend();
|
2020-07-18 21:11:38 +00:00
|
|
|
await yomichan.backendReady();
|
2020-07-18 20:45:57 +00:00
|
|
|
|
|
|
|
api.logIndicatorClear();
|
2020-11-19 23:37:02 +00:00
|
|
|
|
|
|
|
const displayController = new DisplayController();
|
|
|
|
displayController.prepare();
|
2020-07-18 21:11:38 +00:00
|
|
|
|
|
|
|
yomichan.ready();
|
2020-04-23 01:42:20 +00:00
|
|
|
})();
|