summaryrefslogtreecommitdiff
path: root/ext/fg/js/frontend-initialize.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2020-04-16 17:30:09 -0700
committerAlex Yatskov <alex@foosoft.net>2020-04-16 17:30:09 -0700
commit93c4fb9eab2651da24fb816f20dcb967eae1437e (patch)
treedf8f6d5e04da7e6e78db4712d4d3d92c460454b9 /ext/fg/js/frontend-initialize.js
parent3b9a87b2ebe843e30536924639d6c14afef936cd (diff)
parent8c16a6e580bfdd70e27df1816ca90807062cf9b5 (diff)
Merge branch 'master' of https://github.com/FooSoft/yomichan
Diffstat (limited to 'ext/fg/js/frontend-initialize.js')
-rw-r--r--ext/fg/js/frontend-initialize.js130
1 files changed, 91 insertions, 39 deletions
diff --git a/ext/fg/js/frontend-initialize.js b/ext/fg/js/frontend-initialize.js
index 0a586ff9..2b942258 100644
--- a/ext/fg/js/frontend-initialize.js
+++ b/ext/fg/js/frontend-initialize.js
@@ -20,53 +20,105 @@
* Frontend
* PopupProxy
* PopupProxyHost
- * apiForward
+ * apiBroadcastTab
* apiOptionsGet
*/
+async function createIframePopupProxy(url, frameOffsetForwarder) {
+ const rootPopupInformationPromise = yomichan.getTemporaryListenerResult(
+ chrome.runtime.onMessage,
+ ({action, params}, {resolve}) => {
+ if (action === 'rootPopupInformation') {
+ resolve(params);
+ }
+ }
+ );
+ apiBroadcastTab('rootPopupRequestInformationBroadcast');
+ const {popupId, frameId} = await rootPopupInformationPromise;
+
+ const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder);
+
+ const popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset);
+ await popup.prepare();
+
+ return popup;
+}
+
+async function getOrCreatePopup(depth) {
+ const popupHost = new PopupProxyHost();
+ await popupHost.prepare();
+
+ const popup = popupHost.getOrCreatePopup(null, null, depth);
+
+ return popup;
+}
+
+async function createPopupProxy(depth, id, parentFrameId, url) {
+ const popup = new PopupProxy(null, depth + 1, id, parentFrameId, url);
+ await popup.prepare();
+
+ return popup;
+}
+
async function main() {
await yomichan.prepare();
const data = window.frontendInitializationData || {};
- const {id, depth=0, parentFrameId, url, proxy=false} = data;
-
- const optionsContext = {depth, url};
- const options = await apiOptionsGet(optionsContext);
-
- let popup;
- if (!proxy && (window !== window.parent) && options.general.showIframePopupsInRootFrame) {
- const rootPopupInformationPromise = yomichan.getTemporaryListenerResult(
- chrome.runtime.onMessage,
- ({action, params}, {resolve}) => {
- if (action === 'rootPopupInformation') {
- resolve(params);
- }
+ const {id, depth=0, parentFrameId, url=window.location.href, proxy=false, isSearchPage=false} = data;
+
+ const isIframe = !proxy && (window !== window.parent);
+
+ const popups = {
+ iframe: null,
+ proxy: null,
+ normal: null
+ };
+
+ let frontend = null;
+ let frontendPreparePromise = null;
+ let frameOffsetForwarder = null;
+
+ const applyOptions = async () => {
+ const optionsContext = {depth: isSearchPage ? 0 : depth, url};
+ const options = await apiOptionsGet(optionsContext);
+
+ if (!proxy && frameOffsetForwarder === null) {
+ frameOffsetForwarder = new FrameOffsetForwarder();
+ frameOffsetForwarder.start();
+ }
+
+ let popup;
+ if (isIframe && options.general.showIframePopupsInRootFrame) {
+ popup = popups.iframe || await createIframePopupProxy(url, frameOffsetForwarder);
+ popups.iframe = popup;
+ } else if (proxy) {
+ popup = popups.proxy || await createPopupProxy(depth, id, parentFrameId, url);
+ popups.proxy = popup;
+ } else {
+ popup = popups.normal || await getOrCreatePopup(depth);
+ popups.normal = popup;
+ }
+
+ if (frontend === null) {
+ frontend = new Frontend(popup);
+ frontendPreparePromise = frontend.prepare();
+ await frontendPreparePromise;
+ } else {
+ await frontendPreparePromise;
+ if (isSearchPage) {
+ const disabled = !options.scanning.enableOnSearchPage;
+ frontend.setDisabledOverride(disabled);
+ }
+
+ if (isIframe) {
+ await frontend.setPopup(popup);
}
- );
- apiForward('rootPopupRequestInformationBroadcast');
- const {popupId, frameId} = await rootPopupInformationPromise;
-
- const frameOffsetForwarder = new FrameOffsetForwarder();
- frameOffsetForwarder.start();
- const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder);
-
- popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset);
- await popup.prepare();
- } else if (proxy) {
- popup = new PopupProxy(null, depth + 1, id, parentFrameId, url);
- await popup.prepare();
- } else {
- const frameOffsetForwarder = new FrameOffsetForwarder();
- frameOffsetForwarder.start();
-
- const popupHost = new PopupProxyHost();
- await popupHost.prepare();
-
- popup = popupHost.getOrCreatePopup(null, null, depth);
- }
-
- const frontend = new Frontend(popup);
- await frontend.prepare();
+ }
+ };
+
+ yomichan.on('optionsUpdated', applyOptions);
+
+ await applyOptions();
}
main();