Fix window popup screenshot (#1365)
* Pass tabId to the screenshot functionality * Make setVisibleOverride async * Fix argument order * Fix incorrect windowId * Remove unused argument
This commit is contained in:
parent
166451b8f7
commit
673952e825
@ -465,11 +465,7 @@ class Backend {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiInjectAnkiNoteMedia({timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails}, sender) {
|
async _onApiInjectAnkiNoteMedia({timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails}) {
|
||||||
if (isObject(screenshotDetails)) {
|
|
||||||
const {id: tabId, windowId} = (sender && sender.tab ? sender.tab : {});
|
|
||||||
screenshotDetails = Object.assign({}, screenshotDetails, {tabId, windowId});
|
|
||||||
}
|
|
||||||
return await this._injectAnkNoteMedia(
|
return await this._injectAnkNoteMedia(
|
||||||
this._anki,
|
this._anki,
|
||||||
timestamp,
|
timestamp,
|
||||||
@ -1494,23 +1490,21 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _checkTabUrl(tabId, urlPredicate) {
|
async _checkTabUrl(tabId, urlPredicate) {
|
||||||
const tab = await new Promise((resolve) => {
|
let tab;
|
||||||
chrome.tabs.get(
|
try {
|
||||||
tabId,
|
tab = await this._getTabById(tabId);
|
||||||
(result) => { resolve(chrome.runtime.lastError ? null : result); }
|
} catch (e) {
|
||||||
);
|
return null;
|
||||||
});
|
}
|
||||||
if (tab === null) { return null; }
|
|
||||||
|
|
||||||
const url = await this._getTabUrl(tabId);
|
const url = await this._getTabUrl(tabId);
|
||||||
const isValidTab = urlPredicate(url);
|
const isValidTab = urlPredicate(url);
|
||||||
return isValidTab ? tab : null;
|
return isValidTab ? tab : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getScreenshot(windowId, tabId, frameId, format, quality) {
|
async _getScreenshot(tabId, frameId, format, quality) {
|
||||||
if (typeof windowId !== 'number') {
|
const tab = await this._getTabById(tabId);
|
||||||
throw new Error('Invalid window ID');
|
const {windowId} = tab;
|
||||||
}
|
|
||||||
|
|
||||||
let token = null;
|
let token = null;
|
||||||
try {
|
try {
|
||||||
@ -1636,8 +1630,8 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _injectAnkNoteScreenshot(ankiConnect, timestamp, definitionDetails, details) {
|
async _injectAnkNoteScreenshot(ankiConnect, timestamp, definitionDetails, details) {
|
||||||
const {windowId, tabId, frameId, format, quality} = details;
|
const {tabId, frameId, format, quality} = details;
|
||||||
const dataUrl = await this._getScreenshot(windowId, tabId, frameId, format, quality);
|
const dataUrl = await this._getScreenshot(tabId, frameId, format, quality);
|
||||||
|
|
||||||
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
|
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
|
||||||
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
class DisplaySearch extends Display {
|
class DisplaySearch extends Display {
|
||||||
constructor(tabId, frameId, japaneseUtil, documentFocusController, hotkeyHandler) {
|
constructor(tabId, frameId, japaneseUtil, documentFocusController, hotkeyHandler) {
|
||||||
super('search', tabId, frameId, japaneseUtil, documentFocusController, hotkeyHandler);
|
super(tabId, frameId, 'search', japaneseUtil, documentFocusController, hotkeyHandler);
|
||||||
this._searchButton = document.querySelector('#search-button');
|
this._searchButton = document.querySelector('#search-button');
|
||||||
this._queryInput = document.querySelector('#search-textbox');
|
this._queryInput = document.querySelector('#search-textbox');
|
||||||
this._introElement = document.querySelector('#intro');
|
this._introElement = document.querySelector('#intro');
|
||||||
|
@ -85,7 +85,7 @@ class PopupWindow extends EventDispatcher {
|
|||||||
return (this._popupTabId !== null && await api.isTabSearchPopup(this._popupTabId));
|
return (this._popupTabId !== null && await api.isTabSearchPopup(this._popupTabId));
|
||||||
}
|
}
|
||||||
|
|
||||||
setVisibleOverride(_value, _priority) {
|
async setVisibleOverride(_value, _priority) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1539,10 +1539,9 @@ class Display extends EventDispatcher {
|
|||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
const screenshotFrameId = this._contentOriginFrameId;
|
|
||||||
const definitionDetails = this._getDefinitionDetailsForNote(definition);
|
const definitionDetails = this._getDefinitionDetailsForNote(definition);
|
||||||
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl, customSourceType} : null);
|
const audioDetails = (mode !== 'kanji' && this._ankiNoteBuilder.containsMarker(fields, 'audio') ? {sources, customSourceUrl, customSourceType} : null);
|
||||||
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {frameId: screenshotFrameId, format, quality} : null);
|
const screenshotDetails = (this._ankiNoteBuilder.containsMarker(fields, 'screenshot') ? {tabId: this._contentOriginTabId, frameId: this._contentOriginFrameId, format, quality} : null);
|
||||||
const clipboardDetails = {
|
const clipboardDetails = {
|
||||||
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
|
image: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-image'),
|
||||||
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
|
text: this._ankiNoteBuilder.containsMarker(fields, 'clipboard-text')
|
||||||
|
Loading…
Reference in New Issue
Block a user