@@ -621,7 +621,7 @@ Call UseGlobal directly, no need to import
621621 } ) ;
622622 } ) ;
623623 }
624- function deleteElements ( selector , willReRun ) {
624+ function deleteElements ( selector , once ) {
625625 onElementsVisible (
626626 selector ,
627627 ( nodes ) => {
@@ -630,21 +630,35 @@ Call UseGlobal directly, no need to import
630630 console . log ( "Useful-scripts: element removed " , node ) ;
631631 } ) ;
632632 } ,
633- willReRun
633+ once
634634 ) ;
635635 }
636636 function waitForElements ( selector ) {
637637 return new Promise ( ( resolve , reject ) => {
638- onElementsVisible ( selector , resolve , false ) ;
638+ onElementsVisible ( selector , resolve , true ) ;
639639 } ) ;
640640 }
641+ // https://stackoverflow.com/a/46428962
642+ function onHrefChanged ( callback , once ) {
643+ let oldHref = document . location . href ;
644+ const body = document . querySelector ( "body" ) ;
645+ const observer = new MutationObserver ( ( mutations ) => {
646+ let curHref = document . location . href ;
647+ if ( oldHref !== curHref ) {
648+ callback ?. ( oldHref , curHref ) ;
649+ oldHref = document . location . href ;
650+ if ( once ) observer . disconnect ( ) ;
651+ }
652+ } ) ;
653+ observer . observe ( body , { childList : true , subtree : true } ) ;
654+ }
641655 // Idea from https://github.com/gys-dev/Unlimited-Stdphim
642656 // https://stackoverflow.com/a/61511955/11898496
643- function onElementsVisible ( selector , callback , willReRun ) {
657+ function onElementsVisible ( selector , callback , once ) {
644658 let nodes = document . querySelectorAll ( selector ) ;
645659 if ( nodes ?. length ) {
646660 callback ( nodes ) ;
647- if ( ! willReRun ) return ;
661+ if ( once ) return ;
648662 }
649663
650664 const observer = new MutationObserver ( ( mutations ) => {
@@ -660,7 +674,7 @@ Call UseGlobal directly, no need to import
660674
661675 if ( n ?. length ) {
662676 callback ( n ) ;
663- if ( ! willReRun ) observer . disconnect ( ) ;
677+ if ( once ) observer . disconnect ( ) ;
664678 }
665679 }
666680 } ) ;
0 commit comments