diff --git a/.gitignore b/.gitignore index 8d303a4..5984274 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ __pycache__ pysssss.json user/autocomplete.txt +user/url.txt web/js/assets/favicon.user.ico -web/js/assets/favicon-active.user.ico \ No newline at end of file +web/js/assets/favicon-active.user.ico diff --git a/py/autocomplete.py b/py/autocomplete.py index 8ac6a05..7f8e83a 100644 --- a/py/autocomplete.py +++ b/py/autocomplete.py @@ -6,19 +6,34 @@ dir = os.path.abspath(os.path.join(__file__, "../../user")) if not os.path.exists(dir): os.mkdir(dir) -file = os.path.join(dir, "autocomplete.txt") +tag_file = os.path.join(dir, "autocomplete.txt") +url_file = os.path.join(dir, "url.txt") @PromptServer.instance.routes.get("/pysssss/autocomplete") async def get_autocomplete(request): - if os.path.isfile(file): - return web.FileResponse(file) + if os.path.isfile(tag_file): + return web.FileResponse(tag_file) return web.Response(status=404) @PromptServer.instance.routes.post("/pysssss/autocomplete") async def update_autocomplete(request): - with open(file, "w", encoding="utf-8") as f: + with open(tag_file, "w", encoding="utf-8") as f: + f.write(await request.text()) + return web.Response(status=200) + + +@PromptServer.instance.routes.get("/pysssss/cwlUrl") +async def get_url(request): + if os.path.isfile(url_file): + return web.FileResponse(url_file) + return web.Response(status=404) + + +@PromptServer.instance.routes.post("/pysssss/cwlUrl") +async def update_url(request): + with open(url_file, "w", encoding="utf-8") as f: f.write(await request.text()) return web.Response(status=200) diff --git a/web/js/autocompleter.js b/web/js/autocompleter.js index d0fb2ec..177b209 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -85,6 +85,28 @@ async function getCustomWords() { return undefined; } +async function getCWLUrl() { + const resp = await api.fetchApi("/pysssss/cwlUrl", { cache: "no-store" }); + if (resp.status === 200) { + /** @type {string} */ + let url = await resp.text(); + return url; + } + + return "https://gist.githubusercontent.com/pythongosssss/" + + "1d3efa6050356a08cea975183088159a/raw/" + + "a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt"; +} + +/** + * + * @param {string} url + */ +async function saveCWLUrl(url) { + const resp = await api.fetchApi("/pysssss/cwlUrl", { method: "POST", body: url }); + return resp.status === 200; +} + async function addCustomWords(text) { if (!text) { text = await getCustomWords(); @@ -177,12 +199,13 @@ class CustomWordsDialog extends ComfyDialog { }, }); - const input = $el("input", { + this.cwlUrl = await getCWLUrl(); + + this.input = $el("input", { style: { flex: "auto", }, - value: - "https://gist.githubusercontent.com/pythongosssss/1d3efa6050356a08cea975183088159a/raw/a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt", + value: this.cwlUrl, }); super.show( @@ -219,17 +242,22 @@ class CustomWordsDialog extends ComfyDialog { }, [ $el("label", { textContent: "Load Custom List: " }), - input, + this.input, $el("button", { textContent: "Load", onclick: async () => { try { - const res = await fetch(input.value); + this.cwlUrl = this.input.value; + if (!await saveCWLUrl(this.cwlUrl)) { + throw new Error("Error saving URL!"); + } + const res = await fetch(this.cwlUrl); if (res.status !== 200) { throw new Error("Error loading: " + res.status + " " + res.statusText); } this.words.value = await res.text(); } catch (error) { + console.error(error); alert("Error loading custom list, try manually copy + pasting the list"); } }, @@ -249,6 +277,10 @@ class CustomWordsDialog extends ComfyDialog { textContent: "Save", onclick: async (e) => { try { + this.cwlUrl = this.input.value; + if (!await saveCWLUrl(this.cwlUrl)) { + throw new Error("Error saving URL!"); + } const res = await api.fetchApi("/pysssss/autocomplete", { method: "POST", body: this.words.value }); if (res.status !== 200) { throw new Error("Error saving: " + res.status + " " + res.statusText); @@ -259,7 +291,6 @@ class CustomWordsDialog extends ComfyDialog { save.textContent = "Save"; }, 500); } catch (error) { - alert("Error saving word list!"); console.error(error); } }, @@ -494,9 +525,9 @@ app.registerExtension({ onclick: () => { try { // Try closing old settings window - if (typeof app.ui.settings.element?.close === "function") { + if (typeof app.ui.settings.element?.close === "function") { app.ui.settings.element.close(); - } + } } catch (error) { } try { @@ -506,7 +537,7 @@ app.registerExtension({ // Fallback to just hiding the element app.ui.settings.element.style.display = "none"; } - + new CustomWordsDialog().show(); }, style: { @@ -549,7 +580,7 @@ app.registerExtension({ let loras; try { loras = LiteGraph.registered_node_types["LoraLoader"]?.nodeData.input.required.lora_name[0]; - } catch (error) {} + } catch (error) { } if (!loras?.length) { loras = await api.fetchApi("/pysssss/loras", { cache: "no-store" }).then((res) => res.json());