@@ -4,7 +4,11 @@ import { openURL, textFileDownload } from './exports.js'
44
55window . addEventListener ( 'keydown' , handleKeyboard )
66document . addEventListener ( 'DOMContentLoaded' , initLinks )
7-
7+ document . getElementById ( 'findReplace' ) . addEventListener ( 'submit' , findReplace )
8+ document . getElementById ( 'reReset' ) . addEventListener ( 'click' , reResetClick )
9+ document
10+ . getElementsByName ( 'reType' )
11+ . forEach ( ( el ) => el . addEventListener ( 'change' , reTypeChange ) )
812document
913 . querySelectorAll ( '.copy-links' )
1014 . forEach ( ( el ) => el . addEventListener ( 'click' , copyLinksClick ) )
@@ -14,6 +18,19 @@ document
1418document
1519 . querySelectorAll ( '.open-in-tabs' )
1620 . forEach ( ( el ) => el . addEventListener ( 'click' , openLinksClick ) )
21+ document
22+ . querySelectorAll ( '[data-bs-toggle="tooltip"]' )
23+ . forEach ( ( el ) => new bootstrap . Tooltip ( el ) )
24+
25+ const findCollapse = document . getElementById ( 'findCollapse' )
26+ findCollapse . addEventListener ( 'show.bs.collapse' , ( ) => {
27+ console . debug ( 'Show Collapse' )
28+ localStorage . setItem ( 'findCollapse' , 'shown' )
29+ } )
30+ findCollapse . addEventListener ( 'hide.bs.collapse' , ( ) => {
31+ console . debug ( 'Hide Collapse' )
32+ localStorage . setItem ( 'findCollapse' , 'hidden' )
33+ } )
1734
1835const urlParams = new URLSearchParams ( window . location . search )
1936
@@ -109,6 +126,7 @@ function genUrl(url) {
109126 link . text = url
110127 link . href = url
111128 link . title = url
129+ link . dataset . original = url
112130 link . target = '_blank'
113131 link . rel = 'noopener'
114132 return link
@@ -155,6 +173,21 @@ async function initLinks() {
155173 window . close ( )
156174 }
157175
176+ const collapse = localStorage . getItem ( 'findCollapse' )
177+ console . debug ( 'collapse:' , collapse )
178+ if ( collapse === 'shown' ) {
179+ // const bsCollapse = new bootstrap.Collapse(findCollapse, {
180+ // toggle: false,
181+ // })
182+ // bsCollapse.show()
183+ findCollapse . classList . add ( 'show' )
184+ }
185+ const type = localStorage . getItem ( 'reType' )
186+ console . debug ( 'type:' , type )
187+ if ( type ) {
188+ document . getElementById ( type ) . checked = true
189+ }
190+
158191 const { patterns } = await chrome . storage . sync . get ( [ 'patterns' ] )
159192 if ( patterns . length ) {
160193 const datalist = document . createElement ( 'datalist' )
@@ -307,6 +340,98 @@ function dtVisibility(e, settings, column, state) {
307340 linksTable . rows ( ) . invalidate ( ) . draw ( )
308341}
309342
343+ /**
344+ * Find and Replace Submit Callback
345+ * @function findReplace
346+ * @param {SubmitEvent } event
347+ */
348+ async function findReplace ( event ) {
349+ console . debug ( 'findReplace:' , event )
350+ event . preventDefault ( )
351+ const find = event . target . elements . reFind . value
352+ const replace = event . target . elements . reReplace . value
353+ console . debug ( 'find:' , find )
354+ console . debug ( 'replace:' , replace )
355+ if ( ! find ) {
356+ showToast ( 'You must enter a find value.' , 'danger' )
357+ return
358+ }
359+ const re = new RegExp ( find , 'gm' )
360+ console . debug ( 're:' , re )
361+ // const type = document.querySelector('input[name="reType"]:checked').value
362+ const type = event . target . elements . reType . value
363+ console . debug ( 'type:' , type )
364+ const links = document . getElementById ( 'links-body' ) . querySelectorAll ( 'a' )
365+ let count = 0
366+ for ( const link of links ) {
367+ const before = link . href
368+ console . debug ( 'before:' , before )
369+ if ( type === 'normal' ) {
370+ const result = link . href . replace ( find , replace )
371+ console . debug ( 'result:' , result )
372+ link . href = result
373+ link . textContent = result
374+ } else if ( type === 'regex' ) {
375+ const result = link . href . replace ( re , replace )
376+ console . debug ( 'result:' , result )
377+ link . href = result
378+ link . textContent = result
379+ } else if ( type === 'groups' ) {
380+ const matches = link . href . match ( re )
381+ console . debug ( 'matches:' , matches )
382+ if ( matches ) {
383+ matches . forEach ( ( match , i ) => {
384+ console . debug ( `match ${ i } :` , match )
385+ const result = replace . replace ( `$${ i + 1 } ` , match )
386+ console . debug ( 'result:' , result )
387+ link . href = result
388+ link . textContent = result
389+ } )
390+ }
391+ }
392+ const after = link . getAttribute ( 'href' )
393+ console . debug ( 'after:' , after )
394+ if ( after !== before ) {
395+ count ++
396+ }
397+ }
398+ const status = count ? 'success' : 'warning'
399+ showToast ( `Updated ${ count } Links.` , status )
400+ if ( count ) {
401+ document . getElementById ( 'reReset' ) . classList . remove ( 'disabled' )
402+ }
403+ }
404+
405+ /**
406+ * Reset Regex Click Callback
407+ * @function reResetClick
408+ * @param {MouseEvent } event
409+ */
410+ async function reResetClick ( event ) {
411+ console . debug ( 'reResetClick:' , event )
412+ event . currentTarget . classList . add ( 'disabled' )
413+ document
414+ . getElementById ( 'links-body' )
415+ . querySelectorAll ( 'a' )
416+ . forEach ( ( el ) => {
417+ console . debug ( 'el.dataset.original:' , el . dataset . original )
418+ el . href = el . dataset . original
419+ el . textContent = el . dataset . original
420+ } )
421+ showToast ( 'Links reset to original values.' )
422+ }
423+
424+ /**
425+ * Regex Type Change Callback
426+ * @function reTypeChange
427+ * @param {InputEvent } event
428+ */
429+ async function reTypeChange ( event ) {
430+ // console.debug('reTypeChange:', event)
431+ console . debug ( 'reTypeChange id:' , event . target . id )
432+ localStorage . setItem ( 'reType' , event . target . id )
433+ }
434+
310435/**
311436 * Copy links Button Click Callback
312437 * @function copyLinksClick
0 commit comments