HTML page script refactoring (#2162)

* Move some common functionality

* Move setupEnvironmentInfo into ExtensionContentController

* Move background/environment.js to extension/environment.js
This commit is contained in:
toasted-nutbread 2022-05-28 21:57:25 -04:00 committed by GitHub
parent 4e4fa49b0b
commit 0d82c52a76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 159 additions and 96 deletions

View File

@ -220,7 +220,6 @@
"ext/js/yomichan.js", "ext/js/yomichan.js",
"ext/js/accessibility/accessibility-controller.js", "ext/js/accessibility/accessibility-controller.js",
"ext/js/background/backend.js", "ext/js/background/backend.js",
"ext/js/background/environment.js",
"ext/js/background/profile-conditions-util.js", "ext/js/background/profile-conditions-util.js",
"ext/js/background/request-builder.js", "ext/js/background/request-builder.js",
"ext/js/background/script-manager.js", "ext/js/background/script-manager.js",
@ -235,6 +234,7 @@
"ext/js/data/permissions-util.js", "ext/js/data/permissions-util.js",
"ext/js/data/sandbox/string-util.js", "ext/js/data/sandbox/string-util.js",
"ext/js/dom/simple-dom-parser.js", "ext/js/dom/simple-dom-parser.js",
"ext/js/extension/environment.js",
"ext/js/general/cache-map.js", "ext/js/general/cache-map.js",
"ext/js/general/object-property-accessor.js", "ext/js/general/object-property-accessor.js",
"ext/js/general/regex-util.js", "ext/js/general/regex-util.js",

View File

@ -25,7 +25,6 @@
<script src="/js/accessibility/accessibility-controller.js"></script> <script src="/js/accessibility/accessibility-controller.js"></script>
<script src="/js/background/backend.js"></script> <script src="/js/background/backend.js"></script>
<script src="/js/background/environment.js"></script>
<script src="/js/background/profile-conditions-util.js"></script> <script src="/js/background/profile-conditions-util.js"></script>
<script src="/js/background/request-builder.js"></script> <script src="/js/background/request-builder.js"></script>
<script src="/js/background/script-manager.js"></script> <script src="/js/background/script-manager.js"></script>
@ -40,6 +39,7 @@
<script src="/js/data/permissions-util.js"></script> <script src="/js/data/permissions-util.js"></script>
<script src="/js/data/sandbox/string-util.js"></script> <script src="/js/data/sandbox/string-util.js"></script>
<script src="/js/dom/native-simple-dom-parser.js"></script> <script src="/js/dom/native-simple-dom-parser.js"></script>
<script src="/js/extension/environment.js"></script>
<script src="/js/general/cache-map.js"></script> <script src="/js/general/cache-map.js"></script>
<script src="/js/general/object-property-accessor.js"></script> <script src="/js/general/object-property-accessor.js"></script>
<script src="/js/general/regex-util.js"></script> <script src="/js/general/regex-util.js"></script>

View File

@ -0,0 +1,131 @@
/*
* Copyright (C) 2022 Yomichan Authors
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* global
* Environment
*/
class ExtensionContentController {
prepare() {
this._prepareSpecialUrls();
this._prepareExtensionIdExamples();
this._prepareEnvironmentInfo();
}
// Private
async _prepareEnvironmentInfo() {
const {dataset} = document.documentElement;
const {manifest_version: manifestVersion} = chrome.runtime.getManifest();
dataset.manifestVersion = `${manifestVersion}`;
const environment = new Environment();
await environment.prepare();
const {browser, platform} = environment.getInfo();
dataset.browser = browser;
dataset.os = platform.os;
}
_prepareExtensionIdExamples() {
const nodes = document.querySelectorAll('.extension-id-example');
let url = '';
try {
url = chrome.runtime.getURL('/');
} catch (e) {
// NOP
}
for (const node of nodes) {
node.textContent = url;
}
}
_prepareSpecialUrls() {
const nodes = document.querySelectorAll('[data-special-url]');
if (nodes.length === 0) { return; }
let extensionId = '';
try {
extensionId = chrome.runtime.id;
} catch (e) {
// NOP
}
const idPattern = /\{id\}/g;
const onSpecialUrlLinkClick = this._onSpecialUrlLinkClick.bind(this);
const onSpecialUrlLinkMouseDown = this._onSpecialUrlLinkMouseDown.bind(this);
for (const node of nodes) {
let {specialUrl} = node.dataset;
if (typeof specialUrl !== 'string') { specialUrl = ''; }
node.dataset.specialUrl = specialUrl.replace(idPattern, extensionId);
node.addEventListener('click', onSpecialUrlLinkClick, false);
node.addEventListener('auxclick', onSpecialUrlLinkClick, false);
node.addEventListener('mousedown', onSpecialUrlLinkMouseDown, false);
}
}
_onSpecialUrlLinkClick(e) {
switch (e.button) {
case 0:
case 1:
e.preventDefault();
this._createTab(e.currentTarget.dataset.specialUrl, true);
break;
}
}
_onSpecialUrlLinkMouseDown(e) {
switch (e.button) {
case 0:
case 1:
e.preventDefault();
break;
}
}
async _createTab(url, useOpener) {
let openerTabId;
if (useOpener) {
try {
const tab = await new Promise((resolve, reject) => {
chrome.tabs.getCurrent((result) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(result);
}
});
});
openerTabId = tab.id;
} catch (e) {
// NOP
}
}
return await new Promise((resolve, reject) => {
chrome.tabs.create({url, openerTabId}, (tab2) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(tab2);
}
});
});
}
}

