summaryrefslogtreecommitdiff
path: root/ext/js/app/frontend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-14 18:18:02 -0500
committerGitHub <noreply@github.com>2021-02-14 18:18:02 -0500
commit48b59375eb50a3c11ab1cbee659164e6991827ac (patch)
tree160bfe321492839cf3211b27285490e43a7f130d /ext/js/app/frontend.js
parent9279ced68660610764931da681f22c8b71bf1b6e (diff)
Cleanup yomichan api (#1394)
* Move invokeMessageHandler to core.js * Move getMessageResponseResult to backghend.js * Replace getTemporaryListenerResult
Diffstat (limited to 'ext/js/app/frontend.js')
-rw-r--r--ext/js/app/frontend.js51
1 files changed, 35 insertions, 16 deletions
diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js
index 02707f41..92366d6e 100644
--- a/ext/js/app/frontend.js
+++ b/ext/js/app/frontend.js
@@ -238,7 +238,7 @@ class Frontend {
_onRuntimeMessage({action, params}, sender, callback) {
const messageHandler = this._runtimeMessageHandlers.get(action);
if (typeof messageHandler === 'undefined') { return false; }
- return yomichan.invokeMessageHandler(messageHandler, params, callback, sender);
+ return invokeMessageHandler(messageHandler, params, callback, sender);
}
_onZoomChanged({newZoomFactor}) {
@@ -455,7 +455,7 @@ class Frontend {
async _getIframeProxyPopup() {
const targetFrameId = 0; // Root frameId
try {
- await this._waitForFrontendReady(targetFrameId);
+ await this._waitForFrontendReady(targetFrameId, 10000);
} catch (e) {
// Root frame not available
return await this._getDefaultPopup();
@@ -613,21 +613,40 @@ class Frontend {
}
}
- async _waitForFrontendReady(frameId) {
- const promise = yomichan.getTemporaryListenerResult(
- chrome.runtime.onMessage,
- ({action, params}, {resolve}) => {
- if (
- action === 'frontendReady' &&
- params.frameId === frameId
- ) {
- resolve();
+ async _waitForFrontendReady(frameId, timeout) {
+ return new Promise((resolve, reject) => {
+ let timeoutId = null;
+
+ const cleanup = () => {
+ if (timeoutId !== null) {
+ clearTimeout(timeoutId);
+ timeoutId = null;
}
- },
- 10000
- );
- yomichan.api.broadcastTab('requestFrontendReadyBroadcast', {frameId: this._frameId});
- await promise;
+ chrome.runtime.onMessage.removeListener(onMessage);
+ };
+ const onMessage = (message, sender, sendResponse) => {
+ try {
+ const {action, params} = message;
+ if (action === 'frontendReady' && params.frameId === frameId) {
+ cleanup();
+ resolve();
+ sendResponse();
+ }
+ } catch (e) {
+ // NOP
+ }
+ };
+
+ if (timeout !== null) {
+ timeoutId = setTimeout(() => {
+ timeoutId = null;
+ cleanup();
+ reject(new Error(`Wait for frontend ready timed out after ${timeout}ms`));
+ }, timeout);
+ }
+
+ chrome.runtime.onMessage.addListener(onMessage);
+ });
}
_getPreventMiddleMouseValueForPageType(preventMiddleMouseOptions) {