Regex: allow multiple definitions in a single file
| @@ -459,19 +459,12 @@ async function toggleRegexCallback(args, scriptName) { | |||
| 459 | } | 459 | } |
| 460 | 460 | ||
| 461 | /** | 461 | /** |
| 462 | * Performs the import of the regex file. | 462 | * Performs the import of the regex object. |
| 463 | * @param {File} file Input file | 463 | * @param {Object} regexScript Input object |
| 464 | * @param {boolean} isScoped Is the script scoped to a character? | 464 | * @param {boolean} isScoped Is the script scoped to a character? |
| 465 | */ | 465 | */ |
| 466 | async function onRegexImportFileChange(file, isScoped) { | 466 | async function onRegexImportObjectChange(regexScript, isScoped) { |
| 467 | if (!file) { | ||
| 468 | toastr.error('No file provided.'); | ||
| 469 | return; | ||
| 470 | } | ||
| 471 | |||
| 472 | try { | 467 | try { |
| 473 | const fileText = await getFileText(file); | ||
| 474 | const regexScript = JSON.parse(fileText); | ||
| 475 | if (!regexScript.scriptName) { | 468 | if (!regexScript.scriptName) { |
| 476 | throw new Error('No script name provided.'); | 469 | throw new Error('No script name provided.'); |
| 477 | } | 470 | } |
| @@ -491,6 +484,33 @@ async function onRegexImportFileChange(file, isScoped) { | |||
| 491 | toastr.success(`Regex script "${regexScript.scriptName}" imported.`); | 484 | toastr.success(`Regex script "${regexScript.scriptName}" imported.`); |
| 492 | } catch (error) { | 485 | } catch (error) { |
| 493 | console.log(error); | 486 | console.log(error); |
| 487 | toastr.error('Invalid regex object.'); | ||
| 488 | return; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | /** | ||
| 493 | * Performs the import of the regex file. | ||
| 494 | * @param {File} file Input file | ||
| 495 | * @param {boolean} isScoped Is the script scoped to a character? | ||
| 496 | */ | ||
| 497 | async function onRegexImportFileChange(file, isScoped) { | ||
| 498 | if (!file) { | ||
| 499 | toastr.error('No file provided.'); | ||
| 500 | return; | ||
| 501 | } | ||
| 502 | |||
| 503 | try { | ||
| 504 | const regexScripts = JSON.parse(await getFileText(file)); | ||
| 505 | if (Array.isArray(regexScripts)) { | ||
| 506 | for (const regexScript of regexScripts) { | ||
| 507 | await onRegexImportObjectChange(regexScript, isScoped); | ||
| 508 | } | ||
| 509 | } else { | ||
| 510 | await onRegexImportObjectChange(regexScripts, isScoped); | ||
| 511 | } | ||
| 512 | } catch (error) { | ||
| 513 | console.log(error); | ||
| 494 | toastr.error('Invalid JSON file.'); | 514 | toastr.error('Invalid JSON file.'); |
| 495 | return; | 515 | return; |
| 496 | } | 516 | } |