Make Run on edit actually work Fixes #2958

9e50ff34c99e3d405897e2df0d13fb19a01047dd

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +12 -4Ignore whitespace
public/script.js+4 -1
@@ -6798,7 +6798,10 @@ function updateMessage(div) {
6798 text = getRegexedString(6798 text = getRegexedString(
6799 text,6799 text,
6800 regexPlacement,6800 regexPlacement,
6801 { characterOverride: mes.extra?.type === 'narrator' ? undefined : mes.name },6801 {
6802 characterOverride: mes.extra?.type === 'narrator' ? undefined : mes.name,
6803 isEdit: true,
6804 },
6802 );6805 );
68036806
68046807
public/scripts/extensions/regex/editor.html+1 -1
@@ -117,7 +117,7 @@
117 <input type="checkbox" name="disabled" />117 <input type="checkbox" name="disabled" />
118 <span data-i18n="Disabled">Disabled</span>118 <span data-i18n="Disabled">Disabled</span>
119 </label>119 </label>
120 <label class="checkbox flex-container">120 <label class="checkbox flex-container" title="Run the regex script when the message belonging a to specified role(s) is edited.">
121 <input type="checkbox" name="run_on_edit" />121 <input type="checkbox" name="run_on_edit" />
122 <span data-i18n="Run On Edit">Run On Edit</span>122 <span data-i18n="Run On Edit">Run On Edit</span>
123 </label>123 </label>
public/scripts/extensions/regex/engine.js+7 -2
@@ -44,9 +44,9 @@ function getScopedRegex() {
44 * @param {regex_placement} placement The placement of the string44 * @param {regex_placement} placement The placement of the string
45 * @param {RegexParams} params The parameters to use for the regex script45 * @param {RegexParams} params The parameters to use for the regex script
46 * @returns {string} The regexed string46 * @returns {string} The regexed string
47 * @typedef {{characterOverride?: string, isMarkdown?: boolean, isPrompt?: boolean, depth?: number }} RegexParams The parameters to use for the regex script47 * @typedef {{characterOverride?: string, isMarkdown?: boolean, isPrompt?: boolean, isEdit?: boolean, depth?: number }} RegexParams The parameters to use for the regex script
48 */48 */
49function getRegexedString(rawString, placement, { characterOverride, isMarkdown, isPrompt, depth } = {}) {49function getRegexedString(rawString, placement, { characterOverride, isMarkdown, isPrompt, isEdit, depth } = {}) {
50 // WTF have you passed me?50 // WTF have you passed me?
51 if (typeof rawString !== 'string') {51 if (typeof rawString !== 'string') {
52 console.warn('getRegexedString: rawString is not a string. Returning empty string.');52 console.warn('getRegexedString: rawString is not a string. Returning empty string.');
@@ -68,6 +68,11 @@ function getRegexedString(rawString, placement, { characterOverride, isMarkdown,
68 // Script applies to all cases when neither "only"s are true, but there's no need to do it when `isMarkdown`, the as source (chat history) should already be changed beforehand68 // Script applies to all cases when neither "only"s are true, but there's no need to do it when `isMarkdown`, the as source (chat history) should already be changed beforehand
69 (!script.markdownOnly && !script.promptOnly && !isMarkdown)69 (!script.markdownOnly && !script.promptOnly && !isMarkdown)
70 ) {70 ) {
71 if (isEdit && !script.runOnEdit) {
72 console.debug(`getRegexedString: Skipping script ${script.scriptName} because it does not run on edit`);
73 return;
74 }
75
71 // Check if the depth is within the min/max depth76 // Check if the depth is within the min/max depth
72 if (typeof depth === 'number' && depth >= 0) {77 if (typeof depth === 'number' && depth >= 0) {
73 if (!isNaN(script.minDepth) && script.minDepth !== null && script.minDepth >= 0 && depth < script.minDepth) {78 if (!isNaN(script.minDepth) && script.minDepth !== null && script.minDepth >= 0 && depth < script.minDepth) {