Refactor extension install menu

7d3f544e63a27d962b91506a4612814d9bf1da76

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

3 files changed, +32 -34Showing whitespace changes
public/scripts/extensions.js+23 -21
@@ -989,6 +989,28 @@ export async function writeExtensionField(characterId, key, value) {
989989 }
990990}
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+ */
1001+export 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+
9921014jQuery(async function () {
9931015 await addExtensionsButtonAndMenu();
9941016 $('#extensionsMenuButton').css('display', 'flex');
@@ -1004,28 +1026,8 @@ jQuery(async function () {
10041026
10051027 /**
10061028 * 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.
10121029 *
10131030 * @listens #third_party_extension_button#click - The click event of the '#third_party_extension_button' element.
10141031 */
10151032 $('#third_party_extension_button').on('click', async () => {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- });
10311033});
public/scripts/extensions/shared.js+2 -13
@@ -1,5 +1,5 @@
11import { getRequestHeaders } from '../../script.js';
22import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js';
33import { oai_settings } from '../openai.js';
44import { SECRET_KEYS, secret_state } from '../secrets.js';
55import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js';
@@ -202,18 +202,7 @@ export function isWebLlmSupported() {
202202 timeOut: 0,
203203 extendedTimeOut: 0,
204204 preventDuplicates: true,
205205 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- },
217206 });
218207 sessionStorage.setItem(warningKey, '1');
219208 }
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>