diff --git a/src/excmds.ts b/src/excmds.ts index 9e13a8f7..18d7b876 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -254,9 +254,9 @@ export async function rssexec(url: string, type?: string, ...title: string[]) { * That said, `bind gs fillinput null [Tridactyl](https://addons.mozilla.org/en-US/firefox/addon/tridactyl-vim/) is my favourite add-on` could probably come in handy. */ //#content -export function fillinput(selector: string, ...content: string[]) { +export async function fillinput(selector: string, ...content: string[]) { let inputToFill = document.querySelector(selector) - if (!inputToFill) inputToFill = DOM.getLastUsedInput() + if (!inputToFill) inputToFill = await DOM.getLastUsedInput() if ("value" in inputToFill) { ;(inputToFill as HTMLInputElement).value = content.join(" ") } else { @@ -277,14 +277,14 @@ export function getInput(e: HTMLElement) { /** @hidden */ //#content -export function getinput() { - return getInput(DOM.getLastUsedInput()) +export async function getinput() { + return getInput(await DOM.getLastUsedInput()) } /** @hidden */ //#content -export function getInputSelector() { - return DOM.getSelector(DOM.getLastUsedInput()) +export async function getInputSelector() { + return DOM.getSelector(await DOM.getLastUsedInput()) } /** @hidden */ @@ -324,7 +324,7 @@ export function removeTridactylEditorClass(selector: string) { */ //#content export async function editor() { - const elem = DOM.getLastUsedInput() + const elem = await DOM.getLastUsedInput() const selector = DOM.getSelector(elem) addTridactylEditorClass(selector) @@ -1987,19 +1987,21 @@ input[type='password'] * "-b": biggest input field */ //#content -export function focusinput(nth: number | string) { +export async function focusinput(nth: number | string) { let inputToFocus: HTMLElement = null // set to false to avoid falling back on the first available input // if a special finder fails let fallbackToNumeric = true + const lastInput = await DOM.getLastUsedInput() + // nth = "-l" -> use the last used input for this page if (nth === "-l") { // try to recover the last used input stored as a // DOM node, which should be exactly the one used before (or null) - if (DOM.getLastUsedInput()) { - inputToFocus = DOM.getLastUsedInput() + if (lastInput) { + inputToFocus = lastInput } else { // Pick the first input in the DOM. inputToFocus = DOM.getElemsBySelector(INPUTTAGS_selectors, [DOM.isSubstantial])[0] as HTMLElement @@ -2011,8 +2013,8 @@ export function focusinput(nth: number | string) { // attempt to find next/previous input const inputs = DOM.getElemsBySelector(INPUTTAGS_selectors, [DOM.isSubstantial]) as HTMLElement[] if (inputs.length) { - let index = inputs.indexOf(DOM.getLastUsedInput()) - if (DOM.getLastUsedInput()) { + let index = inputs.indexOf(lastInput) + if (lastInput) { if (nth === "-n") { index++ } else { @@ -2331,7 +2333,7 @@ export function tabqueue(...addresses: string[]) { } return tabopen("-b", addresses[0]).then( tab => - new Promise((resolve) => { + new Promise(resolve => { function openNextTab(activeInfo) { if (activeInfo.tabId === tab.id) { resolve(tabqueue(...addresses.slice(1))) diff --git a/src/lib/dom.ts b/src/lib/dom.ts index 91dd441d..d37deef5 100644 --- a/src/lib/dom.ts +++ b/src/lib/dom.ts @@ -508,12 +508,20 @@ export function focus(e: HTMLElement): void { } } -/** DOM reference to the last used Input field - */ -let LAST_USED_INPUT: HTMLElement = null +export async function getLastUsedInputSelector(): Promise { + return State.getAsync("lastFocusInputSelector") +} -export function getLastUsedInput(): HTMLElement { - return LAST_USED_INPUT +export async function getLastUsedInput(): Promise { + try { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + return document.querySelector( + await getLastUsedInputSelector(), + ) as HTMLElement + } catch (e) { + if (e instanceof DOMException) return undefined + throw e + } } /** WARNING: This function can potentially recieve malicious input! For the @@ -526,7 +534,7 @@ export function getLastUsedInput(): HTMLElement { * */ function onPageFocus(elem: HTMLElement): boolean { if (isTextEditable(elem)) { - LAST_USED_INPUT = elem + state.lastFocusInputSelector = getSelector(elem) } return config.get("allowautofocus") === "true" } @@ -560,7 +568,7 @@ export function setupFocusHandler(): void { // Handles when a user selects an input document.addEventListener("focusin", e => { if (isTextEditable(e.target as HTMLElement)) { - LAST_USED_INPUT = e.target as HTMLElement + state.lastFocusInputSelector = getSelector(e.target as HTMLElement) setInput(e.target as HTMLInputElement) } }) diff --git a/src/state.ts b/src/state.ts index 256cdabb..f76bd560 100644 --- a/src/state.ts +++ b/src/state.ts @@ -28,6 +28,7 @@ class State { }, ] last_ex_str = "echo" + lastFocusInputSelector = "" // used for focusinput, not for changing tabs like prevInputs } // Store these keys in the local browser storage to persist between restarts @@ -91,7 +92,9 @@ const state = new Proxy(overlay, { }, }) -export async function getAsync(property: K): Promise { +export async function getAsync( + property: K, +): Promise { if (notBackground()) return browser.runtime.sendMessage({ type: "state",