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

feat(bing): Bing search engine, using new architecture #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export default function () {
},
ddg: {
mode: 'rewrite'
},
bing: {
mode: 'rewrite'
}

}
};
}
5 changes: 5 additions & 0 deletions js/popup/SearchFilterSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export default class SearchFilterSettings {
supportsFilter: true,
supportsRewrite: true,
},
{
id: 'bing',
supportsFilter: true,
supportsRewrite: true,
}
];


Expand Down
133 changes: 133 additions & 0 deletions js/search/bing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict';


import { getWikis } from '../util.js';
import {
GenericSearchModule,
prepareWikisInfo,
crawlUntilParentFound,
awaitElement,
RewriteUtil
} from './baseSearch.js';
import { constructRedirectBadge, constructReplacementMarker } from './components.js';


const wikis = prepareWikisInfo( getWikis( false, true ), {
titles: true,
selectors: true
} );


class DdgSearchModule extends GenericSearchModule {
ENGINE_LAYOUT_SELECTOR = '#b_results';
RESULT_CONTAINER_SELECTOR = 'li.b_algo, div.slide';
ORDINARY_RESULT_CLASS_NAME = 'b_algo';
URL_ELEMENT_SELECTOR = 'h2#b_topTitle';
SPAN_TITLE_ELEMENT_SELECTOR = 'h2 > a';
ANCHOR_ELEMENT_SELECTOR = 'div > cite';
NETWORK_NAME_ELEMENT_SELECTOR = '.tptt';
BLACKLIST = 'slide'; // TODO: This should be a list
/**
* @protected
* @return {string}
*/
getId() {
return 'bing';
}


/**
* Finds a general result container for a given element, if any.
*
* @protected
* @param {HTMLElement} element Element to find result container for.
* @return {HTMLElement?}
*/
resolveResultContainer( element ) {
return crawlUntilParentFound( element, this.RESULT_CONTAINER_SELECTOR );
}

/**
* @protected
* @param {SiteRecord} wikiInfo
* @param {HTMLElement} boundaryElement
* @return {HTMLElement?}
*/

findNearestGgResult( wikiInfo, boundaryElement ) {
for ( const node of document.querySelectorAll( wikiInfo.search.goodSelector ) ) {
if ( node.compareDocumentPosition( boundaryElement ) & 0x02 ) {
return crawlUntilParentFound( node, this.ORDINARY_RESULT_CLASS_NAME );
}
}
return null;
}

/**
* @protected
* @param {SiteRecord} wikiInfo
* @param {HTMLElement} containerElement
* @param {HTMLElement} _foundLinkElement
*/
async hideResult( wikiInfo, containerElement, _foundLinkElement ) {
// This is just a placeholder, what if we had different elements?
if ( containerElement.classList.contains( this.ORDINARY_RESULT_CLASS_NAME ) ) {

// Try to find the first wiki.gg result after this one
const ggResult = this.findNearestGgResult( wikiInfo, containerElement );

let replacement;
if ( ggResult ) {
replacement = ggResult;
} else {
replacement = constructReplacementMarker( wikiInfo );
}
containerElement.textContent = '';
containerElement.append( replacement );
} else {
containerElement.style.display = 'none';
}

}


/**
* @protected
* @param {SiteRecord} wikiInfo
* @param {HTMLElement} containerElement
* @param {HTMLElement} _foundLinkElement
*/
async replaceResult( wikiInfo, containerElement, _foundLinkElement ) {
// Rewrite anchor href links
for ( const a of containerElement.getElementsByTagName( 'a' ) ) {
RewriteUtil.doLink( wikiInfo, a );
}

// Rewrite title and append a badge
// TODO: Maybe use All just incase that we get more elements
const titleSpan = containerElement.querySelector( this.SPAN_TITLE_ELEMENT_SELECTOR );
if ( wikiInfo.search.titlePattern.test( titleSpan.textContent ) ) {
RewriteUtil.doH3( wikiInfo, titleSpan );
}

// Insert our badge
const badgeElement = constructRedirectBadge( {
allMoved: true,
theme: {
fontSize: '60%',
color: '#222'
}
} );
titleSpan.appendChild( badgeElement );

// Rewrite URL breadcrumb
for ( const url of containerElement.querySelectorAll( this.ANCHOR_ELEMENT_SELECTOR ) ) {
RewriteUtil.doUrlSpan( wikiInfo, url );
}

// Rewrite network name
containerElement.querySelector( this.NETWORK_NAME_ELEMENT_SELECTOR ).textContent = 'wiki.gg';
}
}

DdgSearchModule.invoke( wikis );
15 changes: 14 additions & 1 deletion manifests/chrome_dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,18 @@
"built/fandom.js"
],
"run_at": "document_end"
},
{
"matches": [
"https://www.bing.com/*"
],
"js": [
"built/bing.js"
],
"run_at": "document_end"
}


],
"icons": {
"128": "icons/128_dev.png"
Expand Down Expand Up @@ -113,7 +124,9 @@
"https://www.google.nl/*",
"https://www.google.pl/*",
"https://www.google.pt/*",
"https://duckduckgo.com/*"
"https://duckduckgo.com/*",
"https://www.bing.com/*",
"https://www.startpage.com/*"
]
}
],
Expand Down
1 change: 1 addition & 0 deletions rollup-config-background.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const scripts = [
{ script: 'popup' },
{ script: 'search/google', output: 'google', isContentScript: true },
{ script: 'search/ddg', output: 'ddg', isContentScript: true },
{ script: 'search/bing', output: 'bing', isContentScript: true },
{ script: 'fandom', isContentScript: true },
];

Expand Down
Loading