diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-01-12 18:04:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-12 18:04:26 -0500 |
commit | b7c9fa105764eb2cd5befea86c98fe49f5763a1d (patch) | |
tree | 59e9dec9f3b1b08b8e252206a32957385c3907ca /ext | |
parent | 983e2c79361ad15ada6aae0d153fef9f1b867a93 (diff) |
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
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/search.js | 7 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 27 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 65 |
3 files changed, 61 insertions, 38 deletions
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 1417bdb5..991acf2b 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -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, diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index e6f72689..8b2df1b0 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -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} + }; } } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 509683a6..a76e2b71 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -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 |