summaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-08-09 13:11:41 -0400
committerGitHub <noreply@github.com>2020-08-09 13:11:41 -0400
commit8ee717cdf7c7701b9ec2d72799fcdcb621703e9e (patch)
tree04b0f66d0d9b1d1987a6ceb92d5650650d901c55 /ext/bg/js/backend.js
parent04d47bf8a92a71ae49c320d8d7eccd50c254348e (diff)
Persistent display mode (#714)
* Simplify calls using chrome.tabs.sendMessage and getMessageResponseResult * Rename message handlers * Move onMessage handler into Display * Assign search popup mode which persists across refreshes * Update clipboard monitor on the search page * Remove mode param
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js85
1 files changed, 42 insertions, 43 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 85b9b5e6..07920e01 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -899,24 +899,8 @@ class Backend {
);
});
if (tab !== null) {
- const isValidTab = await new Promise((resolve) => {
- chrome.tabs.sendMessage(
- tabId,
- {action: 'getUrl', params: {}},
- {frameId: 0},
- (response) => {
- let result = false;
- try {
- const {url} = yomichan.getMessageResponseResult(response);
- result = url.startsWith(baseUrl);
- } catch (e) {
- // NOP
- }
- resolve(result);
- }
- );
- });
- // windowId
+ const url = await this._getTabUrl(tabId);
+ const isValidTab = (url !== null && url.startsWith(baseUrl));
if (isValidTab) {
return {tab, created: false};
}
@@ -935,7 +919,7 @@ class Backend {
const popupWindow = await new Promise((resolve, reject) => {
chrome.windows.create(
{
- url: `${baseUrl}?mode=popup`,
+ url: baseUrl,
width: popupWidth,
height: popupHeight,
type: 'popup'
@@ -959,23 +943,22 @@ class Backend {
const tab = tabs[0];
await this._waitUntilTabFrameIsReady(tab.id, 0, 2000);
+ await this._sendMessageTab(
+ tab.id,
+ {action: 'setMode', params: {mode: 'popup'}},
+ {frameId: 0}
+ );
+
this._searchPopupTabId = tab.id;
return {tab, created: true};
}
_updateSearchQuery(tabId, text, animate) {
- return new Promise((resolve, reject) => {
- const callback = (response) => {
- try {
- resolve(yomichan.getMessageResponseResult(response));
- } catch (error) {
- reject(error);
- }
- };
-
- const message = {action: 'updateSearchQuery', params: {text, animate}};
- chrome.tabs.sendMessage(tabId, message, callback);
- });
+ return this._sendMessageTab(
+ tabId,
+ {action: 'updateSearchQuery', params: {text, animate}},
+ {frameId: 0}
+ );
}
_sendMessageAllTabs(action, params={}) {
@@ -1381,18 +1364,20 @@ class Backend {
return typeof templates === 'string' ? templates : this._defaultAnkiFieldTemplates;
}
- _getTabUrl(tab) {
- return new Promise((resolve) => {
- chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => {
- let url = null;
- try {
- ({url} = yomichan.getMessageResponseResult(response));
- } catch (error) {
- // NOP
- }
- resolve({tab, url});
- });
- });
+ async _getTabUrl(tabId) {
+ try {
+ const {url} = await this._sendMessageTab(
+ tabId,
+ {action: 'getUrl', params: {}},
+ {frameId: 0}
+ );
+ if (typeof url === 'string') {
+ return url;
+ }
+ } catch (e) {
+ // NOP
+ }
+ return null;
}
async _findTab(timeout, checkUrl) {
@@ -1538,4 +1523,18 @@ class Backend {
}
return await (json ? response.json() : response.text());
}
+
+ _sendMessageTab(...args) {
+ return new Promise((resolve, reject) => {
+ const callback = (response) => {
+ try {
+ resolve(yomichan.getMessageResponseResult(response));
+ } catch (error) {
+ reject(error);
+ }
+ };
+
+ chrome.tabs.sendMessage(...args, callback);
+ });
+ }
}