Fix auto-translate plugin. Add new event types for post-rendering

This commit is contained in:
Cohee
2023-08-22 22:45:12 +03:00
parent 6599717bc5
commit fd95b79ae9
6 changed files with 84 additions and 37 deletions

View File

@ -1,4 +1,4 @@
import { callPopup, eventSource, event_types, saveSettings, saveSettingsDebounced, getRequestHeaders } from "../script.js";
import { callPopup, eventSource, event_types, saveSettings, saveSettingsDebounced, getRequestHeaders, substituteParams } from "../script.js";
import { isSubsetOf, debounce, waitUntilCondition } from "./utils.js";
export {
getContext,
@ -12,7 +12,7 @@ export {
};
let extensionNames = [];
let manifests = [];
let manifests = {};
const defaultUrl = "http://localhost:5100";
export const saveMetadataDebounced = debounce(async () => await getContext().saveMetadata(), 1000);
@ -31,7 +31,7 @@ export function registerExtensionHelper(name, helper) {
* Applies handlebars extension helpers to a message.
* @param {number} messageId Message index in the chat.
*/
function processExtensionHelpers(messageId) {
export function processExtensionHelpers(messageId) {
const context = getContext();
const message = context.chat[messageId];
@ -40,12 +40,12 @@ function processExtensionHelpers(messageId) {
}
// Don't waste time if there are no mustaches
if (!message.mes.includes('{{')) {
if (!substituteParams(message.mes).includes('{{')) {
return;
}
try {
const template = extensionsHandlebars.compile(message.mes, { noEscape: true });
const template = extensionsHandlebars.compile(substituteParams(message.mes), { noEscape: true });
message.mes = template({});
} catch {
// Ignore
@ -211,7 +211,10 @@ async function getManifests(names) {
} else {
reject();
}
}).catch(err => reject() && console.log('Could not load manifest.json for ' + name, err));
}).catch(err => {
reject();
console.log('Could not load manifest.json for ' + name, err);
});
});
promises.push(promise);
@ -268,9 +271,9 @@ async function activateExtensions() {
async function connectClickHandler() {
const baseUrl = $("#extensions_url").val();
extension_settings.apiUrl = baseUrl;
extension_settings.apiUrl = String(baseUrl);
const testApiKey = $("#extensions_api_key").val();
extension_settings.apiKey = testApiKey;
extension_settings.apiKey = String(testApiKey);
saveSettingsDebounced();
await connectToApi(baseUrl);
}
@ -495,7 +498,7 @@ async function generateExtensionHtml(name, manifest, isActive, isDisabled, isExt
* Gets extension data and generates the corresponding HTML for displaying the extension.
*
* @param {Array} extension - An array where the first element is the extension name and the second element is the extension manifest.
* @return {object} - An object with 'isExternal' indicating whether the extension is external, and 'extensionHtml' for the extension's HTML string.
* @return {Promise<object>} - An object with 'isExternal' indicating whether the extension is external, and 'extensionHtml' for the extension's HTML string.
*/
async function getExtensionData(extension) {
const name = extension[0];
@ -612,7 +615,7 @@ async function onDeleteClick() {
* Fetches the version details of a specific extension.
*
* @param {string} extensionName - The name of the extension.
* @return {object} - An object containing the extension's version details.
* @return {Promise<object>} - An object containing the extension's version details.
* This object includes the currentBranchName, currentCommitHash, isUpToDate, and remoteUrl.
* @throws {error} - If there is an error during the fetch operation, it logs the error to the console.
*/
@ -669,9 +672,6 @@ jQuery(function () {
setTimeout(async function () {
addExtensionsButtonAndMenu();
$("#extensionsMenuButton").css("display", "flex");
await waitUntilCondition(() => eventSource !== undefined, 1000, 100);
eventSource.on(event_types.MESSAGE_RECEIVED, processExtensionHelpers);
eventSource.on(event_types.MESSAGE_SENT, processExtensionHelpers);
}, 100)
$("#extensions_connect").on('click', connectClickHandler);