diff --git a/js/ddg.js b/js/ddg.js index 92a694b..e3ebd15 100644 --- a/js/ddg.js +++ b/js/ddg.js @@ -5,7 +5,8 @@ import { getWikis, getNativeSettings } from './util.js'; import { prepareWikisInfo, invokeSearchModule, - awaitElement + awaitElement, + observeMutation } from './baseSearch.js'; @@ -185,6 +186,7 @@ const rewrite = { this.rewriteLink( wiki, a ); } + // Rewrite title and append a badge for ( const span of element.querySelectorAll( this.SPAN_TITLE_ELEMENT_SELECTOR ) ) { if ( !wiki.search.titlePattern.test( span.textContent ) ) { @@ -194,12 +196,14 @@ const rewrite = { this.rewriteSpan( wiki, span ); } - element.querySelector(this.SPAN_TITLE_ELEMENT_SELECTOR).appendChild( this.makeBadgeElement( isTopLevel ) ); + + element.querySelector( this.SPAN_TITLE_ELEMENT_SELECTOR ).appendChild( this.makeBadgeElement( isTopLevel ) ); // Rewrite URL element for ( const url of element.querySelectorAll( this.ANCHOR_ELEMENT_SELECTOR ) ) { this.rewriteURLElement( wiki, url ); } + this.lock( element ); } } @@ -207,27 +211,25 @@ const rewrite = { }; - getNativeSettings().local.get( [ 'ddgEnable' ], result => { if ( result.ddgEnable || result.ddgEnable === undefined ) { - awaitElement( - document.getElementById( 'react-layout' ), - 'div > div > section', - sectionNode => { - awaitElement( - sectionNode, - '.react-results--main', - node => { - invokeSearchModule( wikis, rewrite.run.bind( rewrite ), filter.run.bind( filter ), node ); + const moreResultsContainer = document.getElementById( 'react-layout' ); + const dynamicObserver = new MutationObserver( updates => { + for ( const update of updates ) { + if ( update.addedNodes && update.addedNodes.length > 0 ) { + for ( const addedNode of update.addedNodes ) { + invokeSearchModule( wikis, rewrite.run.bind( rewrite ), filter.run.bind( filter ) ); } - ); + } } - ); + } ); + + dynamicObserver.observe( moreResultsContainer, { + childList: true, + subtree: true + } ); + } } ); -document.querySelector('#react-layout').addEventListener("click", (event) => { - if( event.target.id === 'more-results') { - invokeSearchModule( wikis, rewrite.run.bind( rewrite ), filter.run.bind( filter ) ); - } -}); +