From b7c9fa105764eb2cd5befea86c98fe49f5763a1d Mon Sep 17 00:00:00 2001
From: toasted-nutbread <toasted-nutbread@users.noreply.github.com>
Date: Tue, 12 Jan 2021 18:04:26 -0500
Subject: 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
---
 ext/mixed/js/display.js | 65 +++++++++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 27 deletions(-)

(limited to 'ext/mixed/js')

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
-- 
cgit v1.2.3