Refactor extension install menu

7d3f544e63a27d962b91506a4612814d9bf1da76

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +32 -34Ignore whitespace
public/scripts/extensions.js+23 -21
@@ -989,6 +989,28 @@ export async function writeExtensionField(characterId, key, value) {
989 }989 }
990}990}
991991
992/**
993 * Prompts the user to enter the Git URL of the extension to import.
994 * After obtaining the Git URL, makes a POST request to '/api/extensions/install' to import the extension.
995 * If the extension is imported successfully, a success message is displayed.
996 * If the extension import fails, an error message is displayed and the error is logged to the console.
997 * After successfully importing the extension, the extension settings are reloaded and a 'EXTENSION_SETTINGS_LOADED' event is emitted.
998 * @param {string} [suggestUrl] Suggested URL to install
999 * @returns {Promise<void>}
1000 */
1001export async function openThirdPartyExtensionMenu(suggestUrl = '') {
1002 const html = await renderTemplateAsync('installExtension');
1003 const input = await callGenericPopup(html, POPUP_TYPE.INPUT, suggestUrl ?? '');
1004
1005 if (!input) {
1006 console.debug('Extension install cancelled');
1007 return;
1008 }
1009
1010 const url = String(input).trim();
1011 await installExtension(url);
1012}
1013
992jQuery(async function () {1014jQuery(async function () {
993 await addExtensionsButtonAndMenu();1015 await addExtensionsButtonAndMenu();
994 $('#extensionsMenuButton').css('display', 'flex');1016 $('#extensionsMenuButton').css('display', 'flex');
@@ -1004,28 +1026,8 @@ jQuery(async function () {
10041026
1005 /**1027 /**
1006 * Handles the click event for the third-party extension import button.1028 * Handles the click event for the third-party extension import button.
1007 * Prompts the user to enter the Git URL of the extension to import.
1008 * After obtaining the Git URL, makes a POST request to '/api/extensions/install' to import the extension.
1009 * If the extension is imported successfully, a success message is displayed.
1010 * If the extension import fails, an error message is displayed and the error is logged to the console.
1011 * After successfully importing the extension, the extension settings are reloaded and a 'EXTENSION_SETTINGS_LOADED' event is emitted.
1012 *1029 *
1013 * @listens #third_party_extension_button#click - The click event of the '#third_party_extension_button' element.1030 * @listens #third_party_extension_button#click - The click event of the '#third_party_extension_button' element.
1014 */1031 */
1015 $('#third_party_extension_button').on('click', async () => {1032 $('#third_party_extension_button').on('click', () => openThirdPartyExtensionMenu());
1016 const html = `<h3>Enter the Git URL of the extension to install</h3>
1017 <br>
1018 <p><b>Disclaimer:</b> Please be aware that using external extensions can have unintended side effects and may pose security risks. Always make sure you trust the source before importing an extension. We are not responsible for any damage caused by third-party extensions.</p>
1019 <br>
1020 <p>Example: <tt> https://github.com/author/extension-name </tt></p>`;
1021 const input = await callGenericPopup(html, POPUP_TYPE.INPUT, '');
1022
1023 if (!input) {
1024 console.debug('Extension install cancelled');
1025 return;
1026 }
1027
1028 const url = String(input).trim();
1029 await installExtension(url);
1030 });
1031});1033});
public/scripts/extensions/shared.js+2 -13
@@ -1,5 +1,5 @@
1import { getRequestHeaders } from '../../script.js';1import { getRequestHeaders } from '../../script.js';
2import { extension_settings } from '../extensions.js';2import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js';
3import { oai_settings } from '../openai.js';3import { oai_settings } from '../openai.js';
4import { SECRET_KEYS, secret_state } from '../secrets.js';4import { SECRET_KEYS, secret_state } from '../secrets.js';
5import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js';5import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js';
@@ -202,18 +202,7 @@ export function isWebLlmSupported() {
202 timeOut: 0,202 timeOut: 0,
203 extendedTimeOut: 0,203 extendedTimeOut: 0,
204 preventDuplicates: true,204 preventDuplicates: true,
205 onclick: () => {205 onclick: () => openThirdPartyExtensionMenu('https://github.com/SillyTavern/Extension-WebLLM'),
206 const button = document.getElementById('third_party_extension_button');
207 if (button) {
208 button.click();
209 }
210
211 const input = document.querySelector('dialog textarea');
212
213 if (input instanceof HTMLTextAreaElement) {
214 input.value = 'https://github.com/SillyTavern/Extension-WebLLM';
215 }
216 },
217 });206 });
218 sessionStorage.setItem(warningKey, '1');207 sessionStorage.setItem(warningKey, '1');
219 }208 }
public/scripts/templates/installExtension.html+7 -0
@@ -0,0 +1,7 @@
1<h3>Enter the Git URL of the extension to install</h3>
2<br>
3<p><b>Disclaimer:</b> Please be aware that using external extensions can have unintended side effects and may pose
4 security risks. Always make sure you trust the source before importing an extension. We are not responsible for any
5 damage caused by third-party extensions.</p>
6<br>
7<p>Example: <tt> https://github.com/author/extension-name </tt></p>