BetterDiscordAddons/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js

332 lines
13 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name GoogleSearchReplace
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2021-03-05 13:26:41 +01:00
* @version 1.2.9
* @description Replaces the default Google Text Search with a custom Search Engine
2020-10-20 23:25:34 +02:00
* @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
* @updateUrl https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js
2020-10-20 23:25:34 +02:00
*/
2018-10-11 10:21:26 +02:00
2020-09-19 20:49:33 +02:00
module.exports = (_ => {
2020-10-09 21:09:35 +02:00
const config = {
2020-09-19 20:49:33 +02:00
"info": {
"name": "GoogleSearchReplace",
"author": "DevilBro",
2020-12-28 19:41:31 +01:00
"version": "1.2.9",
2021-03-04 12:15:46 +01:00
"description": "Replaces the default Google Text Search with a custom Search Engine"
2020-12-28 19:41:31 +01:00
},
"changeLog": {
"added": {
"GitHub": "Added github as search engine"
}
2020-02-13 12:25:48 +01:00
}
2020-09-19 20:49:33 +02:00
};
2020-12-28 19:41:31 +01:00
2020-10-09 21:09:35 +02:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2021-01-06 12:38:36 +01:00
getName () {return config.info.name;}
getAuthor () {return config.info.author;}
getVersion () {return config.info.version;}
2021-02-01 17:13:13 +01:00
getDescription () {return `The Library Plugin needed for ${config.info.name} is missing. Open the Plugin Settings to download it. \n\n${config.info.description}`;}
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2021-03-05 13:14:18 +01:00
if (!e && b && r.statusCode == 200) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-02-01 17:13:13 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin, try again later or download it manually from GitHub: https://github.com/mwittrien/BetterDiscordAddons/tree/master/Library/");
});
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load () {
2020-11-19 16:51:14 +01:00
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
2021-01-14 16:14:44 +01:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${config.info.name} is missing. Please click "Download Now" to install it.`, {
2020-09-19 20:49:33 +02:00
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
2020-09-20 08:15:13 +02:00
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
2021-02-01 17:13:13 +01:00
this.downloadLibrary();
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
});
}
if (!window.BDFDB_Global.pluginQueue.includes(config.info.name)) window.BDFDB_Global.pluginQueue.push(config.info.name);
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start () {this.load();}
stop () {}
getSettingsPanel () {
2020-11-28 23:12:09 +01:00
let template = document.createElement("template");
2021-01-14 16:14:44 +01:00
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${config.info.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
2021-02-01 17:13:13 +01:00
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
2020-11-28 23:12:09 +01:00
return template.content.firstElementChild;
}
2020-10-09 21:09:35 +02:00
} : (([Plugin, BDFDB]) => {
const textUrlReplaceString = "DEVILBRO_BD_GOOGLESEARCHREPLACE_REPLACEURL";
2021-01-10 11:41:01 +01:00
var engines = {}, enabledEngines = {};
2020-09-19 20:49:33 +02:00
2020-10-09 21:09:35 +02:00
return class GoogleSearchReplace extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2020-09-19 20:49:33 +02:00
this.defaults = {
engines: {
2020-11-19 16:51:14 +01:00
_all: {value: true, name: BDFDB.LanguageUtils.LanguageStrings.FORM_LABEL_ALL, url: null},
2020-12-28 19:41:31 +01:00
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},
2020-11-19 16:51:14 +01:00
DuckDuckGo: {value: true, name: "DuckDuckGo", url: "https://duckduckgo.com/?q=" + textUrlReplaceString},
2020-12-28 19:41:31 +01:00
GitHub: {value: true, name: "GitHub", url: "https://github.com/search?q=" + textUrlReplaceString},
2020-11-19 16:51:14 +01:00
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},
2020-12-28 19:41:31 +01:00
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.info/?q=" + textUrlReplaceString},
WolframAlpha: {value: true, name: "Wolfram Alpha", url: "https://www.wolframalpha.com/input/?i=" + textUrlReplaceString},
2020-11-19 16:51:14 +01:00
Yandex: {value: true, name: "Yandex", url: "https://yandex.com/search/?text=" + textUrlReplaceString},
2020-12-28 19:41:31 +01:00
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}
2020-09-19 20:49:33 +02:00
}
};
}
2021-01-06 12:38:36 +01:00
onStart () {
2020-09-19 20:49:33 +02:00
this.forceUpdateAll();
}
2020-02-13 12:25:48 +01:00
2021-01-06 12:38:36 +01:00
onStop () {}
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
let settingsPanel, settingsItems = [];
2020-12-15 10:35:30 +01:00
settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, {
title: "Search Engines:",
2020-09-19 20:49:33 +02:00
children: Object.keys(engines).filter(n => n && n != "_all").map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["engines", key],
label: this.defaults.engines[key].name,
value: engines[key]
}))
}));
2020-06-09 09:03:02 +02:00
2020-09-19 20:49:33 +02:00
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-13 12:25:48 +01:00
}
2019-10-22 11:37:23 +02:00
2021-01-06 12:38:36 +01:00
onSettingsClosed () {
2020-09-19 20:49:33 +02:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
this.forceUpdateAll();
}
}
2021-01-06 12:38:36 +01:00
forceUpdateAll () {
2020-09-19 20:49:33 +02:00
engines = BDFDB.DataUtils.get(this, "engines");
enabledEngines = BDFDB.ObjectUtils.filter(engines, n => n);
2020-02-13 12:25:48 +01:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
onMessageContextMenu (e) {
this.injectItem(e);
}
2020-05-19 19:56:29 +02:00
2020-09-19 20:49:33 +02:00
onNativeContextMenu (e) {
this.injectItem(e);
}
injectItem (e) {
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: "search-google"});
if (index > -1) {
let text = document.getSelection().toString();
let enginesWithoutAll = BDFDB.ObjectUtils.filter(enabledEngines, n => n != "_all", true);
let engineKeys = Object.keys(enginesWithoutAll);
if (engineKeys.length == 1) {
children.splice(index, 1, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: this.labels.context_googlesearchreplace.replace("...", this.defaults.engines[engineKeys[0]].name),
2020-09-19 20:49:33 +02:00
id: children[index].props.id,
persisting: true,
action: event => {
if (!event.shiftKey) BDFDB.ContextMenuUtils.close(e.instance);
2021-01-10 11:41:01 +01:00
BDFDB.DiscordUtils.openLink(this.defaults.engines[engineKeys[0]].url.replace(textUrlReplaceString, encodeURIComponent(text)), {
minimized: event.shiftKey
});
2020-02-13 12:25:48 +01:00
}
2020-09-19 20:49:33 +02:00
}));
}
else {
let items = [];
for (let key in enabledEngines) items.push(BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: this.defaults.engines[key].name,
id: BDFDB.ContextMenuUtils.createItemId(this.name, "search", key),
color: key == "_all" ? BDFDB.LibraryComponents.MenuItems.Colors.DANGER : BDFDB.LibraryComponents.MenuItems.Colors.DEFAULT,
persisting: true,
action: event => {
if (!event.shiftKey) BDFDB.ContextMenuUtils.close(e.instance);
if (key == "_all") {
2021-01-10 11:41:01 +01:00
for (let key2 in enginesWithoutAll) BDFDB.DiscordUtils.openLink(this.defaults.engines[key2].url.replace(textUrlReplaceString, encodeURIComponent(text)), {
minimized: event.shiftKey
});
2020-09-19 20:49:33 +02:00
}
2021-01-10 11:41:01 +01:00
else BDFDB.DiscordUtils.openLink(this.defaults.engines[key].url.replace(textUrlReplaceString, encodeURIComponent(text)), {
minimized: event.shiftKey
});
2020-09-19 20:49:33 +02:00
}
}));
if (!items.length) items.push(BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: this.labels.submenu_disabled,
2020-09-19 20:49:33 +02:00
id: BDFDB.ContextMenuUtils.createItemId(this.name, "disabled"),
disabled: true
}));
children.splice(index, 1, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
label: this.labels.context_googlesearchreplace,
2020-09-19 20:49:33 +02:00
id: children[index].props.id,
children: items
}));
}
2019-01-17 23:48:29 +01:00
}
2019-09-11 12:14:43 +02:00
}
2019-01-26 22:45:19 +01:00
2021-01-06 12:38:36 +01:00
setLabelsByLanguage () {
2020-09-19 20:49:33 +02:00
switch (BDFDB.LanguageUtils.getLanguage().id) {
case "bg": // Bulgarian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Търсене с ...",
submenu_disabled: "Всички инвалиди"
2020-09-19 20:49:33 +02:00
};
case "da": // Danish
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Søg med ...",
submenu_disabled: "Alle handicappede"
2020-09-19 20:49:33 +02:00
};
case "de": // German
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Suche mit ...",
submenu_disabled: "Alle deaktiviert"
2020-09-19 20:49:33 +02:00
};
case "el": // Greek
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Αναζήτηση με ...",
submenu_disabled: "Όλα τα άτομα με ειδικές ανάγκες"
2020-09-19 20:49:33 +02:00
};
case "es": // Spanish
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Buscar con ...",
submenu_disabled: "Todos discapacitados"
2020-09-19 20:49:33 +02:00
};
case "fi": // Finnish
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Hae ...",
submenu_disabled: "Kaikki vammaiset"
2020-09-19 20:49:33 +02:00
};
case "fr": // French
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Rechercher avec ...",
submenu_disabled: "Tout désactivé"
2020-09-19 20:49:33 +02:00
};
case "hr": // Croatian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Traži sa ...",
submenu_disabled: "Svi invalidi"
2020-09-19 20:49:33 +02:00
};
case "hu": // Hungarian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Keresés a következővel:",
submenu_disabled: "Minden fogyatékkal él"
2020-09-19 20:49:33 +02:00
};
case "it": // Italian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Cerca con ...",
submenu_disabled: "Tutti disabilitati"
2020-09-19 20:49:33 +02:00
};
case "ja": // Japanese
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "で検索 ...",
submenu_disabled: "すべて無効"
2020-09-19 20:49:33 +02:00
};
case "ko": // Korean
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "다음으로 검색 ...",
submenu_disabled: "모두 비활성화 됨"
2020-09-19 20:49:33 +02:00
};
case "lt": // Lithuanian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Ieškoti naudojant ...",
submenu_disabled: "Visi neįgalūs"
2020-09-19 20:49:33 +02:00
};
case "nl": // Dutch
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Zoeken met ...",
submenu_disabled: "Allemaal uitgeschakeld"
2020-09-19 20:49:33 +02:00
};
case "no": // Norwegian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Søk med ...",
submenu_disabled: "Alle funksjonshemmede"
2020-09-19 20:49:33 +02:00
};
case "pl": // Polish
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Szukaj za pomocą ...",
submenu_disabled: "Wszystkie wyłączone"
2020-09-19 20:49:33 +02:00
};
case "pt-BR": // Portuguese (Brazil)
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Pesquise com ...",
submenu_disabled: "Todos desativados"
2020-09-19 20:49:33 +02:00
};
case "ro": // Romanian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Căutați cu ...",
submenu_disabled: "Toate sunt dezactivate"
2020-09-19 20:49:33 +02:00
};
case "ru": // Russian
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Искать с ...",
submenu_disabled: "Все отключены"
2020-09-19 20:49:33 +02:00
};
case "sv": // Swedish
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "Sök med ...",
submenu_disabled: "Alla funktionshindrade"
2020-09-19 20:49:33 +02:00
};
case "th": // Thai
2020-09-19 20:49:33 +02:00
return {
context_googlesearchreplace: "ค้นหาด้วย ...",
submenu_disabled: "ปิดใช้งานทั้งหมด"
};
case "tr": // Turkish
return {
context_googlesearchreplace: "Şununla ara ...",
submenu_disabled: "Hepsi devre dışı"
};
case "uk": // Ukrainian
return {
context_googlesearchreplace: "Шукати за допомогою ...",
submenu_disabled: "Всі інваліди"
};
case "vi": // Vietnamese
return {
context_googlesearchreplace: "Tìm kiếm với ...",
submenu_disabled: "Tất cả đã bị vô hiệu hóa"
};
2021-01-15 17:54:22 +01:00
case "zh-CN": // Chinese (China)
return {
context_googlesearchreplace: "用 ... 搜索",
submenu_disabled: "全部禁用"
};
2021-01-15 17:54:22 +01:00
case "zh-TW": // Chinese (Taiwan)
return {
context_googlesearchreplace: "用 ... 搜索",
submenu_disabled: "全部禁用"
};
default: // English
return {
context_googlesearchreplace: "Search with ...",
submenu_disabled: "All disabled"
2020-09-19 20:49:33 +02:00
};
}
2020-02-13 12:25:48 +01:00
}
2020-09-19 20:49:33 +02:00
};
2020-10-09 21:09:35 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(config));
2020-11-13 07:06:19 +01:00
})();