Skip to content

Commit

Permalink
Merge pull request #21 from forgetso/20-add-option-to-clear-history
Browse files Browse the repository at this point in the history
fixes #20
  • Loading branch information
forgetso authored Jun 5, 2023
2 parents 9a33900 + 5320383 commit a8a2359
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions assets/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ <h4>History</h4>
<div id="historyContent">
<ul id="historyList">
</ul>
<button id="clearHistory">Clear History</button>
</div>
</div>
<!-- Help Link -->
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
"https://*/*"
],
"update_url": "http://clients2.google.com/service/update2/crx",
"version": "1.5.2"
"version": "1.5.3"

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "search_and_replace",
"version": "1.5.2",
"version": "1.5.3",
"resolutions": {
"author": "Chris Taylor <[email protected]>"
},
Expand Down
8 changes: 8 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []
Expand Down
14 changes: 13 additions & 1 deletion src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ document.addEventListener('DOMContentLoaded', function () {
})
;(<HTMLButtonElement>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
;(<HTMLButtonElement>document.querySelector('#next')).addEventListener('click', function () {
clickHandler('searchReplace', false, tabQueryCallback)
})
;(<HTMLButtonElement>document.querySelector('#all')).addEventListener('click', function () {
clickHandler('searchReplace', true, tabQueryCallback)
})
;(<HTMLButtonElement>document.querySelector('#clearHistory')).addEventListener('click', clearHistoryClickHandler)
;(<HTMLAnchorElement>document.getElementById('help')).addEventListener('click', openHelp)

// Handlers for input elements changing value - storeTerms
Expand All @@ -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) {
;(<HTMLInputElement>document.getElementById('searchTerm')).value = searchReplaceInstance.searchTerm
;(<HTMLInputElement>document.getElementById('replaceTerm')).value = searchReplaceInstance.replaceTerm
Expand Down
23 changes: 22 additions & 1 deletion src/searchreplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,7 +63,7 @@ function replaceInInput(
}

input.focus()
input.value = newValue
setNativeValue(input, newValue)

if (usesKnockout) {
const knockoutValueChanger = getKnockoutValueChanger(input.id, newValue)
Expand Down

0 comments on commit a8a2359

Please sign in to comment.