2019-09-20 22:32:52 +02:00
//META{"name":"GoogleSearchReplace","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
class GoogleSearchReplace {
2019-01-17 23:48:29 +01:00
getName ( ) { return "GoogleSearchReplace" ; }
2019-11-30 11:00:58 +01:00
getVersion ( ) { return "1.2.3" ; }
2019-01-26 22:45:19 +01:00
2019-01-17 23:48:29 +01:00
getAuthor ( ) { return "DevilBro" ; }
getDescription ( ) { return "Replaces the default Google Text Search with a selection menu of several search engines." ; }
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor ( ) {
2019-09-11 12:14:43 +02:00
this . changelog = {
2019-11-29 19:11:56 +01:00
"improved" : [ [ "Inbuilt Window" , "Option to use an inbuilt browser instead of the default OS browser" ] , [ "New Library Structure & React" , "Restructured my Library and switched to React rendering instead of DOM manipulation" ] ]
2019-09-11 12:14:43 +02:00
} ;
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor ( ) {
2018-10-11 10:21:26 +02:00
this . textUrlReplaceString = "DEVILBRO_BD_GOOGLESEARCHREPLACE_REPLACE_TEXTURL" ;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this . defaults = {
2019-11-29 19:11:56 +01:00
settings : {
useChromium : { value : false , description : "Use an inbuilt browser window instead of opening your default browser" } ,
} ,
2018-10-11 10:21:26 +02:00
engines : {
2019-10-19 11:41:39 +02:00
_all : { value : true , name : BDFDB . LanguageUtils . LanguageStrings . FORM _LABEL _ALL , url : null } ,
2019-01-22 22:48:50 +01:00
Ask : { value : true , name : "Ask" , url : "https://ask.com/web?q=" + this . textUrlReplaceString } ,
Bing : { value : true , name : "Bing" , url : "https://www.bing.com/search?q=" + this . textUrlReplaceString } ,
DogPile : { value : true , name : "DogPile" , url : "http://www.dogpile.com/search/web?q=" + this . textUrlReplaceString } ,
DuckDuckGo : { value : true , name : "DuckDuckGo" , url : "https://duckduckgo.com/?q=" + this . textUrlReplaceString } ,
Google : { value : true , name : "Google" , url : "https://www.google.com/search?q=" + this . textUrlReplaceString } ,
GoogleScholar : { value : true , name : "Google Scholar" , url : "https://scholar.google.com/scholar?q=" + this . textUrlReplaceString } ,
Quora : { value : true , name : "Quora" , url : "https://www.quora.com/search?q=" + this . textUrlReplaceString } ,
Qwant : { value : true , name : "Qwant" , url : "https://www.qwant.com/?t=all&q=" + this . textUrlReplaceString } ,
UrbanDictionary : { value : true , name : "Urban Dictionary" , url : "https://www.urbandictionary.com/define.php?term=" + this . textUrlReplaceString } ,
Searx : { value : true , name : "Searx" , url : "https://searx.me/?q=" + this . textUrlReplaceString } ,
WolframAlpha : { value : true , name : "Wolfram Alpha" , url : "https://www.wolframalpha.com/input/?i=" + this . textUrlReplaceString } ,
Yandex : { value : true , name : "Yandex" , url : "https://yandex.com/search/?text=" + this . textUrlReplaceString } ,
Yahoo : { value : true , name : "Yahoo" , url : "https://search.yahoo.com/search?p=" + this . textUrlReplaceString } ,
YouTube : { value : true , name : "YouTube" , url : "https://www.youtube.com/results?q=" + this . textUrlReplaceString }
2018-10-11 10:21:26 +02:00
}
} ;
}
getSettingsPanel ( ) {
2019-01-22 11:28:32 +01:00
if ( ! global . BDFDB || typeof BDFDB != "object" || ! BDFDB . loaded || ! this . started ) return ;
2019-11-29 19:11:56 +01:00
let settings = BDFDB . DataUtils . get ( this , "settings" ) ;
2019-10-22 19:49:57 +02:00
let engines = BDFDB . DataUtils . get ( this , "engines" ) ;
2019-12-18 16:45:08 +01:00
let settingspanel , settingsitems = [ ] , engineitems = [ ] ;
2019-11-07 10:15:10 +01:00
2019-11-29 19:11:56 +01:00
for ( let key in settings ) settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
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 , {
2019-11-07 10:15:10 +01:00
className : BDFDB . disCN . marginbottom8 ,
type : "Switch" ,
plugin : this ,
keys : [ "engines" , key ] ,
label : this . defaults . engines [ key ] . name ,
value : engines [ key ]
} ) ) ;
settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsPanelInner , {
title : "Search Engines:" ,
first : settingsitems . length == 0 ,
last : true ,
2019-11-29 19:11:56 +01:00
children : engineitems
2019-11-07 10:15:10 +01:00
} ) ) ;
2019-12-18 16:45:08 +01:00
return settingspanel = BDFDB . PluginUtils . createSettingsPanel ( this , settingsitems ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
//legacy
load ( ) { }
start ( ) {
2019-02-04 09:13:15 +01:00
if ( ! global . BDFDB ) global . BDFDB = { myPlugins : { } } ;
if ( global . BDFDB && global . BDFDB . myPlugins && typeof global . BDFDB . myPlugins == "object" ) global . BDFDB . myPlugins [ this . getName ( ) ] = this ;
2019-05-26 13:55:26 +02:00
var libraryScript = document . querySelector ( 'head script#BDFDBLibraryScript' ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
2018-10-11 10:21:26 +02:00
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
2019-05-26 13:55:26 +02:00
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
2018-10-11 10:21:26 +02:00
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
2019-10-18 10:56:41 +02:00
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
2019-01-17 23:48:29 +01:00
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
2020-01-14 00:06:07 +01:00
libraryScript . addEventListener ( "load" , _ => { this . initialize ( ) ; } ) ;
2018-10-11 10:21:26 +02:00
document . head . appendChild ( libraryScript ) ;
2019-05-26 13:55:26 +02:00
}
else if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
2020-01-14 00:06:07 +01:00
this . startTimeout = setTimeout ( _ => {
2019-11-01 10:27:07 +01:00
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 ) ;
2018-10-11 10:21:26 +02:00
}
initialize ( ) {
2019-01-17 23:48:29 +01:00
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
2019-01-22 11:05:54 +01:00
if ( this . started ) return ;
2019-10-22 18:55:25 +02:00
BDFDB . PluginUtils . init ( this ) ;
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
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
}
stop ( ) {
2019-01-17 23:48:29 +01:00
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
2019-10-22 11:37:23 +02:00
this . stopping = true ;
2019-10-22 18:55:25 +02:00
BDFDB . PluginUtils . clear ( this ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2019-11-07 10:15:10 +01:00
onNativeContextMenu ( e ) {
2019-11-29 19:11:56 +01:00
if ( e . instance . props . type == BDFDB . DiscordConstants . ContextMenuTypes . NATIVE _TEXT && e . instance . props . value ) this . injectItem ( e , e . instance . props . value ) ;
2019-01-03 11:30:54 +01:00
}
2019-01-26 22:45:19 +01:00
2019-11-07 10:15:10 +01:00
onMessageContextMenu ( e ) {
2019-11-11 10:48:30 +01:00
if ( e . instance . props . message && e . instance . props . channel && e . instance . props . target ) {
2019-01-03 10:45:42 +01:00
let text = document . getSelection ( ) . toString ( ) ;
2019-11-07 10:15:10 +01:00
if ( text ) this . injectItem ( e , text ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2019-11-07 10:15:10 +01:00
injectItem ( e , text ) {
2019-10-22 19:49:57 +02:00
let engines = BDFDB . DataUtils . get ( this , "engines" ) ;
2019-09-11 12:14:43 +02:00
let items = [ ] ;
2019-12-05 09:10:14 +01:00
for ( let key in engines ) if ( engines [ key ] ) items . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Item , {
2019-09-11 12:14:43 +02:00
label : this . defaults . engines [ key ] . name ,
2019-11-07 10:15:10 +01:00
danger : key == "_all" ,
2019-11-11 20:33:29 +01:00
action : event => {
2019-11-29 19:11:56 +01:00
let useChromium = BDFDB . DataUtils . get ( this , "settings" , "useChromium" ) ;
2019-11-11 20:33:29 +01:00
if ( ! event . shiftKey ) BDFDB . ContextMenuUtils . close ( e . instance ) ;
2019-09-11 12:14:43 +02:00
if ( key == "_all" ) {
2019-11-30 11:29:12 +01:00
for ( let key2 in engines ) if ( key2 != "_all" && engines [ key2 ] ) BDFDB . DiscordUtils . openLink ( this . defaults . engines [ key2 ] . url . replace ( this . textUrlReplaceString , encodeURIComponent ( text ) ) , useChromium , event . shiftKey ) ;
2019-01-17 23:48:29 +01:00
}
2019-11-30 11:29:12 +01:00
else BDFDB . DiscordUtils . openLink ( this . defaults . engines [ key ] . url . replace ( this . textUrlReplaceString , encodeURIComponent ( text ) ) , useChromium , event . shiftKey ) ;
2019-09-11 12:14:43 +02:00
}
} ) ) ;
2019-12-05 09:10:14 +01:00
if ( ! items . length ) items . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Item , {
2019-09-11 12:14:43 +02:00
label : this . labels . submenu _disabled _text ,
disabled : true
} ) ) ;
2019-11-07 10:15:10 +01:00
let [ children , index ] = BDFDB . ReactUtils . findChildren ( e . returnvalue , { name : "SearchWithGoogle" } ) ;
2019-12-05 09:10:14 +01:00
const item = BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItems . Sub , {
2019-09-11 12:14:43 +02:00
label : this . labels . context _googlesearchreplace _text ,
render : items
2019-01-17 23:48:29 +01:00
} ) ;
2019-09-11 12:14:43 +02:00
if ( index > - 1 ) children . splice ( index , 1 , item ) ;
else children . push ( item ) ;
2019-01-03 11:30:54 +01:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
setLabelsByLanguage ( ) {
2019-10-24 11:47:57 +02:00
switch ( BDFDB . LanguageUtils . getLanguage ( ) . id ) {
2018-10-11 10:21:26 +02:00
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"
} ;
}
}
}