mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-04 13:17:36 +01:00
Load live2d by posting a file
This commit is contained in:
parent
29a3c5d590
commit
9d023dc3b1
@ -396,28 +396,56 @@ function onExpressionsShowDefaultInput() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadLiveChar(value_name) {
|
async function loadLiveChar() {
|
||||||
if (!live2d_var) {
|
|
||||||
console.debug('live2d is disabled');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!modules.includes('live2d')) {
|
if (!modules.includes('live2d')) {
|
||||||
console.debug('live2d module is disabled');
|
console.debug('live2d module is disabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = `${getApiUrl()}/api/live2d/load?loadchar=${location.origin}/characters/${value_name}`;
|
const context = getContext();
|
||||||
doExtrasFetch(url, {
|
let spriteFolderName = context.name2;
|
||||||
method: 'GET',
|
const message = getLastCharacterMessage();
|
||||||
headers: {
|
const avatarFileName = getSpriteFolderName(message);
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
const expressionOverride = extension_settings.expressionOverrides.find((e) =>
|
||||||
'Bypass-Tunnel-Reminder': 'bypass',
|
e.name == avatarFileName
|
||||||
},
|
);
|
||||||
})
|
|
||||||
.then(response => response.text())
|
if (expressionOverride && expressionOverride.path) {
|
||||||
.then(data => console.log(data))
|
spriteFolderName = expressionOverride.path;
|
||||||
.catch((error) => console.error('Error:', error));
|
}
|
||||||
|
|
||||||
|
const live2dPath = `/characters/${encodeURIComponent(spriteFolderName)}/live2d.png`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const spriteResponse = await fetch(live2dPath);
|
||||||
|
|
||||||
|
if (!spriteResponse.ok) {
|
||||||
|
throw new Error(spriteResponse.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
const spriteBlob = await spriteResponse.blob();
|
||||||
|
const spriteFile = new File([spriteBlob], 'live2d.png', { type: 'image/png' });
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', spriteFile);
|
||||||
|
|
||||||
|
const url = new URL(getApiUrl());
|
||||||
|
url.pathname = '/api/live2d/load';
|
||||||
|
|
||||||
|
const loadResponse = await doExtrasFetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!loadResponse.ok) {
|
||||||
|
throw new Error(loadResponse.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadResponseText = await loadResponse.text();
|
||||||
|
console.log(`Load live2d response: ${loadResponseText}`);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error loading live2d image: ${live2dPath} - ${error}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -484,7 +512,7 @@ async function moduleWorker() {
|
|||||||
|
|
||||||
//Load new char
|
//Load new char
|
||||||
if (live2d_var) {
|
if (live2d_var) {
|
||||||
loadLiveChar(context.name2 + ".png");
|
loadLiveChar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1209,10 +1237,8 @@ function setExpressionOverrideHtml(forceClear = false) {
|
|||||||
|
|
||||||
$('#image_type_toggle').on('change', function () {
|
$('#image_type_toggle').on('change', function () {
|
||||||
const isChecked = this.checked;
|
const isChecked = this.checked;
|
||||||
const inputElement = document.querySelector('input[name="avatar_url"]');
|
|
||||||
const value_name = inputElement ? inputElement.value : '';
|
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
loadLiveChar(value_name);
|
loadLiveChar();
|
||||||
}
|
}
|
||||||
handleImageChange(isChecked);
|
handleImageChange(isChecked);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user