From 286534e648af350d24fbf3c7892a7ec81aaeb4bd Mon Sep 17 00:00:00 2001
From: toasted-nutbread <toasted-nutbread@users.noreply.github.com>
Date: Sun, 14 Feb 2021 15:53:35 -0500
Subject: Move api to yomichan object (#1392)

* Move cross frame API from API to Yomichan

* Add API instance to Yomichan

* Move api global to yomichan.api

* Pass yomichan to API

* Remove IIFE
---
 ext/js/app/content-script-main.js |  4 +---
 ext/js/app/frontend.js            | 17 ++++++++---------
 ext/js/app/popup-factory.js       |  5 ++---
 ext/js/app/popup-proxy.js         |  6 +-----
 ext/js/app/popup-window.js        | 12 ++++--------
 ext/js/app/popup.js               |  5 ++---
 6 files changed, 18 insertions(+), 31 deletions(-)

(limited to 'ext/js/app')

diff --git a/ext/js/app/content-script-main.js b/ext/js/app/content-script-main.js
index ee05034e..a09e52ea 100644
--- a/ext/js/app/content-script-main.js
+++ b/ext/js/app/content-script-main.js
@@ -19,15 +19,13 @@
  * Frontend
  * HotkeyHandler
  * PopupFactory
- * api
  */
 
 (async () => {
     try {
-        api.prepare();
         await yomichan.prepare();
 
-        const {tabId, frameId} = await api.frameInformationGet();
+        const {tabId, frameId} = await yomichan.api.frameInformationGet();
         if (typeof frameId !== 'number') {
             throw new Error('Failed to get frameId');
         }
diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js
index a62b06bf..74cc63d2 100644
--- a/ext/js/app/frontend.js
+++ b/ext/js/app/frontend.js
@@ -20,7 +20,6 @@
  * TextScanner
  * TextSourceElement
  * TextSourceRange
- * api
  */
 
 class Frontend {
@@ -99,7 +98,7 @@ class Frontend {
     async prepare() {
         await this.updateOptions();
         try {
-            const {zoomFactor} = await api.getZoom();
+            const {zoomFactor} = await yomichan.api.getZoom();
             this._pageZoomFactor = zoomFactor;
         } catch (e) {
             // Ignore exceptions which may occur due to being on an unsupported page (e.g. about:blank)
@@ -124,7 +123,7 @@ class Frontend {
         this._textScanner.on('clearSelection', this._onClearSelection.bind(this));
         this._textScanner.on('searched', this._onSearched.bind(this));
 
-        api.crossFrame.registerHandlers([
+        yomichan.crossFrame.registerHandlers([
             ['closePopup',              {async: false, handler: this._onApiClosePopup.bind(this)}],
             ['copySelection',           {async: false, handler: this._onApiCopySelection.bind(this)}],
             ['getSelectionText',        {async: false, handler: this._onApiGetSelectionText.bind(this)}],
@@ -332,7 +331,7 @@ class Frontend {
 
     async _updateOptionsInternal() {
         const optionsContext = await this._getOptionsContext();
-        const options = await api.optionsGet(optionsContext);
+        const options = await yomichan.api.optionsGet(optionsContext);
         const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
         this._options = options;
 
@@ -462,7 +461,7 @@ class Frontend {
             return await this._getDefaultPopup();
         }
 
-        const {popupId} = await api.crossFrame.invoke(targetFrameId, 'getPopupInfo');
+        const {popupId} = await yomichan.crossFrame.invoke(targetFrameId, 'getPopupInfo');
         if (popupId === null) {
             return null;
         }
@@ -608,9 +607,9 @@ class Frontend {
     _signalFrontendReady(targetFrameId=null) {
         const params = {frameId: this._frameId};
         if (targetFrameId === null) {
-            api.broadcastTab('frontendReady', params);
+            yomichan.api.broadcastTab('frontendReady', params);
         } else {
-            api.sendMessageToFrame(targetFrameId, 'frontendReady', params);
+            yomichan.api.sendMessageToFrame(targetFrameId, 'frontendReady', params);
         }
     }
 
@@ -627,7 +626,7 @@ class Frontend {
             },
             10000
         );
-        api.broadcastTab('requestFrontendReadyBroadcast', {frameId: this._frameId});
+        yomichan.api.broadcastTab('requestFrontendReadyBroadcast', {frameId: this._frameId});
         await promise;
     }
 
@@ -653,7 +652,7 @@ class Frontend {
         let documentTitle = document.title;
         if (this._useProxyPopup) {
             try {
-                ({url, documentTitle} = await api.crossFrame.invoke(this._parentFrameId, 'getPageInfo', {}));
+                ({url, documentTitle} = await yomichan.crossFrame.invoke(this._parentFrameId, 'getPageInfo', {}));
             } catch (e) {
                 // NOP
             }
diff --git a/ext/js/app/popup-factory.js b/ext/js/app/popup-factory.js
index 7571d7ab..8f0c2a6e 100644
--- a/ext/js/app/popup-factory.js
+++ b/ext/js/app/popup-factory.js
@@ -20,7 +20,6 @@
  * Popup
  * PopupProxy
  * PopupWindow
- * api
  */
 
 class PopupFactory {
@@ -35,7 +34,7 @@ class PopupFactory {
 
     prepare() {
         this._frameOffsetForwarder.prepare();
-        api.crossFrame.registerHandlers([
+        yomichan.crossFrame.registerHandlers([
             ['getOrCreatePopup',     {async: true,  handler: this._onApiGetOrCreatePopup.bind(this)}],
             ['setOptionsContext',    {async: true,  handler: this._onApiSetOptionsContext.bind(this)}],
             ['hide',                 {async: false, handler: this._onApiHide.bind(this)}],
@@ -132,7 +131,7 @@ class PopupFactory {
                 throw new Error('Invalid frameId');
             }
             const useFrameOffsetForwarder = (parentPopupId === null);
-            ({id, depth, frameId} = await api.crossFrame.invoke(frameId, 'getOrCreatePopup', {
+            ({id, depth, frameId} = await yomichan.crossFrame.invoke(frameId, 'getOrCreatePopup', {
                 id,
                 parentPopupId,
                 frameId,
diff --git a/ext/js/app/popup-proxy.js b/ext/js/app/popup-proxy.js
index b2e81824..19856e3f 100644
--- a/ext/js/app/popup-proxy.js
+++ b/ext/js/app/popup-proxy.js
@@ -15,10 +15,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-/* global
- * api
- */
-
 class PopupProxy extends EventDispatcher {
     constructor({
         id,
@@ -158,7 +154,7 @@ class PopupProxy extends EventDispatcher {
     // Private
 
     _invoke(action, params={}) {
-        return api.crossFrame.invoke(this._frameId, action, params);
+        return yomichan.crossFrame.invoke(this._frameId, action, params);
     }
 
     async _invokeSafe(action, params={}, defaultReturnValue) {
diff --git a/ext/js/app/popup-window.js b/ext/js/app/popup-window.js
index 5fa0c647..d0826775 100644
--- a/ext/js/app/popup-window.js
+++ b/ext/js/app/popup-window.js
@@ -15,10 +15,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-/* global
- * api
- */
-
 class PopupWindow extends EventDispatcher {
     constructor({
         id,
@@ -82,7 +78,7 @@ class PopupWindow extends EventDispatcher {
     }
 
     async isVisible() {
-        return (this._popupTabId !== null && await api.isTabSearchPopup(this._popupTabId));
+        return (this._popupTabId !== null && await yomichan.api.isTabSearchPopup(this._popupTabId));
     }
 
     async setVisibleOverride(_value, _priority) {
@@ -148,7 +144,7 @@ class PopupWindow extends EventDispatcher {
         const frameId = 0;
         if (this._popupTabId !== null) {
             try {
-                return await api.crossFrame.invokeTab(this._popupTabId, frameId, 'popupMessage', {action, params});
+                return await yomichan.crossFrame.invokeTab(this._popupTabId, frameId, 'popupMessage', {action, params});
             } catch (e) {
                 if (yomichan.isExtensionUnloaded) {
                     open = false;
@@ -161,9 +157,9 @@ class PopupWindow extends EventDispatcher {
             return defaultReturnValue;
         }
 
-        const {tabId} = await api.getOrCreateSearchPopup({focus: 'ifCreated'});
+        const {tabId} = await yomichan.api.getOrCreateSearchPopup({focus: 'ifCreated'});
         this._popupTabId = tabId;
 
-        return await api.crossFrame.invokeTab(this._popupTabId, frameId, 'popupMessage', {action, params});
+        return await yomichan.crossFrame.invokeTab(this._popupTabId, frameId, 'popupMessage', {action, params});
     }
 }
diff --git a/ext/js/app/popup.js b/ext/js/app/popup.js
index 75b74257..44cca14a 100644
--- a/ext/js/app/popup.js
+++ b/ext/js/app/popup.js
@@ -18,7 +18,6 @@
 /* global
  * DocumentUtil
  * FrameClient
- * api
  * dynamicLoader
  */
 
@@ -460,7 +459,7 @@ class Popup extends EventDispatcher {
         if (this._frameClient === null || !this._frameClient.isConnected() || contentWindow === null) { return; }
 
         const message = this._frameClient.createMessage({action, params});
-        return await api.crossFrame.invoke(this._frameClient.frameId, 'popupMessage', message);
+        return await yomichan.crossFrame.invoke(this._frameClient.frameId, 'popupMessage', message);
     }
 
     async _invokeSafe(action, params={}, defaultReturnValue) {
@@ -676,7 +675,7 @@ class Popup extends EventDispatcher {
 
     async _setOptionsContext(optionsContext) {
         this._optionsContext = optionsContext;
-        this._options = await api.optionsGet(optionsContext);
+        this._options = await yomichan.api.optionsGet(optionsContext);
         this.updateTheme();
     }
 
-- 
cgit v1.2.3