diff --git a/js/ddg.js b/js/ddg.js index 3e7009b..ac6a73d 100644 --- a/js/ddg.js +++ b/js/ddg.js @@ -171,7 +171,7 @@ const rewrite = { run( wiki, linkElement ) { - if ( linkElement !== null && !this.isLocked( linkElement ) ) { + if ( linkElement !== null ) { // Find result container const element = linkElement.closest( 'article' ); @@ -179,32 +179,31 @@ const rewrite = { return a.href.startsWith( `https://${wiki.oldId || wiki.id}.fandom.com` ); }; - if ( element !== null ) { + if ( element !== null && !this.isLocked( element ) ) { // Rewrite anchor href links for ( const a of element.getElementsByTagName( 'a' ) ) { 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 ) ) { continue; } - // TODO: This should get placed at the end if and only if everything is sucessful. - span.parentElement.appendChild( this.makeBadgeElement( isTopLevel ) ); - - this.lock( span.parentElement ); this.rewriteSpan( wiki, span ); - this.lock( span ); } + + 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( linkElement ); + + this.lock( element ); } } }