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