ReplaceGoogleSearch: add option to replace engine with single option (#3737)

Co-authored-by: Vendicated <vendicated@riseup.net>
This commit is contained in:
Andrew 2025-10-28 18:46:07 +01:00 • committed by GitHub
parent 556a27704a
commit 0f258731db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,6 +22,11 @@ const DefaultEngines = {
Wikipedia: "https://wikipedia.org/w/index.php?search=",
} as const;
const enum ReplacementEngineValue {
OFF = "off",
CUSTOM = "custom",
}
const settings = definePluginSettings({
customEngineName: {
description: "Name of the custom search engine",
@ -32,6 +37,15 @@ const settings = definePluginSettings({
description: "The URL of your Engine",
type: OptionType.STRING,
placeholder: "https://google.com/search?q="
},
replacementEngine: {
description: "Replace with a specific search engine instead of adding a menu",
type: OptionType.SELECT,
options: [
{ label: "Off", value: ReplacementEngineValue.OFF, default: true },
{ label: "Custom Engine", value: ReplacementEngineValue.CUSTOM },
...Object.keys(DefaultEngines).map(engine => ({ label: engine, value: engine }))
]
}
});
@ -40,13 +54,31 @@ function search(src: string, engine: string) {
}
function makeSearchItem(src: string) {
let Engines = {};
const { customEngineName, customEngineURL, replacementEngine } = settings.store;
if (settings.store.customEngineName && settings.store.customEngineURL) {
Engines[settings.store.customEngineName] = settings.store.customEngineURL;
const hasCustomEngine = Boolean(customEngineName && customEngineURL);
const hasValidReplacementEngine = replacementEngine !== ReplacementEngineValue.OFF && !(replacementEngine === ReplacementEngineValue.CUSTOM && !hasCustomEngine);
const Engines = { ...DefaultEngines };
if (hasCustomEngine) {
Engines[customEngineName!] = customEngineURL;
}
Engines = { ...Engines, ...DefaultEngines };
if (hasValidReplacementEngine) {
const name = replacementEngine === ReplacementEngineValue.CUSTOM && hasCustomEngine
? customEngineName
: replacementEngine;
return (
<Menu.MenuItem
label={`Search with ${name}`}
key="search-custom-engine"
id="vc-search-custom-engine"
action={() => search(src, Engines[name!])}
/>
);
}
return (
<Menu.MenuItem
@ -95,7 +127,7 @@ const messageContextMenuPatch: NavContextMenuPatchCallback = (children, _props)
export default definePlugin({
name: "ReplaceGoogleSearch",
description: "Replaces the Google search with different Engines",
description: "Replaces the Google search with different Engine(s)",
authors: [Devs.Moxxie, Devs.Ethan],
settings,