Fix macros replacement in custom stop strings for OAI

This commit is contained in:
Cohee 2023-08-29 00:47:35 +03:00
parent b10f0b0ae8
commit 1bb2778f46
2 changed files with 17 additions and 9 deletions

View File

@ -1801,11 +1801,7 @@ function getStoppingStrings(isImpersonate, addSpace) {
if (power_user.custom_stopping_strings) {
const customStoppingStrings = getCustomStoppingStrings();
if (power_user.custom_stopping_strings_macro) {
result.push(...customStoppingStrings.map(x => substituteParams(x, name1, name2)));
} else {
result.push(...customStoppingStrings);
}
result.push(...customStoppingStrings);
}
return addSpace ? result.map(x => `${x} `) : result;

View File

@ -1616,7 +1616,7 @@ function setAvgBG() {
/**
* Gets the custom stopping strings from the power user settings.
* @param {number | undefined} limit Number of strings to return. If undefined, returns all strings.
* @param {number | undefined} limit Number of strings to return. If 0 or undefined, returns all strings.
* @returns {string[]} An array of custom stopping strings
*/
export function getCustomStoppingStrings(limit = undefined) {
@ -1627,15 +1627,27 @@ export function getCustomStoppingStrings(limit = undefined) {
}
// Parse the JSON string
const strings = JSON.parse(power_user.custom_stopping_strings);
let strings = JSON.parse(power_user.custom_stopping_strings);
// Make sure it's an array
if (!Array.isArray(strings)) {
return [];
}
// Make sure all the elements are strings. Apply the limit.
return strings.filter((s) => typeof s === 'string').slice(0, limit);
// Make sure all the elements are strings.
strings = strings.filter((s) => typeof s === 'string');
// Substitute params if necessary
if (power_user.custom_stopping_strings_macro) {
strings = strings.map(x => substituteParams(x));
}
// Apply the limit. If limit is 0, return all strings.
if (limit > 0) {
strings = strings.slice(0, limit);
}
return strings;
} catch (error) {
// If there's an error, return an empty array
console.warn('Error parsing custom stopping strings:', error);