Refactor note document title (#1227)

* Pass url into setContent

* Update where url is checked from

* Add documentTitle to state information

* Update how _getNoteContext gets the document title

* Update how url is fetched for options context

* Pass document title in to 'searched' event
This commit is contained in:
toasted-nutbread 2021-01-12 18:04:26 -05:00 committed by GitHub
parent 983e2c7936
commit b7c9fa1057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 38 deletions

View File

@ -349,6 +349,9 @@ class DisplaySearch extends Display {
_search(animate, history) {
const query = this._queryInput.value;
const depth = this.depth;
const url = window.location.href;
const documentTitle = document.title;
const details = {
focus: false,
history,
@ -357,8 +360,10 @@ class DisplaySearch extends Display {
},
state: {
focusEntry: 0,
optionsContext: {depth, url},
url,
sentence: {text: query, offset: 0},
url: window.location.href
documentTitle
},
content: {
definitions: null,

View File

@ -114,12 +114,11 @@ class Frontend {
this._textScanner.on('searched', this._onSearched.bind(this));
api.crossFrame.registerHandlers([
['getUrl', {async: false, handler: this._onApiGetUrl.bind(this)}],
['closePopup', {async: false, handler: this._onApiClosePopup.bind(this)}],
['copySelection', {async: false, handler: this._onApiCopySelection.bind(this)}],
['getSelectionText', {async: false, handler: this._onApiGetSelectionText.bind(this)}],
['getPopupInfo', {async: false, handler: this._onApiGetPopupInfo.bind(this)}],
['getDocumentInformation', {async: false, handler: this._onApiGetDocumentInformation.bind(this)}],
['getPageInfo', {async: false, handler: this._onApiGetPageInfo.bind(this)}],
['getFrameSize', {async: true, handler: this._onApiGetFrameSize.bind(this)}],
['setFrameSize', {async: true, handler: this._onApiSetFrameSize.bind(this)}]
]);
@ -187,9 +186,10 @@ class Frontend {
};
}
_onApiGetDocumentInformation() {
_onApiGetPageInfo() {
return {
title: document.title
url: window.location.href,
documentTitle: document.title
};
}
@ -251,7 +251,7 @@ class Frontend {
}
}
_onSearched({type, definitions, sentence, inputInfo: {cause, empty}, textSource, optionsContext, error}) {
_onSearched({type, definitions, sentence, inputInfo: {cause, empty}, textSource, optionsContext, detail: {documentTitle}, error}) {
const scanningOptions = this._options.scanning;
if (error !== null) {
@ -265,7 +265,7 @@ class Frontend {
} if (type !== null) {
this._stopClearSelectionDelayed();
const focus = (cause === 'mouseMove');
this._showContent(textSource, focus, definitions, type, sentence, optionsContext);
this._showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext);
} else {
if (scanningOptions.autoHideResults) {
this._clearSelectionDelayed(scanningOptions.hideDelay, false);
@ -497,8 +497,9 @@ class Frontend {
this._showPopupContent(textSource, null);
}
_showContent(textSource, focus, definitions, type, sentence, optionsContext) {
_showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext) {
const query = textSource.text();
const {url} = optionsContext;
const details = {
focus,
history: false,
@ -509,8 +510,10 @@ class Frontend {
},
state: {
focusEntry: 0,
optionsContext,
url,
sentence,
optionsContext
documentTitle
},
content: {
definitions
@ -624,15 +627,19 @@ class Frontend {
}
let url = window.location.href;
let documentTitle = document.title;
if (this._useProxyPopup) {
try {
url = await api.crossFrame.invoke(this._parentFrameId, 'getUrl', {});
({url, documentTitle} = await api.crossFrame.invoke(this._parentFrameId, 'getPageInfo', {}));
} catch (e) {
// NOP
}
}
const depth = this._depth;
return {optionsContext: {depth, url}};
return {
optionsContext: {depth, url},
detail: {documentTitle}
};
}
}

View File

@ -194,6 +194,10 @@ class Display extends EventDispatcher {
return this._japaneseUtil;
}
get depth() {
return this._depth;
}
async prepare() {
// State setup
const {documentElement} = document;
@ -391,13 +395,6 @@ class Display extends EventDispatcher {
}
}
async getDocumentTitle() {
if (this._pageType === 'popup') {
return await this._getRootFrameDocumentTitle();
}
return document.title;
}
registerActions(actions) {
for (const [name, handler] of actions) {
this._actions.set(name, handler);
@ -466,8 +463,10 @@ class Display extends EventDispatcher {
clone(this._history.state) :
{
focusEntry: 0,
optionsContext: this._optionsContext,
url: window.location.href,
sentence: {text: query, offset: 0},
url: window.location.href
documentTitle: document.title
}
);
const details = {
@ -713,7 +712,9 @@ class Display extends EventDispatcher {
e.preventDefault();
if (!this._historyHasState()) { return; }
const {state: {sentence}} = this._history;
let {state: {sentence, url, documentTitle}} = this._history;
if (typeof url !== 'string') { url = window.location.href; }
if (typeof documentTitle !== 'string') { documentTitle = document.title; }
const optionsContext = this.getOptionsContext();
const query = e.currentTarget.textContent;
const definitions = await api.kanjiFind(query, optionsContext);
@ -723,8 +724,10 @@ class Display extends EventDispatcher {
params: this._createSearchParams('kanji', query, false),
state: {
focusEntry: 0,
optionsContext,
url,
sentence,
optionsContext
documentTitle
},
content: {
definitions
@ -908,15 +911,21 @@ class Display extends EventDispatcher {
changeHistory = true;
}
let {sentence=null, optionsContext=null, focusEntry=null, scrollX=null, scrollY=null} = state;
let {
focusEntry=null,
scrollX=null,
scrollY=null,
optionsContext=null,
sentence=null,
url
} = state;
if (typeof focusEntry !== 'number') { focusEntry = 0; }
if (typeof url !== 'string') { url = window.location.href; }
if (!(typeof optionsContext === 'object' && optionsContext !== null)) {
optionsContext = this.getOptionsContext();
state.optionsContext = optionsContext;
changeHistory = true;
}
let {url} = optionsContext;
if (typeof url !== 'string') { url = window.location.href; }
sentence = this._getValidSentenceData(sentence);
this._setFullQuery(queryFull);
@ -1094,7 +1103,7 @@ class Display extends EventDispatcher {
let states;
try {
if (this._options.anki.checkForDuplicates) {
const noteContext = await this._getNoteContext();
const noteContext = this._getNoteContext();
states = await this._areDefinitionsAddable(definitions, modes, noteContext);
} else {
if (!await api.isAnkiConnected()) {
@ -1208,7 +1217,7 @@ class Display extends EventDispatcher {
const overrideToken = this._progressIndicatorVisible.setOverride(true);
try {
const noteContext = await this._getNoteContext();
const noteContext = this._getNoteContext();
const note = await this._createNote(definition, mode, noteContext, true);
const noteId = await api.addAnkiNote(note);
if (noteId) {
@ -1371,8 +1380,15 @@ class Display extends EventDispatcher {
return elementRect.top - documentRect.top;
}
async _getNoteContext() {
const documentTitle = await this.getDocumentTitle();
_getNoteContext() {
const {state} = this._history;
let documentTitle = null;
if (typeof state === 'object' && state !== null) {
({documentTitle} = state);
}
if (typeof documentTitle !== 'string') {
documentTitle = '';
}
return {
document: {
title: documentTitle
@ -1728,15 +1744,6 @@ class Display extends EventDispatcher {
parent.removeChild(textarea);
}
async _getRootFrameDocumentTitle() {
try {
const {title} = await api.crossFrame.invoke(0, 'getDocumentInformation');
return title;
} catch (e) {
return '';
}
}
_addMultipleEventListeners(container, selector, ...args) {
for (const node of container.querySelectorAll(selector)) {
this._eventListeners.addEventListener(node, ...args);
@ -1815,6 +1822,8 @@ class Display extends EventDispatcher {
if (type === null) { return; }
const query = textSource.text();
const url = window.location.href;
const documentTitle = document.title;
const details = {
focus: false,
history: true,
@ -1825,8 +1834,10 @@ class Display extends EventDispatcher {
},
state: {
focusEntry: 0,
optionsContext,
url,
sentence,
optionsContext
documentTitle
},
content: {
definitions