Macros 2.0 [➕Macros] - Add `{{hasExtension}}` macro, and refactor extension lookup logic (#4948) * Add {{hasExtension}} macro and refactor extension lookup logic - Move findExtension() from extensions-slashcommands.js to extensions.js and export it - Update findExtension() to return object with name and enabled properties instead of just name string - Add {{hasExtension}} macro to check if an extension is enabled - Update /extension-state and /extension-exists commands to use refactored findExtension() - Remove duplicate findExtension() implementation from extensions-slashcommands.js * Refactor extension action callbacks to use extension object instead of separate name and enabled properties * fix eslint
Signed| @@ -1,11 +1,11 @@ | |||
| 1 | import { disableExtension, enableExtension, extension_settings, extensionNames } from './extensions.js'; | 1 | import { disableExtension, enableExtension, extensionNames, findExtension } from './extensions.js'; |
| 2 | import { SlashCommand } from './slash-commands/SlashCommand.js'; | 2 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 3 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; | 3 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; |
| 4 | import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; | 4 | import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; |
| 5 | import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js'; | 5 | import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 6 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; | 6 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; |
| 7 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; | 7 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 8 | import { equalsIgnoreCaseAndAccents, isFalseBoolean, isTrueBoolean } from './utils.js'; | 8 | import { isFalseBoolean, isTrueBoolean } from './utils.js'; |
| 9 | 9 | ||
| 10 | /** | 10 | /** |
| 11 | * @param {'enable' | 'disable' | 'toggle'} action - The action to perform on the extension | 11 | * @param {'enable' | 'disable' | 'toggle'} action - The action to perform on the extension |
| @@ -22,30 +22,28 @@ function getExtensionActionCallback(action) { | |||
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | const reload = !isFalseBoolean(args?.reload?.toString()); | 24 | const reload = !isFalseBoolean(args?.reload?.toString()); |
| 25 | const internalExtensionName = findExtension(extensionName); | 25 | const extension = findExtension(extensionName); |
| 26 | if (!internalExtensionName) { | 26 | if (!extension) { |
| 27 | toastr.warning(`Extension ${extensionName} does not exist.`); | 27 | toastr.warning(`Extension ${extensionName} does not exist.`); |
| 28 | return ''; | 28 | return ''; |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName); | 31 | if (action === 'enable' && extension.enabled) { |
| 32 | 32 | toastr.info(`Extension ${extension.name} is already enabled.`); | |
| 33 | if (action === 'enable' && isEnabled) { | 33 | return extension.name; |
| 34 | toastr.info(`Extension ${extensionName} is already enabled.`); | ||
| 35 | return internalExtensionName; | ||
| 36 | } | 34 | } |
| 37 | 35 | ||
| 38 | if (action === 'disable' && !isEnabled) { | 36 | if (action === 'disable' && !extension.enabled) { |
| 39 | toastr.info(`Extension ${extensionName} is already disabled.`); | 37 | toastr.info(`Extension ${extension.name} is already disabled.`); |
| 40 | return internalExtensionName; | 38 | return extension.name; |
| 41 | } | 39 | } |
| 42 | 40 | ||
| 43 | if (action === 'toggle') { | 41 | if (action === 'toggle') { |
| 44 | action = isEnabled ? 'disable' : 'enable'; | 42 | action = extension.enabled ? 'disable' : 'enable'; |
| 45 | } | 43 | } |
| 46 | 44 | ||
| 47 | if (reload) { | 45 | if (reload) { |
| 48 | toastr.info(`${action.charAt(0).toUpperCase() + action.slice(1)}ing extension ${extensionName} and reloading...`); | 46 | toastr.info(`${action.charAt(0).toUpperCase() + action.slice(1)}ing extension ${extension.name} and reloading...`); |
| 49 | 47 | ||
| 50 | // Clear input, so it doesn't stay because the command didn't "finish", | 48 | // Clear input, so it doesn't stay because the command didn't "finish", |
| 51 | // and wait for a bit to both show the toast and let the clear bubble through. | 49 | // and wait for a bit to both show the toast and let the clear bubble through. |
| @@ -54,36 +52,24 @@ function getExtensionActionCallback(action) { | |||
| 54 | } | 52 | } |
| 55 | 53 | ||
| 56 | if (action === 'enable') { | 54 | if (action === 'enable') { |
| 57 | await enableExtension(internalExtensionName, reload); | 55 | await enableExtension(extension.name, reload); |
| 58 | } else { | 56 | } else { |
| 59 | await disableExtension(internalExtensionName, reload); | 57 | await disableExtension(extension.name, reload); |
| 60 | } | 58 | } |
| 61 | 59 | ||
| 62 | toastr.success(`Extension ${extensionName} ${action}d.`); | 60 | toastr.success(`Extension ${extension.name} ${action}d.`); |
| 63 | 61 | ||
| 64 | 62 | ||
| 65 | console.info(`Extension ${action}ed: ${extensionName}`); | 63 | console.info(`Extension ${action}ed: ${extension.name}`); |
| 66 | if (!reload) { | 64 | if (!reload) { |
| 67 | console.info('Reload not requested, so page needs to be reloaded manually for changes to take effect.'); | 65 | console.info('Reload not requested, so page needs to be reloaded manually for changes to take effect.'); |
| 68 | } | 66 | } |
| 69 | 67 | ||
| 70 | return internalExtensionName; | 68 | return extension.name; |
| 71 | }; | 69 | }; |
| 72 | } | 70 | } |
| 73 | 71 | ||
| 74 | /** | 72 | /** |
| 75 | * Finds an extension by name, allowing omission of the "third-party/" prefix. | ||
| 76 | * | ||
| 77 | * @param {string} name - The name of the extension to find | ||
| 78 | * @returns {string?} - The matched extension name or undefined if not found | ||
| 79 | */ | ||
| 80 | function findExtension(name) { | ||
| 81 | return extensionNames.find(extName => { | ||
| 82 | return equalsIgnoreCaseAndAccents(extName, name) || equalsIgnoreCaseAndAccents(extName, `third-party/${name}`); | ||
| 83 | }); | ||
| 84 | } | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Provides an array of SlashCommandEnumValue objects based on the extension names. | 73 | * Provides an array of SlashCommandEnumValue objects based on the extension names. |
| 88 | * Each object contains the name of the extension and a description indicating if it is a third-party extension. | 74 | * Each object contains the name of the extension and a description indicating if it is a third-party extension. |
| 89 | * | 75 | * |
| @@ -244,14 +230,13 @@ export function registerExtensionSlashCommands() { | |||
| 244 | name: 'extension-state', | 230 | name: 'extension-state', |
| 245 | callback: async (_, extensionName) => { | 231 | callback: async (_, extensionName) => { |
| 246 | if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.'); | 232 | if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.'); |
| 247 | const internalExtensionName = findExtension(extensionName); | 233 | const extension = findExtension(extensionName); |
| 248 | if (!internalExtensionName) { | 234 | if (!extension) { |
| 249 | toastr.warning(`Extension ${extensionName} does not exist.`); | 235 | toastr.warning(`Extension ${extensionName} does not exist.`); |
| 250 | return ''; | 236 | return ''; |
| 251 | } | 237 | } |
| 252 | 238 | ||
| 253 | const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName); | 239 | return String(extension.enabled); |
| 254 | return String(isEnabled); | ||
| 255 | }, | 240 | }, |
| 256 | returns: 'The state of the extension, whether it is enabled.', | 241 | returns: 'The state of the extension, whether it is enabled.', |
| 257 | unnamedArgumentList: [ | 242 | unnamedArgumentList: [ |
| @@ -282,8 +267,8 @@ export function registerExtensionSlashCommands() { | |||
| 282 | aliases: ['extension-installed'], | 267 | aliases: ['extension-installed'], |
| 283 | callback: async (_, extensionName) => { | 268 | callback: async (_, extensionName) => { |
| 284 | if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.'); | 269 | if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.'); |
| 285 | const exists = findExtension(extensionName) !== undefined; | 270 | const extension = findExtension(extensionName); |
| 286 | return exists ? 'true' : 'false'; | 271 | return extension !== null ? 'true' : 'false'; |
| 287 | }, | 272 | }, |
| 288 | returns: 'Whether the extension exists and is installed.', | 273 | returns: 'Whether the extension exists and is installed.', |
| 289 | unnamedArgumentList: [ | 274 | unnamedArgumentList: [ |
| @@ -4,7 +4,7 @@ import { eventSource, event_types, saveSettings, saveSettingsDebounced, getReque | |||
| 4 | import { showLoader } from './loader.js'; | 4 | import { showLoader } from './loader.js'; |
| 5 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup } from './popup.js'; | 5 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup } from './popup.js'; |
| 6 | import { renderTemplate, renderTemplateAsync } from './templates.js'; | 6 | import { renderTemplate, renderTemplateAsync } from './templates.js'; |
| 7 | import { delay, isSubsetOf, sanitizeSelector, setValueByPath, versionCompare } from './utils.js'; | 7 | import { delay, equalsIgnoreCaseAndAccents, isSubsetOf, sanitizeSelector, setValueByPath, versionCompare } from './utils.js'; |
| 8 | import { getContext } from './st-context.js'; | 8 | import { getContext } from './st-context.js'; |
| 9 | import { isAdmin } from './user.js'; | 9 | import { isAdmin } from './user.js'; |
| 10 | import { addLocaleData, getCurrentLocale, t } from './i18n.js'; | 10 | import { addLocaleData, getCurrentLocale, t } from './i18n.js'; |
| @@ -332,6 +332,21 @@ export async function disableExtension(name, reload = true) { | |||
| 332 | } | 332 | } |
| 333 | 333 | ||
| 334 | /** | 334 | /** |
| 335 | * Finds an extension by name, allowing omission of the "third-party/" prefix. | ||
| 336 | * | ||
| 337 | * @param {string} name - The name of the extension to find | ||
| 338 | * @returns {{name: string, enabled: boolean}|null} Object with name and enabled properties, or null if not found | ||
| 339 | */ | ||
| 340 | export function findExtension(name) { | ||
| 341 | const internalExtensionName = extensionNames.find(extName => { | ||
| 342 | return equalsIgnoreCaseAndAccents(extName, name) || equalsIgnoreCaseAndAccents(extName, `third-party/${name}`); | ||
| 343 | }); | ||
| 344 | if (!internalExtensionName) return null; | ||
| 345 | const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName); | ||
| 346 | return { name: internalExtensionName, enabled: isEnabled }; | ||
| 347 | } | ||
| 348 | |||
| 349 | /** | ||
| 335 | * Loads manifest.json files for extensions. | 350 | * Loads manifest.json files for extensions. |
| 336 | * @param {string[]} names Array of extension names | 351 | * @param {string[]} names Array of extension names |
| 337 | * @returns {Promise<Record<string, object>>} Object with extension names as keys and their manifests as values | 352 | * @returns {Promise<Record<string, object>>} Object with extension names as keys and their manifests as values |
| @@ -1,5 +1,6 @@ | |||
| 1 | import { MacroRegistry, MacroCategory } from '../engine/MacroRegistry.js'; | 1 | import { MacroRegistry, MacroCategory } from '../engine/MacroRegistry.js'; |
| 2 | import { eventSource, event_types } from '../../events.js'; | 2 | import { eventSource, event_types } from '../../events.js'; |
| 3 | import { findExtension } from '/scripts/extensions.js'; | ||
| 3 | 4 | ||
| 4 | let lastGenerationTypeValue = ''; | 5 | let lastGenerationTypeValue = ''; |
| 5 | let lastGenerationTypeTrackingInitialized = false; | 6 | let lastGenerationTypeTrackingInitialized = false; |
| @@ -37,4 +38,20 @@ export function registerStateMacros() { | |||
| 37 | returns: 'Type of the last queued generation request.', | 38 | returns: 'Type of the last queued generation request.', |
| 38 | handler: () => lastGenerationTypeValue, | 39 | handler: () => lastGenerationTypeValue, |
| 39 | }); | 40 | }); |
| 41 | |||
| 42 | // Macro that checks if an extension is enabled | ||
| 43 | MacroRegistry.registerMacro('hasExtension', { | ||
| 44 | category: MacroCategory.STATE, | ||
| 45 | unnamedArgs: [{ | ||
| 46 | name: 'extensionName', | ||
| 47 | type: 'string', | ||
| 48 | description: 'The name of the extension to check', | ||
| 49 | }], | ||
| 50 | description: 'Checks if a specific extension is enabled. If the extension does not exist, returns false.', | ||
| 51 | returns: 'true if the extension is enabled, false otherwise.', | ||
| 52 | handler: ({ unnamedArgs: [extensionName] }) => { | ||
| 53 | const extension = findExtension(extensionName); | ||
| 54 | return String(extension?.enabled ?? false); | ||
| 55 | }, | ||
| 56 | }); | ||
| 40 | } | 57 | } |