BetterDiscordAddons/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js

276 lines
12 KiB
JavaScript
Raw Normal View History

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
})();