Skip to content

Commit

Permalink
fix: fixed DDG redirect badge spamming
Browse files Browse the repository at this point in the history
Cherry-picked-from: #54 c881932
  • Loading branch information
ManagarmrWhisperer authored and alex4401 committed Jan 15, 2024
1 parent 2ff37d5 commit 04dd427
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions js/ddg.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,39 @@ const rewrite = {


run( wiki, linkElement ) {
if ( linkElement !== null && !this.isLocked( linkElement ) ) {
if ( linkElement !== null ) {
// Find result container
const element = linkElement.closest( 'article' );

const isTopLevel = a => {
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 );
}
}
}
Expand Down

0 comments on commit 04dd427

Please sign in to comment.