2023-07-25 06:17:46 +00:00
const currentURL = new URL ( document . location ) ;
2023-08-04 05:50:12 +00:00
let filteredWikis = [ ] ;
2023-08-07 06:43:23 +00:00
let hiddenWikisRevealed = { } ;
2023-07-25 06:17:46 +00:00
// Create object prototypes for getting and setting attributes:
Object . prototype . get = function ( prop ) {
this [ prop ] = this [ prop ] || { } ;
return this [ prop ] ;
} ;
Object . prototype . set = function ( prop , value ) {
this [ prop ] = value ;
}
// Function to create an observer to watch for mutations on search pages
function addLocationObserver ( callback ) {
const config = {
attributes : false ,
childList : true ,
subtree : true
}
const observer = new MutationObserver ( callback ) ;
observer . observe ( document . body , config ) ;
}
// Load website data:
async function getData ( ) {
const LANGS = [ "DE" , "EN" , "ES" , "FR" , "IT" , "PL" , "TOK" ] ;
let sites = [ ] ;
let promises = [ ] ;
for ( let i = 0 ; i < LANGS . length ; i ++ ) {
promises . push ( fetch ( chrome . runtime . getURL ( 'data/sites' + LANGS [ i ] + '.json' ) )
. then ( ( resp ) => resp . json ( ) )
. then ( function ( jsonData ) {
jsonData . forEach ( ( site ) => {
site . origins . forEach ( ( origin ) => {
sites . push ( {
"id" : site . id ,
"origin" : origin . origin ,
2023-08-04 05:50:12 +00:00
"origin_group" : site . origins _label ,
2023-07-25 06:17:46 +00:00
"origin_base_url" : origin . origin _base _url ,
"origin_content_path" : origin . origin _content _path ,
"destination" : site . destination ,
"destination_base_url" : site . destination _base _url ,
"destination_content_path" : site . destination _content _path ,
"destination_content_prefix" : ( site . destination _content _prefix ? site . destination _content _prefix : "" ) ,
"destination_platform" : site . destination _platform ,
"destination_icon" : site . destination _icon ,
"lang" : LANGS [ i ]
} )
} )
} ) ;
} ) ) ;
}
await Promise . all ( promises ) ;
2023-08-04 05:50:12 +00:00
2023-07-25 06:17:46 +00:00
return sites ;
}
2023-08-04 05:50:12 +00:00
function insertCSS ( ) {
// Output CSS
styleString = `
. iwb - notice {
2023-08-07 06:43:23 +00:00
display : block ! important ;
2023-08-04 05:50:12 +00:00
margin : . 5 em . 5 em 1 em . 5 em ! important ;
padding : . 5 em . 5 em . 5 em 1 em ! important ;
border - left : 3 px solid # FFCC33 ! important ;
font - size : 14 px ! important ;
color : white ! important ;
mix - blend - mode : difference ! important ;
}
. iwb - notice a {
text - decoration : underline ! important ;
color : white ! important ;
mix - blend - mode : difference ! important ;
}
. iwb - notice button {
cursor : pointer ! important ;
2023-08-07 06:43:23 +00:00
display : inline - block ! important ;
2023-08-04 05:50:12 +00:00
padding : 2 px 8 px ! important ;
2023-08-07 06:43:23 +00:00
margin : 8 px 8 px 0 0 ! important ;
2023-08-04 05:50:12 +00:00
background - color : transparent ! important ;
2023-08-07 06:43:23 +00:00
border : 1 px solid ! important ;
2023-08-04 05:50:12 +00:00
border - radius : 5 px ! important ;
2023-09-18 07:41:42 +00:00
font - size : 12 px ! important ;
2023-08-04 05:50:12 +00:00
color : white ! important ;
mix - blend - mode : difference ! important ;
2023-08-07 06:43:23 +00:00
text - align : left ! important ;
}
2023-08-04 05:50:12 +00:00
. iwb - hide {
display : none ! important ;
}
. iwb - show {
display : block ! important ;
}
. iwb - notice button : hover {
outline : 1 px solid ! important ;
}
2023-10-16 05:47:02 +00:00
. iwb - new - link {
display : inline - block ;
font - size : 12 px ! important ;
text - decoration : none ;
padding - left : 5 px ;
position : relative ;
}
. iwb - new - link : hover {
text - decoration : none ;
z - index : 9999999 ;
}
. iwb - new - link button {
cursor : pointer ;
color : white ;
background : # 005799 ;
border : 0 px solid # fff ;
border - radius : 10 px ;
padding : 5 px 10 px ;
2023-11-02 08:35:34 +00:00
margin : . 5 em 0 ;
2023-10-16 05:47:02 +00:00
font - size : 1.2 em ;
width : fit - content ;
}
. iwb - new - link button : hover {
background : # 00467 a ;
}
. iwb - new - link button * {
vertical - align : middle ;
}
. iwb - new - link div : first - of - type {
display : inline - block ;
background : white ;
border - radius : 16 px ;
margin - right : 10 px ;
width : fit - content ;
height : fit - content ;
line - height : 12 px ;
padding : 5 px ;
}
. iwb - new - link img {
width : 12 px ;
}
2023-11-02 08:35:34 +00:00
. iwb - disavow > * : not ( [ class ^= "iwb-" ] ) {
2023-10-16 05:47:02 +00:00
opacity : 60 % ;
pointer - events : none ;
cursor : default ;
}
2023-11-02 08:35:34 +00:00
. iwb - disavow > a : not ( [ class ^= "iwb-" ] ) , . iwb - disavow > * : not ( [ class ^= "iwb-" ] ) a , . iwb - disavow > * : not ( [ class ^= "iwb-" ] ) a * {
2023-10-16 05:47:02 +00:00
text - decoration : line - through ! important ;
}
2023-11-02 08:35:34 +00:00
. iwb - enable - result {
display : none ;
padding : 2 px 8 px ;
margin : 2 px 8 px ;
cursor : pointer ;
font - size : 12 px ;
color : white ! important ;
border : 1 px solid white ;
border - radius : 10 px ;
mix - blend - mode : difference ! important ;
}
. iwb - disavow . iwb - enable - result {
display : inline - block ;
}
2023-08-04 05:50:12 +00:00
`
style = document . createElement ( 'style' ) ;
style . textContent = styleString ;
document . head . append ( style ) ;
}
// Function to convert strings to consistent IDs
// Used to convert wiki names to element IDs
function stringToId ( string ) {
2023-08-31 05:16:15 +00:00
return string . replaceAll ( ' ' , '-' ) . replaceAll ( "'" , '' ) . replace ( /\W/g , '' ) . toLowerCase ( ) ;
2023-08-04 05:50:12 +00:00
}
2023-08-08 06:33:23 +00:00
// Function to escape string to use in regex
function escapeRegex ( string ) {
return string . replace ( /[.*+?^${}()|[\]\\]/g , '\\$&' ) ;
}
2023-11-02 08:35:34 +00:00
function replaceSearchResults ( searchResultContainer , site , link ) {
2023-10-16 05:47:02 +00:00
let countFiltered = 0 ;
// Build new URL:
let article = link . split ( site [ 'origin_base_url' ] + site [ 'origin_content_path' ] ) [ 1 ] ? . split ( '#' ) [ 0 ] . split ( '?' ) [ 0 ] . split ( '&' ) [ 0 ] ;
let newURL = '' ;
if ( article ) {
let searchParams = '' ;
switch ( site [ 'destination_platform' ] ) {
case 'mediawiki' :
searchParams = 'Special:Search/' + site [ 'destination_content_prefix' ] + article ;
break ;
case 'doku' :
searchParams = 'start?do=search&q=' + article ;
break ;
}
newURL = 'https://' + site [ "destination_base_url" ] + site [ "destination_content_path" ] + searchParams ;
} else {
newURL = 'https://' + site [ "destination_base_url" ] ;
}
if ( searchResultContainer && ! searchResultContainer . classList . contains ( 'iwb-detected' ) ) {
searchResultContainer . classList . add ( 'iwb-detected' ) ;
var indieResultLink = document . createElement ( 'a' ) ;
indieResultLink . href = newURL ;
indieResultLink . classList . add ( 'iwb-new-link' ) ;
var indieResultButton = document . createElement ( 'button' ) ;
var indieResultFaviconContainer = document . createElement ( 'div' ) ;
var indieResultFavicon = document . createElement ( 'img' ) ;
indieResultFavicon . alt = '' ;
indieResultFavicon . width = '12' ;
indieResultFavicon . height = '12' ;
indieResultFavicon . src = chrome . runtime . getURL ( 'favicons/' + site . lang . toLowerCase ( ) + '/' + site . destination _icon ) ;
indieResultFaviconContainer . append ( indieResultFavicon ) ;
var indieResultText = document . createElement ( 'span' ) ;
if ( article ) {
indieResultText . innerText = 'Look up "' + decodeURIComponent ( decodeURIComponent ( article . replaceAll ( '_' , ' ' ) ) ) + '" on ' + site . destination ;
} else {
indieResultText . innerText = 'Visit ' + site . destination + ' instead' ;
}
indieResultButton . append ( indieResultFaviconContainer ) ;
indieResultButton . append ( indieResultText ) ;
indieResultLink . appendChild ( indieResultButton ) ;
2023-11-02 08:35:34 +00:00
// Output link to re-enable disabled result:
enableResult = document . createElement ( 'div' ) ;
enableResult . classList . add ( 'iwb-enable-result' ) ;
2023-11-02 08:41:33 +00:00
enableResult . innerText = 'Re-enable original result' ;
2023-11-02 08:35:34 +00:00
enableResult . addEventListener ( 'click' , function ( e ) {
e . target . closest ( '.iwb-disavow' ) . classList . remove ( 'iwb-disavow' ) ;
} ) ;
searchResultContainer . prepend ( enableResult ) ;
2023-10-16 05:47:02 +00:00
searchResultContainer . prepend ( indieResultLink ) ;
searchResultContainer . classList . add ( 'iwb-disavow' ) ;
countFiltered ++ ;
}
return countFiltered ;
}
function hideSearchResults ( searchResultContainer , searchEngine , site ) {
let countFiltered = 0 ;
// Insert search result removal notice
if ( ! filteredWikis . includes ( site . lang + ' ' + site . origin _group ) ) {
filteredWikis . push ( site . lang + ' ' + site . origin _group ) ;
let elementId = stringToId ( site . lang + '-' + site . origin _group ) ;
hiddenWikisRevealed [ elementId ] = false ;
let searchRemovalNotice = document . createElement ( 'aside' ) ;
searchRemovalNotice . id = 'iwb-notice-' + elementId ;
searchRemovalNotice . classList . add ( 'iwb-notice' ) ;
let searchRemovalNoticeLink = document . createElement ( 'a' ) ;
searchRemovalNoticeLink . href = 'https://' + site . destination _base _url ;
searchRemovalNoticeLink . textContent = site . destination ;
searchRemovalNoticePretext = document . createTextNode ( 'Indie Wiki Buddy has filtered out results from ' + site . origin _group + ( site . lang !== 'EN' ? ' (' + site . lang + ')' : '' ) + '. Look for results from ' ) ;
searchRemovalNoticePosttext = document . createTextNode ( ' instead!' ) ;
linebreak = document . createElement ( "br" ) ;
searchRemovalNotice . appendChild ( searchRemovalNoticePretext ) ;
searchRemovalNotice . appendChild ( searchRemovalNoticeLink ) ;
searchRemovalNotice . appendChild ( searchRemovalNoticePosttext ) ;
searchRemovalNotice . appendChild ( linebreak ) ;
// Output "show results" button
let showResultsButton = document . createElement ( 'button' ) ;
showResultsButton . classList . add ( 'iwb-show-results-button' ) ;
showResultsButton . setAttribute ( 'data-group' , 'iwb-search-result-' + elementId ) ;
showResultsButton . textContent = 'Show filtered results' ;
searchRemovalNotice . appendChild ( showResultsButton ) ;
showResultsButton . onclick = function ( e ) {
if ( e . target . textContent . includes ( 'Show' ) ) {
e . target . textContent = 'Re-hide filtered results' ;
hiddenWikisRevealed [ elementId ] = true ;
const selector = e . currentTarget . dataset . group ;
document . querySelectorAll ( '.' + selector ) . forEach ( el => {
el . classList . add ( 'iwb-show' ) ;
} )
} else {
e . target . textContent = 'Show filtered results' ;
hiddenWikisRevealed [ elementId ] = false ;
const selector = e . currentTarget . dataset . group ;
document . querySelectorAll ( '.' + selector ) . forEach ( el => {
el . classList . remove ( 'iwb-show' ) ;
} )
}
}
// Output "disable filtering" button
let disableFilterButton = document . createElement ( 'button' ) ;
disableFilterButton . classList . add ( 'iwb-disable-filtering-button' ) ;
disableFilterButton . textContent = 'Stop filtering ' + site . origin _group + ' in future searches' ;
disableFilterButton . style . border = '1px solid' ;
searchRemovalNotice . appendChild ( disableFilterButton ) ;
disableFilterButton . onclick = function ( e ) {
if ( e . target . textContent . includes ( 'Stop' ) ) {
2023-11-02 08:12:35 +00:00
chrome . storage . sync . get ( { 'searchEngineSettings' : { } } , function ( response ) {
response . searchEngineSettings . get ( site . id ) . set ( 'action' , 'disabled' ) ;
chrome . storage . sync . set ( { 'searchEngineSettings' : response . searchEngineSettings } ) ;
2023-10-16 05:47:02 +00:00
e . target . textContent = 'Re-enable filtering for ' + site . origin _group ;
} )
} else {
2023-11-02 08:12:35 +00:00
chrome . storage . sync . get ( { 'searchEngineSettings' : { } } , function ( response ) {
response . searchEngineSettings . get ( site . id ) . set ( 'action' , 'hide' ) ;
chrome . storage . sync . set ( { 'searchEngineSettings' : response . searchEngineSettings } ) ;
2023-10-16 05:47:02 +00:00
e . target . textContent = 'Stop filtering ' + site . origin _group + ' in future searches' ;
} )
}
}
switch ( searchEngine ) {
case 'google' :
if ( document . querySelector ( '#search' ) ) {
document . querySelector ( '#search' ) . prepend ( searchRemovalNotice ) ;
} else if ( document . querySelector ( '#topstuff' ) ) {
document . querySelector ( '#topstuff' ) . prepend ( searchRemovalNotice ) ;
} else if ( document . querySelector ( '#main' ) ) {
var el = document . querySelector ( '#main' ) ;
if ( el . querySelector ( '#main > div[data-hveid]' ) ) {
el . insertBefore ( searchRemovalNotice , el . querySelector ( 'div[data-hveid]' ) ) ;
} else {
el . insertBefore ( searchRemovalNotice , el . querySelector ( 'div div[data-hveid]' ) . parentElement ) ;
}
} ;
break ;
case 'bing' :
var li = document . createElement ( 'li' ) ;
li . appendChild ( searchRemovalNotice ) ;
document . querySelector ( '#b_results' ) . prepend ( li ) ;
break ;
case 'duckduckgo' :
if ( document . getElementById ( 'web_content_wrapper' ) ) {
var li = document . createElement ( 'li' ) ;
li . appendChild ( searchRemovalNotice ) ;
document . querySelector ( '#web_content_wrapper ol' ) . prepend ( li ) ;
} else {
document . getElementById ( 'links' ) . prepend ( searchRemovalNotice ) ;
}
break ;
case 'brave' :
document . querySelector ( 'body' ) . prepend ( searchRemovalNotice ) ;
break ;
case 'ecosia' :
document . querySelector ( 'body' ) . prepend ( searchRemovalNotice ) ;
break ;
case 'startpage' :
document . querySelector ( '#main' ) . prepend ( searchRemovalNotice ) ;
break ;
case 'yahoo' :
if ( document . querySelector ( '#web > ol' ) ) {
var li = document . createElement ( 'li' ) ;
li . appendChild ( searchRemovalNotice ) ;
document . querySelector ( '#web > ol' ) . prepend ( li ) ;
} else {
document . querySelector ( '#main-algo' ) . prepend ( searchRemovalNotice ) ;
}
break ;
default :
}
}
if ( ! Array . from ( searchResultContainer . classList ) . includes ( 'iwb-hide' ) ) {
let elementId = stringToId ( site . lang + '-' + site . origin _group ) ;
searchResultContainer . classList . add ( 'iwb-search-result-' + elementId ) ;
searchResultContainer . classList . add ( 'iwb-hide' ) ;
countFiltered ++ ;
if ( hiddenWikisRevealed [ elementId ] ) {
searchResultContainer . classList . add ( 'iwb-show' ) ;
}
}
return countFiltered ;
}
2023-08-08 06:33:23 +00:00
2023-07-25 06:17:46 +00:00
function filterSearchResults ( searchResults , searchEngine , storage ) {
getData ( ) . then ( sites => {
2023-08-04 05:50:12 +00:00
let countFiltered = 0 ;
2023-09-03 18:54:57 +00:00
for ( let searchResult of searchResults ) {
2023-07-25 06:17:46 +00:00
try {
2023-09-03 18:54:57 +00:00
let searchResultLink = '' ;
2023-07-25 06:17:46 +00:00
if ( searchEngine === 'bing' ) {
searchResultLink = searchResult . innerHTML . replaceAll ( '<strong>' , '' ) . replaceAll ( '</strong>' , '' ) ;
} else {
searchResultLink = searchResult . closest ( 'a[href]' ) . href ;
}
2023-09-01 06:04:15 +00:00
let link = String ( decodeURIComponent ( searchResultLink ) ) ;
2023-09-03 18:54:57 +00:00
2023-10-16 05:47:02 +00:00
if ( searchEngine === 'google' ) {
2023-09-03 18:54:57 +00:00
// Break if image result:
if ( link . includes ( 'imgurl=' ) ) {
break ;
2023-07-25 06:17:46 +00:00
}
2023-09-03 18:54:57 +00:00
}
// Check if site is in our list of wikis:
let matchingSites = sites . filter ( el => {
if ( link . substring ( 8 ) . includes ( '/' ) ) {
// If the URL has a path, check if an exact match with base URL or base URL + content path
// This is done to ensure we capture non-English Fandom wikis correctly
return ( link === 'https://' + el . origin _base _url ) || ( link . includes ( 'https://' + el . origin _base _url + el . origin _content _path ) ) ;
2023-07-25 06:17:46 +00:00
} else {
2023-09-03 18:54:57 +00:00
// If URL does not have a path, just check base URL
return link . includes ( 'https://' + el . origin _base _url ) ;
2023-07-25 06:17:46 +00:00
}
2023-09-03 18:54:57 +00:00
} ) ;
if ( matchingSites . length > 0 ) {
// Select match with longest base URL
let closestMatch = "" ;
matchingSites . forEach ( site => {
if ( site . origin _base _url . length > closestMatch . length ) {
closestMatch = site . origin _base _url ;
}
} ) ;
let site = matchingSites . find ( site => site . origin _base _url === closestMatch ) ;
if ( site ) {
// Get user's settings for the wiki
2023-11-02 08:12:35 +00:00
let settings = storage . searchEngineSettings || { } ;
2023-09-03 18:54:57 +00:00
let id = site [ 'id' ] ;
let searchFilterSetting = '' ;
2023-11-02 08:12:35 +00:00
if ( settings . hasOwnProperty ( id ) && settings [ id ] . action ) {
searchFilterSetting = settings [ id ] . action ;
} else if ( storage . defaultSearchAction ) {
searchFilterSetting = storage . defaultSearchAction ;
2023-09-03 18:54:57 +00:00
} else {
2023-11-02 08:12:35 +00:00
searchFilterSetting = 'replace' ;
2023-08-04 05:50:12 +00:00
}
2023-11-02 08:12:35 +00:00
if ( searchFilterSetting !== 'disabled' ) {
2023-09-03 18:54:57 +00:00
// Output stylesheet if not already done
if ( filteredWikis . length === 0 ) {
// Wait for head to be available
const headElement = document . querySelector ( 'head' ) ;
if ( headElement ) {
insertCSS ( ) ;
} else {
const docObserver = new MutationObserver ( function ( mutations , mutationInstance ) {
const headElement = document . querySelector ( 'head' ) ;
if ( headElement ) {
insertCSS ( ) ;
mutationInstance . disconnect ( ) ;
}
} ) ;
docObserver . observe ( document , {
childList : true ,
subtree : true
} ) ;
}
}
2023-08-04 05:50:12 +00:00
2023-09-03 18:54:57 +00:00
let searchResultContainer = null ;
2023-08-04 05:50:12 +00:00
switch ( searchEngine ) {
case 'google' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( 'div[data-hveid]' ) ;
2023-08-04 05:50:12 +00:00
break ;
case 'bing' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( 'li.b_algo' ) ;
2023-08-04 05:50:12 +00:00
break ;
case 'duckduckgo' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( 'li[data-layout], div.web-result' ) ;
2023-08-04 05:50:12 +00:00
break ;
case 'brave' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( 'div.snippet' ) ;
2023-08-04 05:50:12 +00:00
break ;
case 'ecosia' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( 'div.mainline__result-wrapper article div.result__body' ) ;
2023-08-04 05:50:12 +00:00
break ;
case 'startpage' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( 'div.w-gl__result' ) ;
2023-08-04 05:50:12 +00:00
break ;
2023-08-08 06:33:23 +00:00
case 'yahoo' :
2023-10-16 05:47:02 +00:00
searchResultContainer = searchResult . closest ( '#web > ol > li div.itm .exp, #web > ol > li div.algo, #web > ol > li, section.algo' ) ;
2023-08-08 06:33:23 +00:00
break ;
2023-08-04 05:50:12 +00:00
default :
}
2023-10-16 05:47:02 +00:00
if ( searchResultContainer ) {
2023-11-02 08:12:35 +00:00
if ( searchFilterSetting === 'hide' ) {
2023-10-16 05:47:02 +00:00
countFiltered += hideSearchResults ( searchResultContainer , searchEngine , site ) ;
} else {
2023-11-02 08:35:34 +00:00
countFiltered += replaceSearchResults ( searchResultContainer , site , link ) ;
2023-07-25 06:17:46 +00:00
}
2023-08-07 06:43:23 +00:00
}
2023-07-25 06:17:46 +00:00
}
}
}
2023-09-03 18:54:57 +00:00
} catch ( e ) {
console . log ( 'Indie Wiki Buddy failed to properly parse search results with error: ' + e ) ;
2023-07-25 06:17:46 +00:00
}
2023-09-03 18:54:57 +00:00
} ;
2023-08-04 05:50:12 +00:00
addLocationObserver ( main ) ;
2023-07-25 06:17:46 +00:00
if ( countFiltered > 0 ) {
chrome . storage . sync . set ( { 'countSearchFilters' : ( storage . countSearchFilters ? ? 0 ) + countFiltered } ) ;
}
} ) ;
}
function main ( mutations = null , observer = null ) {
if ( observer ) {
observer . disconnect ( ) ;
}
chrome . storage . local . get ( function ( localStorage ) {
chrome . storage . sync . get ( function ( syncStorage ) {
const storage = { ... syncStorage , ... localStorage } ;
// Check if extension is on:
if ( ( storage . power ? ? 'on' ) === 'on' ) {
// Determine which search engine we're on
2023-11-02 08:12:35 +00:00
if ( ( storage . action ? ? 'replace' ) !== 'nothing' ) {
2023-07-25 06:17:46 +00:00
if ( currentURL . hostname . includes ( 'www.google.' ) ) {
2023-07-25 08:47:22 +00:00
// Function to filter search results in Google
2023-07-25 06:17:46 +00:00
function filterGoogle ( ) {
2023-09-01 06:04:15 +00:00
let searchResults = document . querySelectorAll ( "div[data-hveid] a[href*='fandom.com']:first-of-type:not([role='button']):not([target]), div[data-hveid] a[href*='fextralife.com']:first-of-type:not([role='button']):not([target])" ) ;
2023-07-25 06:17:46 +00:00
filterSearchResults ( searchResults , 'google' , storage ) ;
}
2023-07-25 08:47:22 +00:00
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
filterGoogle ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
filterGoogle ( ) ;
}
2023-08-04 05:50:12 +00:00
} , { once : true } ) ;
2023-07-25 08:47:22 +00:00
}
2023-07-25 06:17:46 +00:00
} else if ( currentURL . hostname . includes ( 'duckduckgo.com' ) && ( currentURL . search . includes ( 'q=' ) || currentURL . pathname . includes ( 'html' ) ) ) {
2023-07-25 08:47:22 +00:00
// Function to filter search results in DuckDuckGo
2023-07-25 06:17:46 +00:00
function filterDuckDuckGo ( ) {
2023-09-18 07:04:31 +00:00
let searchResults = document . querySelectorAll ( 'h2>a[href*="fandom.com"], h2>a[href*="fextralife.com"]' ) ;
2023-07-25 06:17:46 +00:00
filterSearchResults ( searchResults , 'duckduckgo' , storage ) ;
}
2023-07-25 08:47:22 +00:00
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterDuckDuckGo ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
2023-07-25 08:47:22 +00:00
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterDuckDuckGo ( ) ;
}
2023-08-04 05:50:12 +00:00
} , { once : true } ) ;
2023-07-25 06:17:46 +00:00
}
} else if ( currentURL . hostname . includes ( 'www.bing.com' ) ) {
2023-07-25 08:47:22 +00:00
// Function to filter search results in Bing
2023-07-25 06:17:46 +00:00
function filterBing ( ) {
2023-09-18 07:04:31 +00:00
let searchResults = Array . from ( document . querySelectorAll ( '.b_attribution>cite' ) ) . filter ( el => el . innerHTML . includes ( 'fandom.com' ) || el . innerHTML . includes ( 'fextralife.com' ) ) ;
2023-07-25 06:17:46 +00:00
filterSearchResults ( searchResults , 'bing' , storage ) ;
}
2023-07-25 08:47:22 +00:00
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterBing ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
2023-07-25 08:47:22 +00:00
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterBing ( ) ;
}
2023-08-04 05:50:12 +00:00
} , { once : true } ) ;
2023-07-25 06:17:46 +00:00
}
} else if ( currentURL . hostname . includes ( 'search.brave.com' ) ) {
2023-07-25 08:47:22 +00:00
// Function to filter search results in Brave
2023-07-25 06:17:46 +00:00
function filterBrave ( ) {
2023-09-18 07:04:31 +00:00
let searchResults = Array . from ( document . querySelectorAll ( 'div.snippet[data-type="web"] a' ) ) . filter ( el => el . innerHTML . includes ( 'fandom.com' ) || el . innerHTML . includes ( 'fextralife.com' ) ) ;
2023-07-25 06:17:46 +00:00
filterSearchResults ( searchResults , 'brave' , storage ) ;
}
2023-07-25 08:47:22 +00:00
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterBrave ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
2023-07-25 08:47:22 +00:00
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterBrave ( ) ;
}
2023-08-04 05:50:12 +00:00
} , { once : true } ) ;
2023-07-25 06:17:46 +00:00
}
} else if ( currentURL . hostname . includes ( 'ecosia.org' ) ) {
2023-07-25 08:47:22 +00:00
// Function to filter search results in Ecosia
2023-07-25 06:17:46 +00:00
function filterEcosia ( ) {
2023-09-18 07:04:31 +00:00
let searchResults = Array . from ( document . querySelectorAll ( 'section.mainline .result__title a.result__link' ) ) . filter ( el => el . href . includes ( 'fandom.com' ) || el . href . includes ( 'fextralife.com' ) ) ;
2023-07-25 06:17:46 +00:00
filterSearchResults ( searchResults , 'ecosia' , storage ) ;
}
2023-07-25 08:47:22 +00:00
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterEcosia ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
2023-07-25 08:47:22 +00:00
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterEcosia ( ) ;
}
2023-08-04 05:50:12 +00:00
} , { once : true } ) ;
2023-07-25 06:17:46 +00:00
}
} else if ( currentURL . hostname . includes ( 'startpage.com' ) ) {
2023-07-25 08:47:22 +00:00
// Function to filter search results in Startpage
2023-07-25 06:17:46 +00:00
function filterStartpage ( ) {
2023-09-18 07:04:31 +00:00
let searchResults = Array . from ( document . querySelectorAll ( 'a.result-link' ) ) . filter ( el => el . href . includes ( 'fandom.com' ) || el . href . includes ( 'fextralife.com' ) ) ;
2023-07-25 06:17:46 +00:00
filterSearchResults ( searchResults , 'startpage' , storage ) ;
}
2023-07-25 08:47:22 +00:00
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterStartpage ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
2023-07-25 08:47:22 +00:00
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
2023-07-25 06:17:46 +00:00
filterStartpage ( ) ;
}
2023-08-04 05:50:12 +00:00
} , { once : true } ) ;
2023-07-25 06:17:46 +00:00
}
2023-08-08 06:33:23 +00:00
} else if ( currentURL . hostname . includes ( 'yahoo.com' ) ) {
// Function to filter search results in Yahoo
function filterYahoo ( ) {
2023-09-18 07:04:31 +00:00
let searchResults = Array . from ( document . querySelectorAll ( '#web > ol > li a:not(.thmb), #main-algo section.algo a:not(.thmb)' ) ) . filter ( el => el . href . includes ( 'fandom.com' ) || el . href . includes ( 'fextralife.com' ) ) ;
2023-08-08 06:33:23 +00:00
filterSearchResults ( searchResults , 'yahoo' , storage ) ;
}
// Wait for document to be interactive/complete:
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
filterYahoo ( ) ;
} else {
document . addEventListener ( 'readystatechange' , e => {
if ( [ 'interactive' , 'complete' ] . includes ( document . readyState ) ) {
filterYahoo ( ) ;
}
} , { once : true } ) ;
}
2023-07-25 06:17:46 +00:00
}
}
}
} ) ;
} ) ;
}
main ( ) ;