Skip to content

Commit

Permalink
Initial Find/Replace Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
smashedr committed Sep 14, 2024
1 parent 23ed010 commit 5444508
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/html/links.html
Original file line number Diff line number Diff line change
@@ -58,6 +58,15 @@ <h2 id="links">Links <span class="badge bg-success-subtle"><span id="links-count
</div>
</div>
</div> <!-- links-buttons -->

<div class="input-group my-2">
<span class="input-group-text">Find</span>
<input id="reFind" type="text" class="form-control" placeholder="Find" aria-label="Find">
<span class="input-group-text">Replace</span>
<input id="reReplace" type="text" class="form-control" placeholder="Replace" aria-label="Replace">
<button id="reExecute" class="btn btn-outline-warning" type="button">Execute</button>
</div>

<div class="table-wrapper">
<table id="links-table" class="table table-sm table-striped table-hover small w-100" data-counter="links-count">
<thead class="">
31 changes: 30 additions & 1 deletion src/js/links.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { openURL, textFileDownload } from './exports.js'

window.addEventListener('keydown', handleKeyboard)
document.addEventListener('DOMContentLoaded', initLinks)

document.getElementById('reExecute').addEventListener('click', reExecuteClick)
document
.querySelectorAll('.copy-links')
.forEach((el) => el.addEventListener('click', copyLinksClick))
@@ -307,6 +307,35 @@ function dtVisibility(e, settings, column, state) {
linksTable.rows().invalidate().draw()
}

/**
* Copy links Button Click Callback
* @function reExecuteClick
* @param {MouseEvent} event
*/
async function reExecuteClick(event) {
console.debug('reExecuteClick:', event)
const { options } = await chrome.storage.sync.get(['options'])
const find = document.getElementById('reFind').value
const replace = document.getElementById('reReplace').value
console.debug('find:', find)
console.debug('replace:', replace)
const re = new RegExp(find, options.flags)
console.debug('re:', re)
const links = document.getElementById('links-body').querySelectorAll('a')
for (const link of links) {
console.debug('href:', link.href)
const matches = link.href.match(re)
console.debug('matches:', matches)
matches.forEach((match, i) => {
console.debug(`match ${i}:`, match)
const result = replace.replace(`$${i + 1}`, match)
console.debug('result:', result)
link.href = result
link.textContent = result
})
}
}

/**
* Copy links Button Click Callback
* @function copyLinksClick

0 comments on commit 5444508

Please sign in to comment.