From 53203838e3da5d513a0c72d7440933ec40bdfc77 Mon Sep 17 00:00:00 2001 From: Chris Taylor Date: Mon, 5 Jun 2023 11:51:19 +0100 Subject: [PATCH] fixes #20 --- assets/popup.html | 1 + manifest.json | 2 +- package.json | 2 +- src/background.ts | 8 ++++++++ src/popup.ts | 14 +++++++++++++- src/searchreplace.ts | 23 ++++++++++++++++++++++- 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/assets/popup.html b/assets/popup.html index 4ebe843..b64c64a 100644 --- a/assets/popup.html +++ b/assets/popup.html @@ -164,6 +164,7 @@

History

+
diff --git a/manifest.json b/manifest.json index e686e30..2d2659e 100644 --- a/manifest.json +++ b/manifest.json @@ -44,6 +44,6 @@ "https://*/*" ], "update_url": "http://clients2.google.com/service/update2/crx", - "version": "1.5.2" + "version": "1.5.3" } diff --git a/package.json b/package.json index f8d4404..b083ac4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "search_and_replace", - "version": "1.5.2", + "version": "1.5.3", "resolutions": { "author": "Chris Taylor " }, diff --git a/src/background.ts b/src/background.ts index c2d4aed..ab47c64 100644 --- a/src/background.ts +++ b/src/background.ts @@ -8,6 +8,14 @@ chrome.runtime.onConnect.addListener(function (port) { const storage = result['storage'] as SearchReplaceStorage port.postMessage(storage) }) + } else if (msg['clearHistory'] === true) { + chrome.storage.local.get(['storage'], function (result) { + const storage = result['storage'] as SearchReplaceStorage + storage.storage.history = [] + chrome.storage.local.set(storage, function () { + port.postMessage('History cleared') + }) + }) } else { const instance: SearchReplaceInstance = msg.instance const history: SearchReplaceInstance[] = msg.history || [] diff --git a/src/popup.ts b/src/popup.ts index 3811f30..d9e4a83 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -51,13 +51,14 @@ document.addEventListener('DOMContentLoaded', function () { }) ;(document.querySelector('#historyHeader')).addEventListener('click', historyHeaderClickHandler) - //Click events for Replace Next, Replace All buttons and Help link + //Click events for Replace Next, Replace All, Help link, and Clear History ;(document.querySelector('#next')).addEventListener('click', function () { clickHandler('searchReplace', false, tabQueryCallback) }) ;(document.querySelector('#all')).addEventListener('click', function () { clickHandler('searchReplace', true, tabQueryCallback) }) + ;(document.querySelector('#clearHistory')).addEventListener('click', clearHistoryClickHandler) ;(document.getElementById('help')).addEventListener('click', openHelp) // Handlers for input elements changing value - storeTerms @@ -84,6 +85,17 @@ function historyHeaderClickHandler(e) { } } +function clearHistoryClickHandler() { + const port = tabConnect() + port.postMessage({ + clearHistory: true, + }) + const historyList = document.getElementById('historyList') + if (historyList) { + historyList.innerHTML = '' + } +} + function restoreSearchReplaceInstance(searchReplaceInstance: SearchReplaceInstance) { ;(document.getElementById('searchTerm')).value = searchReplaceInstance.searchTerm ;(document.getElementById('replaceTerm')).value = searchReplaceInstance.replaceTerm diff --git a/src/searchreplace.ts b/src/searchreplace.ts index 6e8d22b..d01c332 100644 --- a/src/searchreplace.ts +++ b/src/searchreplace.ts @@ -24,6 +24,27 @@ function replaceInInnerHTML(element: HTMLElement, searchPattern: RegExp, replace return !(element.innerHTML === searchStr) } +function setNativeValue(element, value) { + const valueFn = Object.getOwnPropertyDescriptor(element, 'value') + let valueSetter: ((v: any) => void) | undefined + let prototypeValueSetter: ((v: any) => void) | undefined + if (valueFn) { + valueSetter = valueFn.set + } + const prototype = Object.getPrototypeOf(element) + const prototypeValueFn = Object.getOwnPropertyDescriptor(prototype, 'value') + if (prototypeValueFn) { + prototypeValueSetter = prototypeValueFn.set + } + if (valueSetter && prototypeValueSetter && valueSetter !== prototypeValueSetter) { + prototypeValueSetter.call(element, value) + } else if (valueSetter) { + valueSetter.call(element, value) + } else { + element.value = value + } +} + function replaceInInput( input: HTMLInputElement, searchPattern: RegExp, @@ -42,7 +63,7 @@ function replaceInInput( } input.focus() - input.value = newValue + setNativeValue(input, newValue) if (usesKnockout) { const knockoutValueChanger = getKnockoutValueChanger(input.id, newValue)