Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect badge spam and more results not working. #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions js/ddg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { getWikis, getNativeSettings } from './util.js';
import {
prepareWikisInfo,
invokeSearchModule,
awaitElement
} from './baseSearch.js';


Expand Down Expand Up @@ -171,40 +170,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 => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're here, have another commit address this being a closure that's later being passed 'as' a bool. There's no reason for this to be a closure.

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 All @@ -213,18 +211,23 @@ 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
} );

}
} );