summaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-09 23:14:29 -0500
committerGitHub <noreply@github.com>2021-02-09 23:14:29 -0500
commit673952e82576b0bc4b6b02c2105fbb1850e55950 (patch)
treeb69661c8ff852c94be8b67f7f448345e59e6e1fa /ext/bg/js/backend.js
parent166451b8f76224542b49c13cb27a258eb291f05e (diff)
Fix window popup screenshot (#1365)
* Pass tabId to the screenshot functionality * Make setVisibleOverride async * Fix argument order * Fix incorrect windowId * Remove unused argument
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js46
1 files changed, 28 insertions, 18 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index fd90a220..465c8137 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -465,11 +465,7 @@ class Backend {
return results;
}
- async _onApiInjectAnkiNoteMedia({timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails}, sender) {
- if (isObject(screenshotDetails)) {
- const {id: tabId, windowId} = (sender && sender.tab ? sender.tab : {});
- screenshotDetails = Object.assign({}, screenshotDetails, {tabId, windowId});
- }
+ async _onApiInjectAnkiNoteMedia({timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails}) {
return await this._injectAnkNoteMedia(
this._anki,
timestamp,
@@ -1494,23 +1490,21 @@ class Backend {
}
async _checkTabUrl(tabId, urlPredicate) {
- const tab = await new Promise((resolve) => {
- chrome.tabs.get(
- tabId,
- (result) => { resolve(chrome.runtime.lastError ? null : result); }
- );
- });
- if (tab === null) { return null; }
+ let tab;
+ try {
+ tab = await this._getTabById(tabId);
+ } catch (e) {
+ return null;
+ }
const url = await this._getTabUrl(tabId);
const isValidTab = urlPredicate(url);
return isValidTab ? tab : null;
}
- async _getScreenshot(windowId, tabId, frameId, format, quality) {
- if (typeof windowId !== 'number') {
- throw new Error('Invalid window ID');
- }
+ async _getScreenshot(tabId, frameId, format, quality) {
+ const tab = await this._getTabById(tabId);
+ const {windowId} = tab;
let token = null;
try {
@@ -1636,8 +1630,8 @@ class Backend {
}
async _injectAnkNoteScreenshot(ankiConnect, timestamp, definitionDetails, details) {
- const {windowId, tabId, frameId, format, quality} = details;
- const dataUrl = await this._getScreenshot(windowId, tabId, frameId, format, quality);
+ const {tabId, frameId, format, quality} = details;
+ const dataUrl = await this._getScreenshot(tabId, frameId, format, quality);
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
@@ -1954,4 +1948,20 @@ class Backend {
}
}));
}
+
+ _getTabById(tabId) {
+ return new Promise((resolve, reject) => {
+ chrome.tabs.get(
+ tabId,
+ (result) => {
+ const e = chrome.runtime.lastError;
+ if (e) {
+ reject(new Error(e.message));
+ } else {
+ resolve(result);
+ }
+ }
+ );
+ });
+ }
}