Autoswipe: Fix endless loop if blacklist empty

This commit is contained in:
Cohee
2025-01-29 23:01:37 +02:00
parent 15a3cfcb8a
commit c32e0bdde7
2 changed files with 47 additions and 57 deletions

View File

@ -2916,6 +2916,46 @@ export function flushEphemeralStoppingStrings() {
EPHEMERAL_STOPPING_STRINGS.splice(0, EPHEMERAL_STOPPING_STRINGS.length);
}
/**
* Checks if the generated text should be filtered based on the auto-swipe settings.
* @param {string} text The text to check
* @returns {boolean} If the generated text should be filtered
*/
export function generatedTextFiltered(text) {
/**
* Checks if the given text contains any of the blacklisted words.
* @param {string} text The text to check
* @param {string[]} blacklist The list of blacklisted words
* @param {number} threshold The number of blacklisted words that need to be present to trigger the check
* @returns {boolean} Whether the text contains blacklisted words
*/
function containsBlacklistedWords(text, blacklist, threshold) {
const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
const matches = text.match(regex) || [];
return matches.length >= threshold;
}
// Make sure a generated text is non-empty
// Otherwise we might get in a loop with a broken API
text = text.trim();
if (text.length > 0) {
if (power_user.auto_swipe_minimum_length) {
if (text.length < power_user.auto_swipe_minimum_length) {
console.log('Generated text size too small');
return true;
}
}
if (power_user.auto_swipe_blacklist.length && power_user.auto_swipe_blacklist_threshold) {
if (containsBlacklistedWords(text, power_user.auto_swipe_blacklist, power_user.auto_swipe_blacklist_threshold)) {
console.log('Generated text has blacklisted words');
return true;
}
}
}
return false;
}
/**
* Gets the custom stopping strings from the power user settings.
* @param {number | undefined} limit Number of strings to return. If 0 or undefined, returns all strings.