Refactor context-main.js (#671)

This commit is contained in:
toasted-nutbread 2020-07-18 16:45:57 -04:00 committed by GitHub
parent c6c0126394
commit e696dc84a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,11 +19,10 @@
* api * api
*/ */
function showExtensionInfo() { function showExtensionInfo(manifest) {
const node = document.getElementById('extension-info'); const node = document.getElementById('extension-info');
if (node === null) { return; } if (node === null) { return; }
const manifest = chrome.runtime.getManifest();
node.textContent = `${manifest.name} v${manifest.version}`; node.textContent = `${manifest.name} v${manifest.version}`;
} }
@ -49,44 +48,43 @@ function setupButtonEvents(selector, command, url) {
} }
} }
async function mainInner() { async function setupEnvironment() {
api.forwardLogsToBackend(); // Firefox mobile opens this page as a full webpage.
await yomichan.ready(); const {browser} = await api.getEnvironmentInfo();
document.documentElement.dataset.mode = (browser === 'firefox-mobile' ? 'full' : 'mini');
await api.logIndicatorClear(); }
showExtensionInfo();
api.getEnvironmentInfo().then(({browser}) => {
// Firefox mobile opens this page as a full webpage.
document.documentElement.dataset.mode = (browser === 'firefox-mobile' ? 'full' : 'mini');
});
const manifest = chrome.runtime.getManifest();
setupButtonEvents('.action-open-search', 'search', chrome.runtime.getURL('/bg/search.html'));
setupButtonEvents('.action-open-options', 'options', chrome.runtime.getURL(manifest.options_ui.page));
setupButtonEvents('.action-open-help', 'help', 'https://foosoft.net/projects/yomichan/');
async function setupOptions() {
const optionsContext = { const optionsContext = {
depth: 0, depth: 0,
url: window.location.href url: window.location.href
}; };
api.optionsGet(optionsContext).then((options) => { const options = await api.optionsGet(optionsContext);
const toggle = document.querySelector('#enable-search');
toggle.checked = options.general.enable;
toggle.addEventListener('change', () => api.commandExec('toggle'), false);
const toggle2 = document.querySelector('#enable-search2'); const toggle = document.querySelector('#enable-search');
toggle2.checked = options.general.enable; toggle.checked = options.general.enable;
toggle2.addEventListener('change', () => api.commandExec('toggle'), false); toggle.addEventListener('change', () => api.commandExec('toggle'), false);
setTimeout(() => { const toggle2 = document.querySelector('#enable-search2');
document.body.dataset.loaded = 'true'; toggle2.checked = options.general.enable;
}, 10); toggle2.addEventListener('change', () => api.commandExec('toggle'), false);
});
setTimeout(() => {
document.body.dataset.loaded = 'true';
}, 10);
} }
(async () => { (async () => {
window.addEventListener('DOMContentLoaded', mainInner, false); api.forwardLogsToBackend();
await yomichan.ready();
const manifest = chrome.runtime.getManifest();
api.logIndicatorClear();
showExtensionInfo(manifest);
setupEnvironment();
setupOptions();
setupButtonEvents('.action-open-search', 'search', chrome.runtime.getURL('/bg/search.html'));
setupButtonEvents('.action-open-options', 'options', chrome.runtime.getURL(manifest.options_ui.page));
setupButtonEvents('.action-open-help', 'help', 'https://foosoft.net/projects/yomichan/');
})(); })();