diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | ext/bg/background.html | 3 | ||||
-rw-r--r-- | ext/bg/context.html | 2 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 12 | ||||
-rw-r--r-- | ext/bg/js/background-main.js | 25 | ||||
-rw-r--r-- | ext/bg/js/context-main.js (renamed from ext/bg/js/context.js) | 8 | ||||
-rw-r--r-- | ext/bg/js/search-main.js (renamed from ext/bg/js/search-frontend.js) | 12 | ||||
-rw-r--r-- | ext/bg/js/search.js | 8 | ||||
-rw-r--r-- | ext/bg/js/settings/popup-preview-frame-main.js | 25 | ||||
-rw-r--r-- | ext/bg/js/settings/popup-preview-frame.js | 11 | ||||
-rw-r--r-- | ext/bg/search.html | 3 | ||||
-rw-r--r-- | ext/bg/settings-popup-preview.html | 2 | ||||
-rw-r--r-- | ext/fg/float.html | 2 | ||||
-rw-r--r-- | ext/fg/js/content-script-main.js (renamed from ext/fg/js/frontend-initialize.js) | 6 | ||||
-rw-r--r-- | ext/fg/js/float-main.js (renamed from ext/fg/js/popup-nested.js) | 9 | ||||
-rw-r--r-- | ext/fg/js/float.js | 2 | ||||
-rw-r--r-- | ext/manifest.json | 2 | ||||
-rw-r--r-- | ext/mixed/js/dynamic-loader-sentinel.js | 18 | ||||
-rw-r--r-- | ext/mixed/js/dynamic-loader.js | 82 |
19 files changed, 189 insertions, 45 deletions
@@ -82,7 +82,7 @@ primary language is not English, you may consider also importing the English ver * **[Innocent Corpus](https://forum.koohii.com/post-168613.html#pid168613)** (Term and Kanji frequencies across 5000+ novels) * [innocent\_corpus.zip](https://foosoft.net/projects/yomichan/dl/dict/innocent_corpus.zip) * **[Kanjium](https://github.com/mifunetoshiro/kanjium)** (Pitch dictionary, see [related project page](https://github.com/toasted-nutbread/yomichan-pitch-accent-dictionary) for details) - * [kanjium_pitch_accents.zip](https://foosoft.net/projects/yomichan/dl/dict/kanjium_pitch_accents.zip) (currently available for **testing version only**) + * [kanjium_pitch_accents.zip](https://foosoft.net/projects/yomichan/dl/dict/kanjium_pitch_accents.zip) ## Basic Usage ## diff --git a/ext/bg/background.html b/ext/bg/background.html index f1006f8d..3446d9ce 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -24,6 +24,7 @@ <script src="/bg/js/anki.js"></script> <script src="/bg/js/anki-note-builder.js"></script> + <script src="/bg/js/backend.js"></script> <script src="/bg/js/mecab.js"></script> <script src="/bg/js/audio-uri-builder.js"></script> <script src="/bg/js/backend-api-forwarder.js"></script> @@ -45,6 +46,6 @@ <script src="/bg/js/util.js"></script> <script src="/mixed/js/audio-system.js"></script> - <script src="/bg/js/backend.js"></script> + <script src="/bg/js/background-main.js"></script> </body> </html> diff --git a/ext/bg/context.html b/ext/bg/context.html index 0e50ed7c..394869b1 100644 --- a/ext/bg/context.html +++ b/ext/bg/context.html @@ -185,6 +185,6 @@ <script src="/bg/js/options.js"></script> <script src="/bg/js/util.js"></script> - <script src="/bg/js/context.js"></script> + <script src="/bg/js/context-main.js"></script> </body> </html> diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 8a19203f..693a9ad6 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -496,7 +496,7 @@ class Backend { async _onApiDefinitionAdd({definition, mode, context, details, optionsContext}) { const options = this.getOptions(optionsContext); - const templates = this.defaultAnkiFieldTemplates; + const templates = this._getTemplates(options); if (mode !== 'kanji') { const {customSourceUrl} = options.audio; @@ -522,7 +522,7 @@ class Backend { async _onApiDefinitionsAddable({definitions, modes, context, optionsContext}) { const options = this.getOptions(optionsContext); - const templates = this.defaultAnkiFieldTemplates; + const templates = this._getTemplates(options); const states = []; try { @@ -945,6 +945,11 @@ class Backend { return handlebarsRenderDynamic(template, data); } + _getTemplates(options) { + const templates = options.anki.fieldTemplates; + return typeof templates === 'string' ? templates : this.defaultAnkiFieldTemplates; + } + static _getTabUrl(tab) { return new Promise((resolve) => { chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => { @@ -1054,6 +1059,3 @@ class Backend { } } } - -window.yomichanBackend = new Backend(); -window.yomichanBackend.prepare(); diff --git a/ext/bg/js/background-main.js b/ext/bg/js/background-main.js new file mode 100644 index 00000000..24117f4e --- /dev/null +++ b/ext/bg/js/background-main.js @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2020 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 + * Backend + */ + +(async () => { + window.yomichanBackend = new Backend(); + await window.yomichanBackend.prepare(); +})(); diff --git a/ext/bg/js/context.js b/ext/bg/js/context-main.js index e3d4ad4a..e2086a96 100644 --- a/ext/bg/js/context.js +++ b/ext/bg/js/context-main.js @@ -51,7 +51,7 @@ function setupButtonEvents(selector, command, url) { } } -window.addEventListener('DOMContentLoaded', async () => { +async function mainInner() { await yomichan.prepare(); showExtensionInfo(); @@ -86,4 +86,8 @@ window.addEventListener('DOMContentLoaded', async () => { } }, 10); }); -}); +} + +(async () => { + window.addEventListener('DOMContentLoaded', mainInner, false); +})(); diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-main.js index e534e771..38b6d99a 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-main.js @@ -16,6 +16,7 @@ */ /* global + * DisplaySearch * apiOptionsGet */ @@ -27,7 +28,7 @@ function injectSearchFrontend() { '/fg/js/popup.js', '/fg/js/popup-proxy-host.js', '/fg/js/frontend.js', - '/fg/js/frontend-initialize.js' + '/fg/js/content-script-main.js' ]; for (const src of scriptSrcs) { const node = document.querySelector(`script[src='${src}']`); @@ -51,9 +52,12 @@ function injectSearchFrontend() { } } -async function main() { +(async () => { await yomichan.prepare(); + const displaySearch = new DisplaySearch(); + await displaySearch.prepare(); + let optionsApplied = false; const applyOptions = async () => { @@ -74,6 +78,4 @@ async function main() { yomichan.on('optionsUpdated', applyOptions); await applyOptions(); -} - -main(); +})(); diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 006536c0..684ea6d3 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -73,12 +73,6 @@ class DisplaySearch extends Display { ]); } - static create() { - const instance = new DisplaySearch(); - instance.prepare(); - return instance; - } - async prepare() { try { await super.prepare(); @@ -377,5 +371,3 @@ class DisplaySearch extends Display { } } } - -DisplaySearch.instance = DisplaySearch.create(); diff --git a/ext/bg/js/settings/popup-preview-frame-main.js b/ext/bg/js/settings/popup-preview-frame-main.js new file mode 100644 index 00000000..86c2814c --- /dev/null +++ b/ext/bg/js/settings/popup-preview-frame-main.js @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2019-2020 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 + * SettingsPopupPreview + */ + +(async () => { + const instance = new SettingsPopupPreview(); + await instance.prepare(); +})(); diff --git a/ext/bg/js/settings/popup-preview-frame.js b/ext/bg/js/settings/popup-preview-frame.js index fba114e2..420a7c5a 100644 --- a/ext/bg/js/settings/popup-preview-frame.js +++ b/ext/bg/js/settings/popup-preview-frame.js @@ -41,12 +41,6 @@ class SettingsPopupPreview { ]); } - static create() { - const instance = new SettingsPopupPreview(); - instance.prepare(); - return instance; - } - async prepare() { // Setup events window.addEventListener('message', this.onMessage.bind(this), false); @@ -178,8 +172,3 @@ class SettingsPopupPreview { this.setInfoVisible(!this.popupShown); } } - -SettingsPopupPreview.instance = SettingsPopupPreview.create(); - - - diff --git a/ext/bg/search.html b/ext/bg/search.html index fe88e264..9a824776 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -94,6 +94,7 @@ <script src="/bg/js/search-query-parser.js"></script> <script src="/bg/js/clipboard-monitor.js"></script> <script src="/bg/js/search.js"></script> - <script src="/bg/js/search-frontend.js"></script> + + <script src="/bg/js/search-main.js"></script> </body> </html> diff --git a/ext/bg/settings-popup-preview.html b/ext/bg/settings-popup-preview.html index f33ecedf..66475b7c 100644 --- a/ext/bg/settings-popup-preview.html +++ b/ext/bg/settings-popup-preview.html @@ -129,5 +129,7 @@ <script src="/fg/js/popup-proxy-host.js"></script> <script src="/fg/js/frontend.js"></script> <script src="/bg/js/settings/popup-preview-frame.js"></script> + + <script src="/bg/js/settings/popup-preview-frame-main.js"></script> </body> </html> diff --git a/ext/fg/float.html b/ext/fg/float.html index c8ea9b67..07c3c9e6 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -57,6 +57,6 @@ <script src="/fg/js/float.js"></script> - <script src="/fg/js/popup-nested.js"></script> + <script src="/fg/js/float-main.js"></script> </body> </html> diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/content-script-main.js index 2df59e20..14285536 100644 --- a/ext/fg/js/frontend-initialize.js +++ b/ext/fg/js/content-script-main.js @@ -61,7 +61,7 @@ async function createPopupProxy(depth, id, parentFrameId, url) { return popup; } -async function main() { +(async () => { await yomichan.prepare(); const data = window.frontendInitializationData || {}; @@ -128,6 +128,4 @@ async function main() { window.addEventListener('fullscreenchange', applyOptions, false); await applyOptions(); -} - -main(); +})(); diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/float-main.js index c140f9c8..f056f707 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/float-main.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2020 Yomichan Authors + * Copyright (C) 2020 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 @@ -16,6 +16,7 @@ */ /* global + * DisplayFloat * apiOptionsGet */ @@ -26,7 +27,7 @@ function injectPopupNested() { '/fg/js/popup.js', '/fg/js/popup-proxy.js', '/fg/js/frontend.js', - '/fg/js/frontend-initialize.js' + '/fg/js/content-script-main.js' ]; for (const src of scriptSrcs) { const script = document.createElement('script'); @@ -65,3 +66,7 @@ async function popupNestedInitialize(id, depth, parentFrameId, url) { await applyOptions(); } + +(async () => { + new DisplayFloat(); +})(); diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 5c2c50c2..18d15a72 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -189,5 +189,3 @@ class DisplayFloat extends Display { } } } - -DisplayFloat.instance = new DisplayFloat(); diff --git a/ext/manifest.json b/ext/manifest.json index d383dab0..4f35b03c 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -31,7 +31,7 @@ "fg/js/popup-proxy.js", "fg/js/popup-proxy-host.js", "fg/js/frontend.js", - "fg/js/frontend-initialize.js" + "fg/js/content-script-main.js" ], "match_about_blank": true, "all_frames": true diff --git a/ext/mixed/js/dynamic-loader-sentinel.js b/ext/mixed/js/dynamic-loader-sentinel.js new file mode 100644 index 00000000..f783bdb7 --- /dev/null +++ b/ext/mixed/js/dynamic-loader-sentinel.js @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2020 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/>. + */ + +yomichan.trigger('dynamicLoaderSentinel', {script: document.currentScript}); diff --git a/ext/mixed/js/dynamic-loader.js b/ext/mixed/js/dynamic-loader.js new file mode 100644 index 00000000..29672d36 --- /dev/null +++ b/ext/mixed/js/dynamic-loader.js @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2020 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/>. + */ + +const dynamicLoader = (() => { + function loadStyles(urls) { + const parent = document.head; + for (const url of urls) { + const node = parent.querySelector(`link[href='${escapeCSSAttribute(url)}']`); + if (node !== null) { continue; } + + const style = document.createElement('link'); + style.rel = 'stylesheet'; + style.type = 'text/css'; + style.href = url; + parent.appendChild(style); + } + } + + function loadScripts(urls) { + return new Promise((resolve) => { + const parent = document.body; + for (const url of urls) { + const node = parent.querySelector(`script[src='${escapeCSSAttribute(url)}']`); + if (node !== null) { continue; } + + const script = document.createElement('script'); + script.async = false; + script.src = url; + parent.appendChild(script); + } + + loadScriptSentinel(resolve); + }); + } + + function loadScriptSentinel(resolve, reject) { + const parent = document.body; + const script = document.createElement('script'); + + const sentinelEventName = 'dynamicLoaderSentinel'; + const sentinelEventCallback = (e) => { + if (e.script !== script) { return; } + yomichan.off(sentinelEventName, sentinelEventCallback); + parent.removeChild(script); + resolve(); + }; + yomichan.on(sentinelEventName, sentinelEventCallback); + + try { + script.async = false; + script.src = '/mixed/js/dynamic-loader-sentinel.js'; + parent.appendChild(script); + } catch (e) { + yomichan.off(sentinelEventName, sentinelEventCallback); + reject(e); + } + } + + function escapeCSSAttribute(value) { + return value.replace(/['\\]/g, (character) => `\\${character}`); + } + + + return { + loadStyles, + loadScripts + }; +})(); |