From 114edbb231e8a3b33c32ea830859bd12ee887410 Mon Sep 17 00:00:00 2001 From: Chris Taylor Date: Sat, 14 Oct 2023 17:15:47 +0100 Subject: [PATCH] more bug fixes for swapping and term counts --- src/popup.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/popup.ts b/src/popup.ts index 6d2f4bb..ad84da4 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -22,11 +22,17 @@ const INPUT_ELEMENTS_AND_EVENTS = { help: ['click'], } +function getSearchTermElement() { + return document.getElementById('searchTerm') +} + +function getReplaceTermElement() { + return document.getElementById('replaceTerm') +} + const CHECKBOXES: SearchReplaceCheckboxNames[] = Object.values(SearchReplaceCheckboxNames) const MIN_SEARCH_TERM_LENGTH = 1 window.addEventListener('DOMContentLoaded', function () { - // Create a variable for storing the time since last time terms were stored - // Set the onchange and onkeydown functions for the input fields const inputs: HTMLCollectionOf = document.getElementsByClassName('data_field') for (const el of inputs) { @@ -98,8 +104,8 @@ window.addEventListener('DOMContentLoaded', function () { // Click handler for swapping terms ;(document.getElementById('swapTerms')).addEventListener('click', function (e) { - const searchTerm = document.getElementById('searchTerm') - const replaceTerm = document.getElementById('replaceTerm') + const searchTerm = getSearchTermElement() + const replaceTerm = getReplaceTermElement() swapTerms(searchTerm, replaceTerm) autoGrow(searchTerm) autoGrow(replaceTerm) @@ -140,9 +146,8 @@ function clearHistoryClickHandler() { } function restoreSearchReplaceInstance(searchReplaceInstance: SearchReplaceInstance) { - ;(document.getElementById('searchTerm')).value = searchReplaceInstance.searchTerm - ;(document.getElementById('replaceTerm')).value = searchReplaceInstance.replaceTerm - + getSearchTermElement().value = searchReplaceInstance.searchTerm + getReplaceTermElement().value = searchReplaceInstance.replaceTerm for (const checkbox of CHECKBOXES) { ;(document.getElementById(checkbox)).checked = searchReplaceInstance.options[checkbox] } @@ -219,10 +224,12 @@ export async function tabQuery( function tabQueryCallback(msg) { removeLoader() if (msg && 'inIframe' in msg && msg['inIframe'] === false) { - if ('searchTermCount' in msg) { + if ('searchTermCount' in msg && getSearchTermElement().value.length >= MIN_SEARCH_TERM_LENGTH) { ;(( document.getElementById('searchTermCount') )).innerHTML = `${msg['searchTermCount']} matches` + } else { + ;(document.getElementById('searchTermCount')).innerHTML = '' } const hintsElement = document.getElementById('hints') @@ -248,7 +255,7 @@ function removeLoader() { content!.style.display = 'block' } -async function storeTerms(e, save?: boolean) { +async function storeTerms(e, save?: boolean, ignoreLength?: boolean) { console.debug('storing terms') e = e || window.event if (e.keyCode === 13) { @@ -258,7 +265,7 @@ async function storeTerms(e, save?: boolean) { const searchReplaceInput = getInputValues(false) const history = constructSearchReplaceHistory() - if (searchReplaceInput.searchTerm.length >= MIN_SEARCH_TERM_LENGTH) { + if (searchReplaceInput.searchTerm.length >= MIN_SEARCH_TERM_LENGTH || ignoreLength) { // This does the actual search replace const url = await tabQuery('store', searchReplaceInput, history, tabQueryCallback) // This sends the search replace terms to the background page and stores them @@ -338,8 +345,8 @@ function swapTerms(source: HTMLTextAreaElement, target: HTMLTextAreaElement) { const sourceText = source.value source.value = target.value target.value = sourceText - storeTerms({}, true) - .then((r) => console.log(r)) + storeTerms({}, true, true) + .then((r) => console.log(`swapTerms response: ${r}`)) .catch((e) => console.error(e)) } @@ -363,8 +370,8 @@ function getUniqueHistoryItems(historyItems: SearchReplaceInstance[]) { } function getInputValues(replaceAll: boolean): SearchReplaceInstance { - const searchTerm = (document.getElementById('searchTerm')).value || '' - const replaceTerm = (document.getElementById('replaceTerm')).value || '' + const searchTerm = getSearchTermElement().value || '' + const replaceTerm = getReplaceTermElement().value || '' const matchCase = (document.getElementById('matchCase')).checked const inputFieldsOnly = (document.getElementById('inputFieldsOnly')).checked const visibleOnly = (document.getElementById('visibleOnly')).checked