Regex: add export buttons for global and scoped scripts

6df95b98ecdfadedd65570051e9309ab9de558ae

gk <gkpyon@gmail.com>

2 files changed, +29 -1Showing whitespace changes
public/scripts/extensions/regex/dropdown.html+8 -0
@@ -21,6 +21,14 @@
2121 <small data-i18n="ext_regex_import_script">Import</small>
2222 </div>
2323 <input type="file" id="import_regex_file" hidden accept="*.json" multiple />
24+ <div id="export_global_regex" class="menu_button menu_button_icon">
25+ <i class="fa-solid fa-file-export"></i>
26+ <small data-i18n="ext_regex_export_global">Export Global</small>
27+ </div>
28+ <div id="export_scoped_regex" class="menu_button menu_button_icon">
29+ <i class="fa-solid fa-file-export"></i>
30+ <small data-i18n="ext_regex_export_scoped">Export Scoped</small>
31+ </div>
2432 </div>
2533 <hr />
2634 <div id="global_scripts_block" class="padding5">
public/scripts/extensions/regex/index.js+21 -1
@@ -12,6 +12,8 @@ import { regex_placement, runRegexScript, substitute_find_regex } from './engine
1212import { t } from '../../i18n.js';
1313import { accountStorage } from '../../util/AccountStorage.js';
1414
15+const sanitizeFileName = name => name.replace(/[\s.<>:"/\\|?*\x00-\x1F\x7F]/g, '_').toLowerCase();
16+
1517/**
1618 * @typedef {import('../../char-data.js').RegexScriptData} RegexScript
1719 */
@@ -167,7 +169,7 @@ async function loadRegexScripts() {
167169 await saveRegexScript(script, -1, true);
168170 });
169171 scriptHtml.find('.export_regex').on('click', async function () {
170- const fileName = `${script.scriptName.replace(/[\s.<>:"/\\|?*\x00-\x1F\x7F]/g, '_').toLowerCase()}.json`;
172+ const fileName = `regex-${sanitizeFileName(script.scriptName)}.json`;
171173 const fileData = JSON.stringify(script, null, 4);
172174 download(fileData, fileName, 'application/json');
173175 });
@@ -605,6 +607,24 @@ jQuery(async () => {
605607 $('#import_regex_file').trigger('click');
606608 });
607609
610+ $('#export_global_regex').on('click', async function () {
611+ const regexScripts = extension_settings?.regex ?? [];
612+ if (regexScripts.length) {
613+ const fileName = 'regex-global.json';
614+ const fileData = JSON.stringify(regexScripts, null, 4);
615+ download(fileData, fileName, 'application/json');
616+ }
617+ });
618+
619+ $('#export_scoped_regex').on('click', async function () {
620+ const regexScripts = characters[this_chid]?.data?.extensions?.regex_scripts ?? [];
621+ if (regexScripts.length) {
622+ const fileName = `regex-${sanitizeFileName(characters[this_chid].name)}.json`;
623+ const fileData = JSON.stringify(regexScripts, null, 4);
624+ download(fileData, fileName, 'application/json');
625+ }
626+ });
627+
608628 let sortableDatas = [
609629 {
610630 selector: '#saved_regex_scripts',