2020-02-27 08:44:03 +01:00
//META{"name":"GoogleSearchReplace","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/GoogleSearchReplace","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js"}*//
2018-10-11 10:21:26 +02:00
2020-02-13 12:25:48 +01:00
var GoogleSearchReplace = ( _ => {
const textUrlReplaceString = "DEVILBRO_BD_GOOGLESEARCHREPLACE_REPLACE_TEXTURL" ;
return class GoogleSearchReplace {
getName ( ) { return "GoogleSearchReplace" ; }
2019-01-17 23:48:29 +01:00
2020-02-13 12:25:48 +01:00
getVersion ( ) { return "1.2.4" ; }
2019-01-26 22:45:19 +01:00
2020-02-13 12:25:48 +01:00
getAuthor ( ) { return "DevilBro" ; }
2019-01-17 23:48:29 +01:00
2020-02-13 12:25:48 +01:00
getDescription ( ) { return "Replaces the default Google Text Search with a selection menu of several search engines." ; }
2019-01-26 22:45:19 +01:00
2020-02-13 12:25:48 +01:00
constructor ( ) {
this . changelog = {
2020-02-13 12:35:32 +01:00
"improved" : [ [ "One Engine" , "Enabling only one search engine doesn't create a SubMenu anymore" ] , [ "New Library Structure & React" , "Restructured my Library and switched to React rendering instead of DOM manipulation" ] ]
2020-02-13 12:25:48 +01:00
} ;
this . patchedModules = {
after : {
SearchWithGoogle : "render"
}
} ;
}
2019-01-26 22:45:19 +01:00
2020-02-13 12:25:48 +01:00
initConstructor ( ) {
this . defaults = {
settings : {
useChromium : { value : false , description : "Use an inbuilt browser window instead of opening your default browser" } ,
} ,
engines : {
_all : { value : true , name : BDFDB . LanguageUtils . LanguageStrings . FORM _LABEL _ALL , url : null } ,
Ask : { value : true , name : "Ask" , url : "https://ask.com/web?q=" + textUrlReplaceString } ,
Bing : { value : true , name : "Bing" , url : "https://www.bing.com/search?q=" + textUrlReplaceString } ,
DogPile : { value : true , name : "DogPile" , url : "http://www.dogpile.com/search/web?q=" + textUrlReplaceString } ,
DuckDuckGo : { value : true , name : "DuckDuckGo" , url : "https://duckduckgo.com/?q=" + textUrlReplaceString } ,
Google : { value : true , name : "Google" , url : "https://www.google.com/search?q=" + textUrlReplaceString } ,
GoogleScholar : { value : true , name : "Google Scholar" , url : "https://scholar.google.com/scholar?q=" + textUrlReplaceString } ,
Quora : { value : true , name : "Quora" , url : "https://www.quora.com/search?q=" + textUrlReplaceString } ,
Qwant : { value : true , name : "Qwant" , url : "https://www.qwant.com/?t=all&q=" + textUrlReplaceString } ,
UrbanDictionary : { value : true , name : "Urban Dictionary" , url : "https://www.urbandictionary.com/define.php?term=" + textUrlReplaceString } ,
Searx : { value : true , name : "Searx" , url : "https://searx.me/?q=" + textUrlReplaceString } ,
WolframAlpha : { value : true , name : "Wolfram Alpha" , url : "https://www.wolframalpha.com/input/?i=" + textUrlReplaceString } ,
Yandex : { value : true , name : "Yandex" , url : "https://yandex.com/search/?text=" + textUrlReplaceString } ,
Yahoo : { value : true , name : "Yahoo" , url : "https://search.yahoo.com/search?p=" + textUrlReplaceString } ,
YouTube : { value : true , name : "YouTube" , url : "https://www.youtube.com/results?q=" + textUrlReplaceString }
}
} ;
}
2018-10-11 10:21:26 +02:00
2020-02-13 12:25:48 +01:00
getSettingsPanel ( ) {
if ( ! window . BDFDB || typeof BDFDB != "object" || ! BDFDB . loaded || ! this . started ) return ;
let settings = BDFDB . DataUtils . get ( this , "settings" ) ;
let engines = BDFDB . DataUtils . get ( this , "engines" ) ;
2020-03-28 07:55:39 +01:00
let settingsPanel , settingsItems = [ ] , engineitems = [ ] ;
2020-02-13 12:25:48 +01:00
2020-03-28 07:55:39 +01:00
for ( let key in settings ) settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
2020-02-13 12:25:48 +01:00
className : BDFDB . disCN . marginbottom8 ,
type : "Switch" ,
plugin : this ,
keys : [ "settings" , key ] ,
label : this . defaults . settings [ key ] . description ,
value : settings [ key ]
} ) ) ;
for ( let key in engines ) engineitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
className : BDFDB . disCN . marginbottom8 ,
type : "Switch" ,
plugin : this ,
keys : [ "engines" , key ] ,
label : this . defaults . engines [ key ] . name ,
value : engines [ key ]
} ) ) ;
2020-03-28 07:55:39 +01:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsPanelInner , {
2020-02-13 12:25:48 +01:00
title : "Search Engines:" ,
2020-03-28 07:55:39 +01:00
first : settingsItems . length == 0 ,
2020-02-13 12:25:48 +01:00
last : true ,
children : engineitems
} ) ) ;
2020-03-28 07:55:39 +01:00
return settingsPanel = BDFDB . PluginUtils . createSettingsPanel ( this , settingsItems ) ;
2020-02-13 12:25:48 +01:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-02-13 12:25:48 +01:00
load ( ) { }
2018-10-11 10:21:26 +02:00
2020-02-13 12:25:48 +01:00
start ( ) {
if ( ! window . BDFDB ) window . BDFDB = { myPlugins : { } } ;
if ( window . BDFDB && window . BDFDB . myPlugins && typeof window . BDFDB . myPlugins == "object" ) window . BDFDB . myPlugins [ this . getName ( ) ] = this ;
let libraryScript = document . querySelector ( "head script#BDFDBLibraryScript" ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
libraryScript . addEventListener ( "load" , _ => { this . initialize ( ) ; } ) ;
document . head . appendChild ( libraryScript ) ;
}
else if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
this . startTimeout = setTimeout ( _ => {
try { return this . initialize ( ) ; }
catch ( err ) { console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not initiate plugin! " + err ) ; }
} , 30000 ) ;
2019-05-26 13:55:26 +02:00
}
2018-10-11 10:21:26 +02:00
2020-02-13 12:25:48 +01:00
initialize ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
if ( this . started ) return ;
BDFDB . PluginUtils . init ( this ) ;
}
else console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not load BD functions!" ) ;
2018-10-11 10:21:26 +02:00
}
2020-02-13 12:25:48 +01:00
stop ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
this . stopping = true ;
2019-10-22 11:37:23 +02:00
2020-02-13 12:25:48 +01:00
BDFDB . PluginUtils . clear ( this ) ;
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-01-26 22:45:19 +01:00
2020-02-13 12:25:48 +01:00
processSearchWithGoogle ( e ) {
if ( e . instance . props . value ) {
let enabledEngines = BDFDB . ObjectUtils . filter ( BDFDB . DataUtils . get ( this , "engines" ) , n => n ) ;
let enginesWithoutAll = BDFDB . ObjectUtils . filter ( enabledEngines , n => n != "_all" , true ) ;
let engineKeys = Object . keys ( enginesWithoutAll ) ;
if ( engineKeys . length == 1 ) return BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Item , {
label : this . labels . context _googlesearchreplace _text . replace ( "..." , this . defaults . engines [ engineKeys [ 0 ] ] . name ) ,
action : event => {
let useChromium = BDFDB . DataUtils . get ( this , "settings" , "useChromium" ) ;
if ( ! event . shiftKey ) BDFDB . ContextMenuUtils . close ( e . instance ) ;
BDFDB . DiscordUtils . openLink ( this . defaults . engines [ engineKeys [ 0 ] ] . url . replace ( textUrlReplaceString , encodeURIComponent ( e . instance . props . value ) ) , useChromium , event . shiftKey ) ;
}
} ) ;
else {
let items = [ ] ;
for ( let key in enabledEngines ) items . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Item , {
label : this . defaults . engines [ key ] . name ,
danger : key == "_all" ,
action : event => {
let useChromium = BDFDB . DataUtils . get ( this , "settings" , "useChromium" ) ;
if ( ! event . shiftKey ) BDFDB . ContextMenuUtils . close ( e . instance ) ;
if ( key == "_all" ) {
for ( let key2 in enginesWithoutAll ) BDFDB . DiscordUtils . openLink ( this . defaults . engines [ key2 ] . url . replace ( textUrlReplaceString , encodeURIComponent ( e . instance . props . value ) ) , useChromium , event . shiftKey ) ;
}
else BDFDB . DiscordUtils . openLink ( this . defaults . engines [ key ] . url . replace ( textUrlReplaceString , encodeURIComponent ( e . instance . props . value ) ) , useChromium , event . shiftKey ) ;
}
} ) ) ;
if ( ! items . length ) items . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Item , {
label : this . labels . submenu _disabled _text ,
disabled : true
} ) ) ;
return BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Sub , {
label : this . labels . context _googlesearchreplace _text ,
render : items
} ) ;
2019-01-17 23:48:29 +01:00
}
2019-09-11 12:14:43 +02:00
}
2020-02-13 12:25:48 +01:00
}
2019-01-26 22:45:19 +01:00
2020-02-13 12:25:48 +01:00
setLabelsByLanguage ( ) {
switch ( BDFDB . LanguageUtils . getLanguage ( ) . id ) {
case "hr" : //croatian
return {
context _googlesearchreplace _text : "Pretražujte s ..." ,
submenu _disabled _text : "Svi su onemogućeni"
} ;
case "da" : //danish
return {
context _googlesearchreplace _text : "Søg med ..." ,
submenu _disabled _text : "Alle deaktiveret"
} ;
case "de" : //german
return {
context _googlesearchreplace _text : "Suche mit ..." ,
submenu _disabled _text : "Alle deaktiviert"
} ;
case "es" : //spanish
return {
context _googlesearchreplace _text : "Buscar con ..." ,
submenu _disabled _text : "Todo desactivado"
} ;
case "fr" : //french
return {
context _googlesearchreplace _text : "Rechercher avec ..." ,
submenu _disabled _text : "Tous désactivés"
} ;
case "it" : //italian
return {
context _googlesearchreplace _text : "Cerca con ..." ,
submenu _disabled _text : "Tutto disattivato"
} ;
case "nl" : //dutch
return {
context _googlesearchreplace _text : "Zoeken met ..." ,
submenu _disabled _text : "Alles gedeactiveerd"
} ;
case "no" : //norwegian
return {
context _googlesearchreplace _text : "Søk med ..." ,
submenu _disabled _text : "Alle deaktivert"
} ;
case "pl" : //polish
return {
context _googlesearchreplace _text : "Szukaj za pomocą ..." ,
submenu _disabled _text : "Wszystkie wyłączone"
} ;
case "pt-BR" : //portuguese (brazil)
return {
context _googlesearchreplace _text : "Pesquisar com ..." ,
submenu _disabled _text : "Todos desativados"
} ;
case "fi" : //finnish
return {
context _googlesearchreplace _text : "Etsi ..." ,
submenu _disabled _text : "Kaikki on poistettu käytöstä"
} ;
case "sv" : //swedish
return {
context _googlesearchreplace _text : "Sök med ..." ,
submenu _disabled _text : "Alla avaktiverade"
} ;
case "tr" : //turkish
return {
context _googlesearchreplace _text : "Ile ara ..." ,
submenu _disabled _text : "Hepsi deaktive"
} ;
case "cs" : //czech
return {
context _googlesearchreplace _text : "Hledat s ..." ,
submenu _disabled _text : "Všechny deaktivované"
} ;
case "bg" : //bulgarian
return {
context _googlesearchreplace _text : "Търсене с ..." ,
submenu _disabled _text : "Всички с а деактивирани"
} ;
case "ru" : //russian
return {
context _googlesearchreplace _text : "Поиск с ..." ,
submenu _disabled _text : "В с е деактивированные"
} ;
case "uk" : //ukrainian
return {
context _googlesearchreplace _text : "Пошук з ..." ,
submenu _disabled _text : "В с і вимкнені"
} ;
case "ja" : //japanese
return {
context _googlesearchreplace _text : "で検索する ..." ,
submenu _disabled _text : "すべて非アクティブ化"
} ;
case "zh-TW" : //chinese (traditional)
return {
context _googlesearchreplace _text : "搜索 ..." ,
submenu _disabled _text : "全部停用"
} ;
case "ko" : //korean
return {
context _googlesearchreplace _text : "다음으로 검색 ..." ,
submenu _disabled _text : "모두 비활성화 됨"
} ;
default : //default: english
return {
context _googlesearchreplace _text : "Search with ..." ,
submenu _disabled _text : "All disabled"
} ;
}
2018-10-11 10:21:26 +02:00
}
}
2020-02-13 12:25:48 +01:00
} ) ( ) ;