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
|
2021-01-18 20:23:49 +00:00
|
|
|
* HotkeyHelpController
|
2021-02-11 23:55:09 +00:00
|
|
|
* PermissionsUtil
|
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;
|
2021-02-11 23:55:09 +00:00
|
|
|
this._permissionsUtil = new PermissionsUtil();
|
2020-11-19 23:37:02 +00:00
|
|
|
}
|
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-02-13 04:03:15 +00:00
|
|
|
this._setupButtonEvents('.action-open-search', 'openSearchPage', chrome.runtime.getURL('/search.html'));
|
|
|
|
this._setupButtonEvents('.action-open-info', 'openInfoPage', chrome.runtime.getURL('/info.html'));
|
2020-11-19 23:37:02 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
const optionsFull = await yomichan.api.optionsGetFull();
|
2020-11-19 23:37:02 +00:00
|
|
|
this._optionsFull = optionsFull;
|
2019-10-13 22:23:59 +00:00
|
|
|
|
2021-01-18 20:23:49 +00:00
|
|
|
this._setupHotkeys();
|
|
|
|
|
2021-03-15 02:51:48 +00:00
|
|
|
const optionsPageUrl = manifest.options_ui.page;
|
2021-01-18 19:22:48 +00:00
|
|
|
this._setupButtonEvents('.action-open-settings', 'openSettingsPage', chrome.runtime.getURL(optionsPageUrl));
|
2021-02-13 04:03:15 +00:00
|
|
|
this._setupButtonEvents('.action-open-permissions', null, chrome.runtime.getURL('/permissions.html'));
|
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) {
|
2021-02-11 23:55:09 +00:00
|
|
|
if (typeof command === 'string') {
|
|
|
|
node.addEventListener('click', (e) => {
|
|
|
|
if (e.button !== 0) { return; }
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.api.commandExec(command, {mode: e.ctrlKey ? 'newTab' : 'existingOrNewTab'});
|
2021-02-11 23:55:09 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}, false);
|
|
|
|
node.addEventListener('auxclick', (e) => {
|
|
|
|
if (e.button !== 1) { return; }
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.api.commandExec(command, {mode: 'newTab'});
|
2021-02-11 23:55:09 +00:00
|
|
|
e.preventDefault();
|
|
|
|
}, false);
|
|
|
|
}
|
2020-11-19 23:37:02 +00:00
|
|
|
|
|
|
|
if (typeof url === 'string') {
|
|
|
|
node.href = url;
|
|
|
|
node.target = '_blank';
|
|
|
|
node.rel = 'noopener';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setupEnvironment() {
|
2021-02-08 22:53:22 +00:00
|
|
|
const urlSearchParams = new URLSearchParams(location.search);
|
|
|
|
let mode = urlSearchParams.get('mode');
|
|
|
|
switch (mode) {
|
|
|
|
case 'full':
|
|
|
|
case 'mini':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
let tab;
|
|
|
|
try {
|
|
|
|
tab = await this._getCurrentTab();
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
mode = (tab ? 'full' : 'mini');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.documentElement.dataset.mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getCurrentTab() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.tabs.getCurrent((result) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
if (e) {
|
|
|
|
reject(new Error(e.message));
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-11-19 23:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_setupOptions({options}) {
|
|
|
|
const extensionEnabled = options.general.enable;
|
2021-02-14 20:53:35 +00:00
|
|
|
const onToggleChanged = () => yomichan.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);
|
|
|
|
}
|
2021-02-08 22:52:46 +00:00
|
|
|
this._updateDictionariesEnabledWarnings(options);
|
2021-02-11 23:55:09 +00:00
|
|
|
this._updatePermissionsWarnings(options);
|
2020-11-19 23:37:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:23:49 +00:00
|
|
|
async _setupHotkeys() {
|
|
|
|
const hotkeyHelpController = new HotkeyHelpController();
|
|
|
|
await hotkeyHelpController.prepare();
|
|
|
|
|
|
|
|
const {profiles, profileCurrent} = this._optionsFull;
|
|
|
|
const primaryProfile = (profileCurrent >= 0 && profileCurrent < profiles.length) ? profiles[profileCurrent] : null;
|
|
|
|
if (primaryProfile !== null) {
|
|
|
|
hotkeyHelpController.setOptions(primaryProfile.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
hotkeyHelpController.setupNode(document.documentElement);
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:37:02 +00:00
|
|
|
_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) {
|
2021-02-14 20:53:35 +00:00
|
|
|
return await yomichan.api.modifySettings(
|
2020-11-19 23:37:02 +00:00
|
|
|
[{
|
|
|
|
action: 'set',
|
|
|
|
path: 'profileCurrent',
|
|
|
|
value,
|
|
|
|
scope: 'global'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
2021-02-08 22:52:46 +00:00
|
|
|
|
|
|
|
async _updateDictionariesEnabledWarnings(options) {
|
|
|
|
const noDictionariesEnabledWarnings = document.querySelectorAll('.no-dictionaries-enabled-warning');
|
2021-02-14 20:53:35 +00:00
|
|
|
const dictionaries = await yomichan.api.getDictionaryInfo();
|
2021-02-08 22:52:46 +00:00
|
|
|
|
|
|
|
let enabledCount = 0;
|
|
|
|
for (const {title} of dictionaries) {
|
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(options.dictionaries, title) &&
|
|
|
|
options.dictionaries[title].enabled
|
|
|
|
) {
|
|
|
|
++enabledCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasEnabledDictionary = (enabledCount > 0);
|
|
|
|
for (const node of noDictionariesEnabledWarnings) {
|
|
|
|
node.hidden = hasEnabledDictionary;
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 23:55:09 +00:00
|
|
|
|
|
|
|
async _updatePermissionsWarnings(options) {
|
|
|
|
const permissions = await this._permissionsUtil.getAllPermissions();
|
|
|
|
if (this._permissionsUtil.hasRequiredPermissionsForOptions(permissions, options)) { return; }
|
|
|
|
|
|
|
|
const warnings = document.querySelectorAll('.action-open-permissions,.permissions-required-warning');
|
|
|
|
for (const node of warnings) {
|
|
|
|
node.hidden = false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 01:42:20 +00:00
|
|
|
(async () => {
|
2021-02-14 20:19:31 +00:00
|
|
|
await yomichan.prepare();
|
2020-07-18 20:45:57 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.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
|
|
|
})();
|