Add support for getting the document title

This commit is contained in:
toasted-nutbread 2020-04-06 18:55:36 -04:00
parent 199b926f23
commit 696897f6fa
2 changed files with 37 additions and 1 deletions

View File

@ -162,6 +162,33 @@ class DisplayFloat extends Display {
setContentScale(scale) {
document.body.style.fontSize = `${scale}em`;
}
async getDocumentTitle() {
try {
const uniqueId = yomichan.generateId(16);
const promise = yomichan.getTemporaryListenerResult(
chrome.runtime.onMessage,
({action, params}, {resolve}) => {
if (
action === 'documentInformationBroadcast' &&
isObject(params) &&
params.uniqueId === uniqueId &&
params.frameId === 0
) {
resolve(params);
}
},
2000
);
apiForward('requestDocumentInformationBroadcast', {uniqueId});
const {title} = await promise;
return title;
} catch (e) {
return '';
}
}
}
DisplayFloat.instance = new DisplayFloat();

View File

@ -54,7 +54,8 @@ class Frontend extends TextScanner {
this._runtimeMessageHandlers = new Map([
['popupSetVisibleOverride', ({visible}) => { this.popup.setVisibleOverride(visible); }],
['rootPopupRequestInformationBroadcast', () => { this._broadcastRootPopupInformation(); }]
['rootPopupRequestInformationBroadcast', () => { this._broadcastRootPopupInformation(); }],
['requestDocumentInformationBroadcast', ({uniqueId}) => { this._broadcastDocumentInformation(uniqueId); }]
]);
}
@ -264,6 +265,14 @@ class Frontend extends TextScanner {
}
}
_broadcastDocumentInformation(uniqueId) {
apiForward('documentInformationBroadcast', {
uniqueId,
frameId: this.popup.frameId,
title: document.title
});
}
async _updatePopupPosition() {
const textSource = this.getCurrentTextSource();
if (textSource !== null && await this.popup.isVisible()) {