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

View File

@ -114,12 +114,11 @@ class Frontend {
this._textScanner.on('searched', this._onSearched.bind(this)); this._textScanner.on('searched', this._onSearched.bind(this));
api.crossFrame.registerHandlers([ api.crossFrame.registerHandlers([
['getUrl', {async: false, handler: this._onApiGetUrl.bind(this)}],
['closePopup', {async: false, handler: this._onApiClosePopup.bind(this)}], ['closePopup', {async: false, handler: this._onApiClosePopup.bind(this)}],
['copySelection', {async: false, handler: this._onApiCopySelection.bind(this)}], ['copySelection', {async: false, handler: this._onApiCopySelection.bind(this)}],
['getSelectionText', {async: false, handler: this._onApiGetSelectionText.bind(this)}], ['getSelectionText', {async: false, handler: this._onApiGetSelectionText.bind(this)}],
['getPopupInfo', {async: false, handler: this._onApiGetPopupInfo.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)}], ['getFrameSize', {async: true, handler: this._onApiGetFrameSize.bind(this)}],
['setFrameSize', {async: true, handler: this._onApiSetFrameSize.bind(this)}] ['setFrameSize', {async: true, handler: this._onApiSetFrameSize.bind(this)}]
]); ]);
@ -187,9 +186,10 @@ class Frontend {
}; };
} }
_onApiGetDocumentInformation() { _onApiGetPageInfo() {
return { 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; const scanningOptions = this._options.scanning;
if (error !== null) { if (error !== null) {
@ -265,7 +265,7 @@ class Frontend {
} if (type !== null) { } if (type !== null) {
this._stopClearSelectionDelayed(); this._stopClearSelectionDelayed();
const focus = (cause === 'mouseMove'); const focus = (cause === 'mouseMove');
this._showContent(textSource, focus, definitions, type, sentence, optionsContext); this._showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext);
} else { } else {
if (scanningOptions.autoHideResults) { if (scanningOptions.autoHideResults) {
this._clearSelectionDelayed(scanningOptions.hideDelay, false); this._clearSelectionDelayed(scanningOptions.hideDelay, false);
@ -497,8 +497,9 @@ class Frontend {
this._showPopupContent(textSource, null); this._showPopupContent(textSource, null);
} }
_showContent(textSource, focus, definitions, type, sentence, optionsContext) { _showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext) {
const query = textSource.text(); const query = textSource.text();
const {url} = optionsContext;
const details = { const details = {
focus, focus,
history: false, history: false,
@ -509,8 +510,10 @@ class Frontend {
}, },
state: { state: {
focusEntry: 0, focusEntry: 0,
optionsContext,
url,
sentence, sentence,
optionsContext documentTitle
}, },
content: { content: {
definitions definitions
@ -624,15 +627,19 @@ class Frontend {
} }
let url = window.location.href; let url = window.location.href;
let documentTitle = document.title;
if (this._useProxyPopup) { if (this._useProxyPopup) {
try { try {
url = await api.crossFrame.invoke(this._parentFrameId, 'getUrl', {}); ({url, documentTitle} = await api.crossFrame.invoke(this._parentFrameId, 'getPageInfo', {}));
} catch (e) { } catch (e) {
// NOP // NOP
} }
} }
const depth = this._depth; 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; return this._japaneseUtil;
} }
get depth() {
return this._depth;
}
async prepare() { async prepare() {
// State setup // State setup
const {documentElement} = document; 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) { registerActions(actions) {
for (const [name, handler] of actions) { for (const [name, handler] of actions) {
this._actions.set(name, handler); this._actions.set(name, handler);
@ -466,8 +463,10 @@ class Display extends EventDispatcher {
clone(this._history.state) : clone(this._history.state) :
{ {
focusEntry: 0, focusEntry: 0,
optionsContext: this._optionsContext,
url: window.location.href,
sentence: {text: query, offset: 0}, sentence: {text: query, offset: 0},
url: window.location.href documentTitle: document.title
} }
); );
const details = { const details = {
@ -713,7 +712,9 @@ class Display extends EventDispatcher {
e.preventDefault(); e.preventDefault();
if (!this._historyHasState()) { return; } 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 optionsContext = this.getOptionsContext();
const query = e.currentTarget.textContent; const query = e.currentTarget.textContent;
const definitions = await api.kanjiFind(query, optionsContext); const definitions = await api.kanjiFind(query, optionsContext);
@ -723,8 +724,10 @@ class Display extends EventDispatcher {
params: this._createSearchParams('kanji', query, false), params: this._createSearchParams('kanji', query, false),
state: { state: {
focusEntry: 0, focusEntry: 0,
optionsContext,
url,
sentence, sentence,
optionsContext documentTitle
}, },
content: { content: {
definitions definitions
@ -908,15 +911,21 @@ class Display extends EventDispatcher {
changeHistory = true; 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 focusEntry !== 'number') { focusEntry = 0; }
if (typeof url !== 'string') { url = window.location.href; }
if (!(typeof optionsContext === 'object' && optionsContext !== null)) { if (!(typeof optionsContext === 'object' && optionsContext !== null)) {
optionsContext = this.getOptionsContext(); optionsContext = this.getOptionsContext();
state.optionsContext = optionsContext; state.optionsContext = optionsContext;
changeHistory = true; changeHistory = true;
} }
let {url} = optionsContext;
if (typeof url !== 'string') { url = window.location.href; }
sentence = this._getValidSentenceData(sentence); sentence = this._getValidSentenceData(sentence);
this._setFullQuery(queryFull); this._setFullQuery(queryFull);
@ -1094,7 +1103,7 @@ class Display extends EventDispatcher {
let states; let states;
try { try {
if (this._options.anki.checkForDuplicates) { if (this._options.anki.checkForDuplicates) {
const noteContext = await this._getNoteContext(); const noteContext = this._getNoteContext();
states = await this._areDefinitionsAddable(definitions, modes, noteContext); states = await this._areDefinitionsAddable(definitions, modes, noteContext);
} else { } else {
if (!await api.isAnkiConnected()) { if (!await api.isAnkiConnected()) {
@ -1208,7 +1217,7 @@ class Display extends EventDispatcher {
const overrideToken = this._progressIndicatorVisible.setOverride(true); const overrideToken = this._progressIndicatorVisible.setOverride(true);
try { try {
const noteContext = await this._getNoteContext(); const noteContext = this._getNoteContext();
const note = await this._createNote(definition, mode, noteContext, true); const note = await this._createNote(definition, mode, noteContext, true);
const noteId = await api.addAnkiNote(note); const noteId = await api.addAnkiNote(note);
if (noteId) { if (noteId) {
@ -1371,8 +1380,15 @@ class Display extends EventDispatcher {
return elementRect.top - documentRect.top; return elementRect.top - documentRect.top;
} }
async _getNoteContext() { _getNoteContext() {
const documentTitle = await this.getDocumentTitle(); const {state} = this._history;
let documentTitle = null;
if (typeof state === 'object' && state !== null) {
({documentTitle} = state);
}
if (typeof documentTitle !== 'string') {
documentTitle = '';
}
return { return {
document: { document: {
title: documentTitle title: documentTitle
@ -1728,15 +1744,6 @@ class Display extends EventDispatcher {
parent.removeChild(textarea); parent.removeChild(textarea);
} }
async _getRootFrameDocumentTitle() {
try {
const {title} = await api.crossFrame.invoke(0, 'getDocumentInformation');
return title;
} catch (e) {
return '';
}
}
_addMultipleEventListeners(container, selector, ...args) { _addMultipleEventListeners(container, selector, ...args) {
for (const node of container.querySelectorAll(selector)) { for (const node of container.querySelectorAll(selector)) {
this._eventListeners.addEventListener(node, ...args); this._eventListeners.addEventListener(node, ...args);
@ -1815,6 +1822,8 @@ class Display extends EventDispatcher {
if (type === null) { return; } if (type === null) { return; }
const query = textSource.text(); const query = textSource.text();
const url = window.location.href;
const documentTitle = document.title;
const details = { const details = {
focus: false, focus: false,
history: true, history: true,
@ -1825,8 +1834,10 @@ class Display extends EventDispatcher {
}, },
state: { state: {
focusEntry: 0, focusEntry: 0,
optionsContext,
url,
sentence, sentence,
optionsContext documentTitle
}, },
content: { content: {
definitions definitions