Add Chub downloader for characters and lorebooks

This commit is contained in:
Cohee
2023-06-27 18:39:08 +03:00
parent 92775e459c
commit 69fdb9090f
5 changed files with 242 additions and 75 deletions

View File

@ -32,6 +32,7 @@ import {
importEmbeddedWorldInfo,
checkEmbeddedWorld,
setWorldInfoButtonClass,
importWorldInfo,
} from "./scripts/world-info.js";
import {
@ -7924,6 +7925,55 @@ $(document).ready(function () {
restoreCaretPosition($(this).get(0), caretPosition);
});
$('#external_import_button').on('click', async () => {
const html = `<h3>Enter the URL of the content to import</h3>
Supported sources:<br>
<ul class="justifyLeft">
<li>Chub characters (direct link or id)<br>Example: <tt>lorebooks/bartleby/example-lorebook</tt></li>
<li>Chub lorebooks (direct link or id)<br>Example: <tt>Anonymous/example-character</tt></li>
<li>More coming soon...</li>
<ul>`
const input = await callPopup(html, 'input');
if (!input) {
console.debug('Custom content import cancelled');
return;
}
const url = input.trim();
console.debug('Custom content import started', url);
const request = await fetch('/import_custom', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ url }),
});
if (!request.ok) {
toastr.info(request.statusText, 'Custom content import failed');
console.error('Custom content import failed', request.status, request.statusText);
return;
}
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':
processDroppedFiles([file]);
break;
case 'lorebook':
await importWorldInfo(file);
break;
default:
toastr.warn('Unknown content type');
console.error('Unknown content type', customContentType);
break;
}
});
const $dropzone = $(document.body);
$dropzone.on('dragover', (event) => {