aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-07-18 16:45:57 -0400
committerGitHub <noreply@github.com>2020-07-18 16:45:57 -0400
commite696dc84a857e60adcfa7feec44398765c430cac (patch)
tree78045c4d3b60c448b1402372bc2bdabb475b8c93 /ext
parentc6c0126394f4bf5862061aaa5be7f941ff957a07 (diff)
Refactor context-main.js (#671)
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/context-main.js62
1 files changed, 30 insertions, 32 deletions
diff --git a/ext/bg/js/context-main.js b/ext/bg/js/context-main.js
index 8718f583..6d17dbf7 100644
--- a/ext/bg/js/context-main.js
+++ b/ext/bg/js/context-main.js
@@ -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 toggle = document.querySelector('#enable-search');
+ toggle.checked = options.general.enable;
+ toggle.addEventListener('change', () => api.commandExec('toggle'), false);
- const toggle2 = document.querySelector('#enable-search2');
- toggle2.checked = options.general.enable;
- toggle2.addEventListener('change', () => api.commandExec('toggle'), false);
+ 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);
- });
+ 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/');
})();