From e93bc49b3613665d978bc61a1d5b1e4c2bae1b17 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sun, 2 Jun 2024 23:44:50 +0200 Subject: [PATCH 01/25] Support importing multiple URLs on external import --- public/script.js | 80 ++++++++++--------- .../scripts/templates/importCharacters.html | 26 +++--- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/public/script.js b/public/script.js index f7a4f8080..7fed9be28 100644 --- a/public/script.js +++ b/public/script.js @@ -10537,6 +10537,8 @@ jQuery(async function () { $(document).on('click', '.external_import_button, #external_import_button', async () => { const html = await renderTemplateAsync('importCharacters'); + + /** @type {string?} */ const input = await callGenericPopup(html, POPUP_TYPE.INPUT, '', { okButton: $('#shadow_popup_template').attr('popup_text_import'), rows: 4 }); if (!input) { @@ -10544,47 +10546,51 @@ jQuery(async function () { return; } - const url = input.trim(); - var request; + // break input into one input per line + const inputs = input.split('\n').map(x => x.trim()).filter(x => x.length > 0); - if (isValidUrl(url)) { - console.debug('Custom content import started for URL: ', url); - request = await fetch('/api/content/importURL', { - method: 'POST', - headers: getRequestHeaders(), - body: JSON.stringify({ url }), - }); - } else { - console.debug('Custom content import started for Char UUID: ', url); - request = await fetch('/api/content/importUUID', { - method: 'POST', - headers: getRequestHeaders(), - body: JSON.stringify({ url }), - }); - } + for (const url of inputs) { + var request; - if (!request.ok) { - toastr.info(request.statusText, 'Custom content import failed'); - console.error('Custom content import failed', request.status, request.statusText); - return; - } + if (isValidUrl(url)) { + console.debug('Custom content import started for URL: ', url); + request = await fetch('/api/content/importURL', { + method: 'POST', + headers: getRequestHeaders(), + body: JSON.stringify({ url }), + }); + } else { + console.debug('Custom content import started for Char UUID: ', url); + request = await fetch('/api/content/importUUID', { + method: 'POST', + headers: getRequestHeaders(), + body: JSON.stringify({ url }), + }); + } - const data = await request.blob(); - const customContentType = request.headers.get('X-Custom-Content-Type'); - const fileName = request.headers.get('Content-Disposition').split('filename=')[1].replace(/"/g, ''); - const file = new File([data], fileName, { type: data.type }); + if (!request.ok) { + toastr.info(request.statusText, 'Custom content import failed'); + console.error('Custom content import failed', request.status, request.statusText); + return; + } - switch (customContentType) { - case 'character': - await processDroppedFiles([file]); - break; - case 'lorebook': - await importWorldInfo(file); - break; - default: - toastr.warning('Unknown content type'); - console.error('Unknown content type', customContentType); - break; + const data = await request.blob(); + const customContentType = request.headers.get('X-Custom-Content-Type'); + const fileName = request.headers.get('Content-Disposition').split('filename=')[1].replace(/"/g, ''); + const file = new File([data], fileName, { type: data.type }); + + switch (customContentType) { + case 'character': + await processDroppedFiles([file]); + break; + case 'lorebook': + await importWorldInfo(file); + break; + default: + toastr.warning('Unknown content type'); + console.error('Unknown content type', customContentType); + break; + } } }); diff --git a/public/scripts/templates/importCharacters.html b/public/scripts/templates/importCharacters.html index 7ea497892..e8c5598cc 100644 --- a/public/scripts/templates/importCharacters.html +++ b/public/scripts/templates/importCharacters.html @@ -1,11 +1,15 @@ -

Enter the URL of the content to import

-Supported sources:
-