View File

@ -17,16 +17,15 @@
/* global /* global
* DocumentFocusController * DocumentFocusController
* ExtensionContentController
*/ */
function setupEnvironmentInfo() {
const {manifest_version: manifestVersion} = chrome.runtime.getManifest();
document.documentElement.dataset.manifestVersion = `${manifestVersion}`;
}
(() => { (() => {
const documentFocusController = new DocumentFocusController(); const documentFocusController = new DocumentFocusController();
documentFocusController.prepare(); documentFocusController.prepare();
const extensionContentController = new ExtensionContentController();
extensionContentController.prepare();
document.documentElement.dataset.loaded = 'true'; document.documentElement.dataset.loaded = 'true';
setupEnvironmentInfo();
})(); })();

View File

@ -17,6 +17,7 @@
/* global /* global
* DocumentFocusController * DocumentFocusController
* ExtensionContentController
* ModalController * ModalController
* PermissionsOriginController * PermissionsOriginController
* PermissionsToggleController * PermissionsToggleController
@ -66,19 +67,11 @@ function setupPermissionsToggles() {
const documentFocusController = new DocumentFocusController(); const documentFocusController = new DocumentFocusController();
documentFocusController.prepare(); documentFocusController.prepare();
const extensionContentController = new ExtensionContentController();
extensionContentController.prepare();
setupPermissionsToggles(); setupPermissionsToggles();
for (const node of document.querySelectorAll('.extension-id-example')) {
node.textContent = chrome.runtime.getURL('/');
}
const extensionId = chrome.runtime.id;
const idPattern = /\{id\}/g;
for (const node of document.querySelectorAll('.extension-settings-link[data-special-url]')) {
const {specialUrl} = node.dataset;
node.dataset.specialUrl = `${specialUrl}`.replace(idPattern, extensionId);
}
await yomichan.prepare(); await yomichan.prepare();
setupEnvironmentInfo(); setupEnvironmentInfo();

View File

@ -55,14 +55,6 @@ class SettingsDisplayController {
node.addEventListener('keydown', onInputTabActionKeyDown, false); node.addEventListener('keydown', onInputTabActionKeyDown, false);
} }
const onSpecialUrlLinkClick = this._onSpecialUrlLinkClick.bind(this);
const onSpecialUrlLinkMouseDown = this._onSpecialUrlLinkMouseDown.bind(this);
for (const node of document.querySelectorAll('[data-special-url]')) {
node.addEventListener('click', onSpecialUrlLinkClick, false);
node.addEventListener('auxclick', onSpecialUrlLinkClick, false);
node.addEventListener('mousedown', onSpecialUrlLinkMouseDown, false);
}
for (const node of document.querySelectorAll('.defer-load-iframe')) { for (const node of document.querySelectorAll('.defer-load-iframe')) {
this._setupDeferLoadIframe(node); this._setupDeferLoadIframe(node);
} }
@ -229,57 +221,6 @@ class SettingsDisplayController {
} }
} }
_onSpecialUrlLinkClick(e) {
switch (e.button) {
case 0:
case 1:
e.preventDefault();
this._createTab(e.currentTarget.dataset.specialUrl, true);
break;
}
}
_onSpecialUrlLinkMouseDown(e) {
switch (e.button) {
case 0:
case 1:
e.preventDefault();
break;
}
}
async _createTab(url, useOpener) {
let openerTabId;
if (useOpener) {
try {
const tab = await new Promise((resolve, reject) => {
chrome.tabs.getCurrent((result) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(result);
}
});
});
openerTabId = tab.id;
} catch (e) {
// NOP
}
}
return await new Promise((resolve, reject) => {
chrome.tabs.create({url, openerTabId}, (tab2) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve(tab2);
}
});
});
}
_updateScrollTarget() { _updateScrollTarget() {
const hash = window.location.hash; const hash = window.location.hash;
if (!hash.startsWith('#!')) { return; } if (!hash.startsWith('#!')) { return; }

View File

@ -24,7 +24,7 @@
* DictionaryController * DictionaryController
* DictionaryImportController * DictionaryImportController
* DocumentFocusController * DocumentFocusController
* Environment * ExtensionContentController
* ExtensionKeyboardShortcutController * ExtensionKeyboardShortcutController
* GenericSettingController * GenericSettingController
* KeyboardShortcutController * KeyboardShortcutController
@ -48,19 +48,6 @@
* TranslationTextReplacementsController * TranslationTextReplacementsController
*/ */
async function setupEnvironmentInfo() {
const {dataset} = document.documentElement;
const {manifest_version: manifestVersion} = chrome.runtime.getManifest();
dataset.manifestVersion = `${manifestVersion}`;
const environment = new Environment();
await environment.prepare();
const {browser, platform} = environment.getInfo();
dataset.browser = browser;
dataset.os = platform.os;
}
async function setupGenericSettingsController(genericSettingController) { async function setupGenericSettingsController(genericSettingController) {
await genericSettingController.prepare(); await genericSettingController.prepare();
await genericSettingController.refresh(); await genericSettingController.refresh();
@ -71,11 +58,12 @@ async function setupGenericSettingsController(genericSettingController) {
const documentFocusController = new DocumentFocusController(); const documentFocusController = new DocumentFocusController();
documentFocusController.prepare(); documentFocusController.prepare();
const extensionContentController = new ExtensionContentController();
extensionContentController.prepare();
const statusFooter = new StatusFooter(document.querySelector('.status-footer-container')); const statusFooter = new StatusFooter(document.querySelector('.status-footer-container'));
statusFooter.prepare(); statusFooter.prepare();
setupEnvironmentInfo();
let prepareTimer = setTimeout(() => { let prepareTimer = setTimeout(() => {
prepareTimer = null; prepareTimer = null;
document.documentElement.dataset.loadingStalled = 'true'; document.documentElement.dataset.loadingStalled = 'true';

View File

@ -19,6 +19,7 @@
* DictionaryController * DictionaryController
* DictionaryImportController * DictionaryImportController
* DocumentFocusController * DocumentFocusController
* ExtensionContentController
* GenericSettingController * GenericSettingController
* ModalController * ModalController
* ScanInputsSimpleController * ScanInputsSimpleController
@ -45,6 +46,9 @@ async function setupGenericSettingsController(genericSettingController) {
const documentFocusController = new DocumentFocusController(); const documentFocusController = new DocumentFocusController();
documentFocusController.prepare(); documentFocusController.prepare();
const extensionContentController = new ExtensionContentController();
extensionContentController.prepare();
const statusFooter = new StatusFooter(document.querySelector('.status-footer-container')); const statusFooter = new StatusFooter(document.querySelector('.status-footer-container'));
statusFooter.prepare(); statusFooter.prepare();

View File

@ -153,6 +153,8 @@ THE SOFTWARE.
<!-- Scripts --> <!-- Scripts -->
<script src="/js/dom/document-focus-controller.js"></script> <script src="/js/dom/document-focus-controller.js"></script>
<script src="/js/extension/environment.js"></script>
<script src="/js/pages/common/extension-content-controller.js"></script>
<script src="/js/pages/generic-page-main.js"></script> <script src="/js/pages/generic-page-main.js"></script>

View File

@ -280,6 +280,8 @@
<script src="/js/dom/panel-element.js"></script> <script src="/js/dom/panel-element.js"></script>
<script src="/js/dom/popup-menu.js"></script> <script src="/js/dom/popup-menu.js"></script>
<script src="/js/dom/selector-observer.js"></script> <script src="/js/dom/selector-observer.js"></script>
<script src="/js/extension/environment.js"></script>
<script src="/js/pages/common/extension-content-controller.js"></script>
<script src="/js/pages/settings/modal.js"></script> <script src="/js/pages/settings/modal.js"></script>
<script src="/js/pages/settings/modal-controller.js"></script> <script src="/js/pages/settings/modal-controller.js"></script>
<script src="/js/pages/settings/permissions-origin-controller.js"></script> <script src="/js/pages/settings/permissions-origin-controller.js"></script>

View File

@ -3623,7 +3623,6 @@
<script src="/js/yomichan.js"></script> <script src="/js/yomichan.js"></script>
<script src="/js/background/environment.js"></script>
<script src="/js/comm/anki.js"></script> <script src="/js/comm/anki.js"></script>
<script src="/js/comm/api.js"></script> <script src="/js/comm/api.js"></script>
<script src="/js/comm/cross-frame-api.js"></script> <script src="/js/comm/cross-frame-api.js"></script>
@ -3641,6 +3640,7 @@
<script src="/js/dom/panel-element.js"></script> <script src="/js/dom/panel-element.js"></script>
<script src="/js/dom/popup-menu.js"></script> <script src="/js/dom/popup-menu.js"></script>
<script src="/js/dom/selector-observer.js"></script> <script src="/js/dom/selector-observer.js"></script>
<script src="/js/extension/environment.js"></script>
<script src="/js/general/cache-map.js"></script> <script src="/js/general/cache-map.js"></script>
<script src="/js/general/object-property-accessor.js"></script> <script src="/js/general/object-property-accessor.js"></script>
<script src="/js/general/task-accumulator.js"></script> <script src="/js/general/task-accumulator.js"></script>
@ -3651,6 +3651,7 @@
<script src="/js/language/sandbox/japanese-util.js"></script> <script src="/js/language/sandbox/japanese-util.js"></script>
<script src="/js/media/audio-system.js"></script> <script src="/js/media/audio-system.js"></script>
<script src="/js/media/text-to-speech-audio.js"></script> <script src="/js/media/text-to-speech-audio.js"></script>
<script src="/js/pages/common/extension-content-controller.js"></script>
<script src="/js/pages/settings/anki-controller.js"></script> <script src="/js/pages/settings/anki-controller.js"></script>
<script src="/js/pages/settings/anki-templates-controller.js"></script> <script src="/js/pages/settings/anki-templates-controller.js"></script>
<script src="/js/pages/settings/audio-controller.js"></script> <script src="/js/pages/settings/audio-controller.js"></script>

View File

@ -22,7 +22,6 @@ self.importScripts(
'/js/yomichan.js', '/js/yomichan.js',
'/js/accessibility/accessibility-controller.js', '/js/accessibility/accessibility-controller.js',
'/js/background/backend.js', '/js/background/backend.js',
'/js/background/environment.js',
'/js/background/profile-conditions-util.js', '/js/background/profile-conditions-util.js',
'/js/background/request-builder.js', '/js/background/request-builder.js',
'/js/background/script-manager.js', '/js/background/script-manager.js',
@ -37,6 +36,7 @@ self.importScripts(
'/js/data/permissions-util.js', '/js/data/permissions-util.js',
'/js/data/sandbox/string-util.js', '/js/data/sandbox/string-util.js',
'/js/dom/simple-dom-parser.js', '/js/dom/simple-dom-parser.js',
'/js/extension/environment.js',
'/js/general/cache-map.js', '/js/general/cache-map.js',
'/js/general/object-property-accessor.js', '/js/general/object-property-accessor.js',
'/js/general/regex-util.js', '/js/general/regex-util.js',

View File

@ -401,12 +401,14 @@
<script src="/js/dom/panel-element.js"></script> <script src="/js/dom/panel-element.js"></script>
<script src="/js/dom/popup-menu.js"></script> <script src="/js/dom/popup-menu.js"></script>
<script src="/js/dom/selector-observer.js"></script> <script src="/js/dom/selector-observer.js"></script>
<script src="/js/extension/environment.js"></script>
<script src="/js/general/cache-map.js"></script> <script src="/js/general/cache-map.js"></script>
<script src="/js/general/object-property-accessor.js"></script> <script src="/js/general/object-property-accessor.js"></script>
<script src="/js/general/task-accumulator.js"></script> <script src="/js/general/task-accumulator.js"></script>
<script src="/js/input/hotkey-util.js"></script> <script src="/js/input/hotkey-util.js"></script>
<script src="/js/language/dictionary-importer-media-loader.js"></script> <script src="/js/language/dictionary-importer-media-loader.js"></script>
<script src="/js/language/dictionary-worker.js"></script> <script src="/js/language/dictionary-worker.js"></script>
<script src="/js/pages/common/extension-content-controller.js"></script>
<script src="/js/pages/settings/dictionary-controller.js"></script> <script src="/js/pages/settings/dictionary-controller.js"></script>
<script src="/js/pages/settings/dictionary-import-controller.js"></script> <script src="/js/pages/settings/dictionary-import-controller.js"></script>
<script src="/js/pages/settings/generic-setting-controller.js"></script> <script src="/js/pages/settings/generic-setting-controller.js"></script>