From 71f3c79a6bfde29a249ec02b5fbde8fae1e629b5 Mon Sep 17 00:00:00 2001 From: Endericedragon Date: Fri, 25 Apr 2025 02:51:01 +0800 Subject: [PATCH 1/5] Now the custom word list url would be saved in user/url.txt. The value would be saved when clicking "Save" or "Load" button of CustomWordsDialog. The value would be loaded when showing the dialog. --- .gitignore | 4 ++- py/autocomplete.py | 23 ++++++++++++++--- web/js/autocompleter.js | 55 +++++++++++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 8d303a4..04f22a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ __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 +log.txt 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..58ebc45 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -85,6 +85,32 @@ 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"; +} + +async function insertCWLUrlTo(self) { + this.cwlUrl = await getCWLUrl(); +} + +/** + * + * @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 +203,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 +246,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 +281,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 +295,6 @@ class CustomWordsDialog extends ComfyDialog { save.textContent = "Save"; }, 500); } catch (error) { - alert("Error saving word list!"); console.error(error); } }, @@ -494,9 +529,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 +541,7 @@ app.registerExtension({ // Fallback to just hiding the element app.ui.settings.element.style.display = "none"; } - + new CustomWordsDialog().show(); }, style: { @@ -549,7 +584,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()); From 1dd0c80d9a927c0502014df96d129297510a3daf Mon Sep 17 00:00:00 2001 From: Endericedragon Date: Fri, 25 Apr 2025 14:18:37 +0800 Subject: [PATCH 2/5] Removed unnecessary codes. --- .gitignore | 1 - web/js/autocompleter.js | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 04f22a5..5984274 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ user/autocomplete.txt user/url.txt web/js/assets/favicon.user.ico web/js/assets/favicon-active.user.ico -log.txt diff --git a/web/js/autocompleter.js b/web/js/autocompleter.js index 58ebc45..177b209 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -91,17 +91,13 @@ async function getCWLUrl() { /** @type {string} */ let url = await resp.text(); return url; - } + return "https://gist.githubusercontent.com/pythongosssss/" + "1d3efa6050356a08cea975183088159a/raw/" + "a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt"; } -async function insertCWLUrlTo(self) { - this.cwlUrl = await getCWLUrl(); -} - /** * * @param {string} url From be9faf0cdebcc5ab4d93dcc46fcd9f0f448d059e Mon Sep 17 00:00:00 2001 From: Endericedragon Fu Date: Wed, 29 Apr 2026 11:56:22 +0800 Subject: [PATCH 3/5] Use python backend to get word list instead of frontend to avoid cross origin problem. --- py/autocomplete.py | 13 ++++++++++++- web/js/autocompleter.js | 24 ++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/py/autocomplete.py b/py/autocomplete.py index 7f8e83a..798efda 100644 --- a/py/autocomplete.py +++ b/py/autocomplete.py @@ -1,5 +1,5 @@ from server import PromptServer -from aiohttp import web +from aiohttp import web, ClientSession import os import folder_paths @@ -42,3 +42,14 @@ async def update_url(request): async def get_loras(request): loras = folder_paths.get_filename_list("loras") return web.json_response(list(map(lambda a: os.path.splitext(a)[0], loras))) + + +@PromptServer.instance.routes.post("/pysssss/getWordList") +async def get_word_list(request): + with open(url_file, "r", encoding="utf-8") as f: + url = f.read().strip() + async with ClientSession() as session: + async with session.get(url) as resp: + text = await resp.text() + return web.Response(text=text, status=200) + return web.Response(status=500) diff --git a/web/js/autocompleter.js b/web/js/autocompleter.js index a2f18d0..59d605f 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -85,6 +85,10 @@ async function getCustomWords() { return undefined; } +/** + * 获取自定义词的URL + * @returns {string} + */ async function getCWLUrl() { const resp = await api.fetchApi("/pysssss/cwlUrl", { cache: "no-store" }); if (resp.status === 200) { @@ -92,14 +96,14 @@ async function getCWLUrl() { let url = await resp.text(); return url; } - + return "https://gist.githubusercontent.com/pythongosssss/" + "1d3efa6050356a08cea975183088159a/raw/" + "a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt"; } /** - * + * 将自定义词的URL保存到服务器 * @param {string} url */ async function saveCWLUrl(url) { @@ -251,10 +255,18 @@ class CustomWordsDialog extends ComfyDialog { 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); - } + const res = await api.fetchApi("/pysssss/getWordList", { + method: "POST", cache: "no-store" + }); + // const res = await fetch(this.cwlUrl, { + // method: "GET", + // headers: { + // "Access-Control-Allow-Origin": "*", + // } + // }); + // if (res.status !== 200) { + // throw new Error("Error loading: " + res.status + " " + res.statusText); + // } this.words.value = await res.text(); } catch (error) { console.error(error); From b6d0e35512a9c9d63eef868c51914750173a8e83 Mon Sep 17 00:00:00 2001 From: Endericedragon Fu Date: Wed, 29 Apr 2026 15:35:42 +0800 Subject: [PATCH 4/5] Now custom words could be obtained through http proxies --- py/autocomplete.py | 14 +++++++++----- web/js/autocompleter.js | 21 ++++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/py/autocomplete.py b/py/autocomplete.py index 798efda..16057b0 100644 --- a/py/autocomplete.py +++ b/py/autocomplete.py @@ -46,10 +46,14 @@ async def get_loras(request): @PromptServer.instance.routes.post("/pysssss/getWordList") async def get_word_list(request): + proxies = await request.text() + if proxies: + session = ClientSession(proxy=proxies) + else: + session = ClientSession() with open(url_file, "r", encoding="utf-8") as f: url = f.read().strip() - async with ClientSession() as session: - async with session.get(url) as resp: - text = await resp.text() - return web.Response(text=text, status=200) - return web.Response(status=500) + async with session.get(url) as resp: + text = await resp.text() + await session.close() + return web.Response(text=text, status=200) diff --git a/web/js/autocompleter.js b/web/js/autocompleter.js index 59d605f..bba3559 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -6,6 +6,10 @@ import { TextAreaAutoComplete } from "./common/autocomplete.js"; import { ModelInfoDialog } from "./common/modelInfoDialog.js"; import { LoraInfoDialog } from "./modelInfo.js"; +const DEFAULT_CWL_URL = "https://gist.githubusercontent.com/pythongosssss/" + + "1d3efa6050356a08cea975183088159a/raw/" + + "a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt"; + function parseCSV(csvText) { const rows = []; const delimiter = ","; @@ -97,9 +101,7 @@ async function getCWLUrl() { return url; } - return "https://gist.githubusercontent.com/pythongosssss/" + - "1d3efa6050356a08cea975183088159a/raw/" + - "a18fb2f94f9156cf4476b0c24a09544d6c0baec6/danbooru-tags.txt"; + return DEFAULT_CWL_URL; } /** @@ -251,12 +253,12 @@ class CustomWordsDialog extends ComfyDialog { textContent: "Load", onclick: async () => { try { - this.cwlUrl = this.input.value; + this.cwlUrl = this.input.value || DEFAULT_CWL_URL; if (!await saveCWLUrl(this.cwlUrl)) { throw new Error("Error saving URL!"); } const res = await api.fetchApi("/pysssss/getWordList", { - method: "POST", cache: "no-store" + method: "POST", cache: "no-store", body: app.extensionManager.setting.get(`${id}.Proxies`) }); // const res = await fetch(this.cwlUrl, { // method: "GET", @@ -317,6 +319,15 @@ const id = "pysssss.AutoCompleter"; app.registerExtension({ name: id, + settings: [ + { + id: `${id}.Proxies`, + name: "Proxies", + type: "text", + defaultValue: "", + tooltip: "http://:" + } + ], init() { const STRING = ComfyWidgets.STRING; const SKIP_WIDGETS = new Set(["ttN xyPlot.x_values", "ttN xyPlot.y_values"]); From 7bbeb1fb78864d5ec79784bd012f51c4949314e8 Mon Sep 17 00:00:00 2001 From: Endericedragon Fu Date: Sat, 8 Aug 2026 09:26:10 +0800 Subject: [PATCH 5/5] =?UTF-8?q?1.=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=9B2.=E7=96=91=E4=BC=BC=E7=BC=93?= =?UTF-8?q?=E8=A7=A3=E4=B8=8B=E6=8B=89=E6=A1=86=E9=81=AE=E4=BD=8F=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=96=87=E6=9C=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/js/autocompleter.js | 9 ----- web/js/common/autocomplete.js | 76 ++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 46 deletions(-) diff --git a/web/js/autocompleter.js b/web/js/autocompleter.js index bba3559..cd1c887 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -260,15 +260,6 @@ class CustomWordsDialog extends ComfyDialog { const res = await api.fetchApi("/pysssss/getWordList", { method: "POST", cache: "no-store", body: app.extensionManager.setting.get(`${id}.Proxies`) }); - // const res = await fetch(this.cwlUrl, { - // method: "GET", - // headers: { - // "Access-Control-Allow-Origin": "*", - // } - // }); - // if (res.status !== 200) { - // throw new Error("Error loading: " + res.status + " " + res.statusText); - // } this.words.value = await res.text(); } catch (error) { console.error(error); diff --git a/web/js/common/autocomplete.js b/web/js/common/autocomplete.js index ac5fb1d..a91b1cf 100644 --- a/web/js/common/autocomplete.js +++ b/web/js/common/autocomplete.js @@ -4,16 +4,16 @@ import { addStylesheet } from "./utils.js"; addStylesheet(import.meta.url); /* - https://github.com/component/textarea-caret-position - The MIT License (MIT) + https://github.com/component/textarea-caret-position + The MIT License (MIT) - Copyright (c) 2015 Jonathan Ong me@jongleberry.com + Copyright (c) 2015 Jonathan Ong me@jongleberry.com - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ const getCaretCoordinates = (function () { // We'll copy the properties below into the mirror div. @@ -140,10 +140,12 @@ const getCaretCoordinates = (function () { span.textContent = element.value.substring(position) || "."; // || because a completely empty faux span doesn't render at all div.appendChild(span); + const lineHeight = Math.ceil(parseFloat(computed["lineHeight"])); + var coordinates = { top: span.offsetTop + parseInt(computed["borderTopWidth"]), left: span.offsetLeft + parseInt(computed["borderLeftWidth"]), - height: parseInt(computed["lineHeight"]), + height: lineHeight, }; if (debug) { @@ -157,19 +159,19 @@ const getCaretCoordinates = (function () { })(); /* - Key functions from: - https://github.com/yuku/textcomplete - © Yuku Takahashi - This software is licensed under the MIT license. + Key functions from: + https://github.com/yuku/textcomplete + © Yuku Takahashi - This software is licensed under the MIT license. - The MIT License (MIT) + The MIT License (MIT) - Copyright (c) 2015 Jonathan Ong me@jongleberry.com + Copyright (c) 2015 Jonathan Ong me@jongleberry.com - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ const CHAR_CODE_ZERO = "0".charCodeAt(0); const CHAR_CODE_NINE = "9".charCodeAt(0); @@ -613,29 +615,29 @@ export class TextAreaAutoComplete { const item = $el( "div.pysssss-autocomplete-item", { - onclick: () => { - this.el.focus(); - let value = wordInfo.value ?? wordInfo.text; - const use_replacer = wordInfo.use_replacer ?? true; - if (TextAreaAutoComplete.replacer && use_replacer) { - value = TextAreaAutoComplete.replacer(value); - } - value = this.#escapeParentheses(value); - - const afterCursor = this.helper.getAfterCursor(); - const shouldAddSeparator = !afterCursor.trim().startsWith(this.separator.trim()); - this.helper.insertAtCursor( - value + (shouldAddSeparator ? this.separator : ''), - -before.length, - wordInfo.caretOffset - ); - setTimeout(() => { - this.#update(); - }, 150); - }, + onclick: () => { + this.el.focus(); + let value = wordInfo.value ?? wordInfo.text; + const use_replacer = wordInfo.use_replacer ?? true; + if (TextAreaAutoComplete.replacer && use_replacer) { + value = TextAreaAutoComplete.replacer(value); + } + value = this.#escapeParentheses(value); + + const afterCursor = this.helper.getAfterCursor(); + const shouldAddSeparator = !afterCursor.trim().startsWith(this.separator.trim()); + this.helper.insertAtCursor( + value + (shouldAddSeparator ? this.separator : ''), + -before.length, + wordInfo.caretOffset + ); + setTimeout(() => { + this.#update(); + }, 150); + }, }, parts - ); + ); if (wordInfo === this.selected) { hasSelected = true; @@ -662,7 +664,7 @@ export class TextAreaAutoComplete { #escapeParentheses(text) { return text.replace(/\(/g, '\\(').replace(/\)/g, '\\)'); - } + } #hide() { this.selected = null;