Skip to content

Commit

Permalink
more bug fixes for swapping and term counts
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Oct 14, 2023
1 parent 10582a4 commit 114edbb
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ const INPUT_ELEMENTS_AND_EVENTS = {
help: ['click'],
}

function getSearchTermElement() {
return <HTMLTextAreaElement>document.getElementById('searchTerm')
}

function getReplaceTermElement() {
return <HTMLTextAreaElement>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<Element> = document.getElementsByClassName('data_field')
for (const el of inputs) {
Expand Down Expand Up @@ -98,8 +104,8 @@ window.addEventListener('DOMContentLoaded', function () {

// Click handler for swapping terms
;(<HTMLButtonElement>document.getElementById('swapTerms')).addEventListener('click', function (e) {
const searchTerm = <HTMLTextAreaElement>document.getElementById('searchTerm')
const replaceTerm = <HTMLTextAreaElement>document.getElementById('replaceTerm')
const searchTerm = getSearchTermElement()
const replaceTerm = getReplaceTermElement()
swapTerms(searchTerm, replaceTerm)
autoGrow(searchTerm)
autoGrow(replaceTerm)
Expand Down Expand Up @@ -140,9 +146,8 @@ function clearHistoryClickHandler() {
}

function restoreSearchReplaceInstance(searchReplaceInstance: SearchReplaceInstance) {
;(<HTMLInputElement>document.getElementById('searchTerm')).value = searchReplaceInstance.searchTerm
;(<HTMLInputElement>document.getElementById('replaceTerm')).value = searchReplaceInstance.replaceTerm

getSearchTermElement().value = searchReplaceInstance.searchTerm
getReplaceTermElement().value = searchReplaceInstance.replaceTerm
for (const checkbox of CHECKBOXES) {
;(<HTMLInputElement>document.getElementById(checkbox)).checked = searchReplaceInstance.options[checkbox]
}
Expand Down Expand Up @@ -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) {
;(<HTMLDivElement>(
document.getElementById('searchTermCount')
)).innerHTML = `${msg['searchTermCount']} matches`
} else {
;(<HTMLDivElement>document.getElementById('searchTermCount')).innerHTML = ''
}
const hintsElement = document.getElementById('hints')

Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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))
}

Expand All @@ -363,8 +370,8 @@ function getUniqueHistoryItems(historyItems: SearchReplaceInstance[]) {
}

function getInputValues(replaceAll: boolean): SearchReplaceInstance {
const searchTerm = (<HTMLInputElement>document.getElementById('searchTerm')).value || ''
const replaceTerm = (<HTMLInputElement>document.getElementById('replaceTerm')).value || ''
const searchTerm = getSearchTermElement().value || ''
const replaceTerm = getReplaceTermElement().value || ''
const matchCase = (<HTMLInputElement>document.getElementById('matchCase')).checked
const inputFieldsOnly = (<HTMLInputElement>document.getElementById('inputFieldsOnly')).checked
const visibleOnly = (<HTMLInputElement>document.getElementById('visibleOnly')).checked
Expand Down

0 comments on commit 114edbb

Please sign in to comment.