Download javascript/text_fix.js from Kazzze/sd-text-fix: direct link, hf CLI and curl.
- Browser
- Download file 11.3 kB
-
https://huggingface.co/Kazzze/sd-text-fix/resolve/main/javascript/text_fix.js
- Command line
-
hf download hf://Kazzze/sd-text-fix/javascript/text_fix.js
-
curl -L -o text_fix.js https://huggingface.co/Kazzze/sd-text-fix/resolve/main/javascript/text_fix.js
11.3 kB
| // Host side of Text Fix: opens the editor (a React page in an iframe) over the page and hands | |
| // it the image the gallery shows. Two ways in: an icon in the row of tools under Generate, and | |
| // a button in the row of buttons under the gallery. Host JS: no arrow functions, no template literals. | |
| (function () { | |
| var PAGE = '/text_fix/ui'; | |
| var overlay = null; | |
| var frame = null; | |
| var ready = false; | |
| var tab = 'txt2img'; | |
| function app() { | |
| return (typeof gradioApp === 'function') ? gradioApp() : document; | |
| } | |
| // The image the gallery shows now. | |
| function galleryImage() { | |
| var gallery = app().querySelector('#' + tab + '_gallery'); | |
| if (!gallery) { | |
| return null; | |
| } | |
| var thumbs = gallery.querySelectorAll('.grid-wrap .thumbnail-item img'); | |
| if (!thumbs.length) { | |
| thumbs = gallery.querySelectorAll('.thumbnails .thumbnail-item img'); | |
| } | |
| var index = (typeof selected_gallery_index === 'function') ? selected_gallery_index() : 0; | |
| if (index < 0) { | |
| index = 0; | |
| } | |
| var shown = gallery.querySelector('.preview > img, img[data-testid="detailed-image"]'); | |
| var picked = shown || thumbs[index] || gallery.querySelector('img'); | |
| if (!picked || !picked.src || picked.src.indexOf('data:') === 0) { | |
| return null; | |
| } | |
| var name = decodeURIComponent(picked.src.split('/').pop().split('?')[0] || 'image.png'); | |
| return {url: picked.src, name: name}; | |
| } | |
| function theme() { | |
| var dark = document.body.classList.contains('dark') || app().querySelector('.dark') !== null; | |
| return dark ? 'dark' : 'light'; | |
| } | |
| function send() { | |
| if (!frame || !ready || !frame.contentWindow) { | |
| return; | |
| } | |
| var message = {type: 'TF_INIT', theme: theme(), tab: tab}; | |
| var image = galleryImage(); | |
| if (image) { | |
| message.image = image; | |
| } | |
| frame.contentWindow.postMessage(message, window.location.origin); | |
| } | |
| function close() { | |
| if (overlay) { | |
| overlay.style.display = 'none'; | |
| } | |
| } | |
| function build() { | |
| overlay = document.createElement('div'); | |
| overlay.id = 'text_fix_overlay'; | |
| overlay.style.cssText = 'position:fixed;inset:0;z-index:10000;background:rgba(0,0,0,0.6);display:none;'; | |
| frame = document.createElement('iframe'); | |
| frame.title = 'Text Fix'; | |
| frame.style.cssText = 'position:absolute;inset:12px;width:calc(100% - 24px);height:calc(100% - 24px);' + | |
| 'border:1px solid rgba(128,128,128,0.4);border-radius:10px;background:#111317;'; | |
| overlay.appendChild(frame); | |
| document.body.appendChild(overlay); | |
| } | |
| window.addEventListener('message', function (event) { | |
| if (!frame || event.source !== frame.contentWindow || event.origin !== window.location.origin) { | |
| return; | |
| } | |
| var data = event.data || {}; | |
| if (data.type === 'TF_READY') { | |
| ready = true; | |
| send(); | |
| } else if (data.type === 'TF_CLOSE') { | |
| close(); | |
| } else if (data.type === 'TF_TO_GALLERY' && typeof data.session === 'string') { | |
| toGallery(data.session); | |
| close(); | |
| } else if (data.type === 'TF_SEND' && typeof data.session === 'string' && | |
| (data.target === 'img2img' || data.target === 'inpaint')) { | |
| // The result goes into the gallery first; the host's own button under the gallery | |
| // then carries it, with the generation parameters, to the img2img or inpaint tab. | |
| var before = galleryImage(); | |
| var from = tab; | |
| toGallery(data.session); | |
| close(); | |
| afterGalleryChanges(before ? before.url : '', function () { | |
| var send = app().querySelector('#' + from + '_send_to_' + data.target); | |
| if (send) { | |
| send.click(); | |
| } | |
| }); | |
| } | |
| }); | |
| // The hidden Gradio button reads the session id from here and fills the gallery. | |
| function toGallery(session) { | |
| window.textFixSession = session; | |
| var button = app().querySelector('#text_fix_to_gallery_' + tab); | |
| if (button) { | |
| button.click(); | |
| } | |
| } | |
| // Run `then` once the gallery shows another image than `old`, or after a few seconds. | |
| function afterGalleryChanges(old, then) { | |
| var waited = 0; | |
| var timer = setInterval(function () { | |
| waited += 200; | |
| var now = galleryImage(); | |
| if ((now && now.url !== old) || waited >= 8000) { | |
| clearInterval(timer); | |
| setTimeout(then, 150); | |
| } | |
| }, 200); | |
| } | |
| document.addEventListener('keydown', function (event) { | |
| if (event.key === 'Escape' && overlay && overlay.style.display !== 'none') { | |
| close(); | |
| } | |
| }); | |
| // Each opening loads the page afresh: it starts clean, with the image the gallery shows now. | |
| window.textFixOpen = function (which) { | |
| tab = which || 'txt2img'; | |
| if (!overlay) { | |
| build(); | |
| } | |
| ready = false; | |
| frame.src = PAGE + '?t=' + Date.now(); | |
| overlay.style.display = 'block'; | |
| }; | |
| var NS = 'http://www.w3.org/2000/svg'; | |
| function icon() { | |
| var svg = document.createElementNS(NS, 'svg'); | |
| svg.setAttribute('viewBox', '0 0 24 24'); | |
| svg.setAttribute('fill', 'none'); | |
| svg.setAttribute('stroke', 'currentColor'); | |
| svg.setAttribute('stroke-width', '1.8'); | |
| svg.setAttribute('stroke-linecap', 'round'); | |
| svg.setAttribute('stroke-linejoin', 'round'); | |
| svg.setAttribute('aria-hidden', 'true'); | |
| // A capital A with a tick beside it: text, set right. | |
| var parts = ['M3.5 18L9 4.5l5.5 13.5', 'M5.4 13.5h7.2', 'M14.5 19l2.5 2.5 4.5-5.5']; | |
| for (var i = 0; i < parts.length; i++) { | |
| var path = document.createElementNS(NS, 'path'); | |
| path.setAttribute('d', parts[i]); | |
| svg.appendChild(path); | |
| } | |
| return svg; | |
| } | |
| function opener(id, which, title, content) { | |
| var button = document.createElement('button'); | |
| button.type = 'button'; | |
| button.id = id; | |
| button.className = 'textfix-tool-btn lg secondary gradio-button tool'; | |
| button.title = title; | |
| button.setAttribute('aria-label', title); | |
| if (typeof content === 'string') { | |
| button.textContent = content; | |
| } else { | |
| button.appendChild(content); | |
| } | |
| button.addEventListener('click', function (event) { | |
| event.preventDefault(); | |
| event.stopPropagation(); | |
| window.textFixOpen(which); | |
| }); | |
| return button; | |
| } | |
| // A button takes the look of a tool button the host itself made, whatever the theme: | |
| // its classes, and the sizes and colors the browser worked out for it. | |
| var LOOK = ['backgroundColor', 'backgroundImage', 'borderTopColor', 'borderRightColor', 'borderBottomColor', | |
| 'borderLeftColor', 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', | |
| 'borderTopStyle', 'borderRightStyle', 'borderBottomStyle', 'borderLeftStyle', 'borderTopLeftRadius', | |
| 'borderTopRightRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius', 'boxShadow', 'color', | |
| 'width', 'height', 'minWidth', 'maxWidth', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', | |
| 'marginTop', 'marginRight', 'marginBottom', 'marginLeft', 'flexGrow', 'flexShrink', 'flexBasis', 'alignSelf', | |
| 'fontSize', 'lineHeight']; | |
| function ownedByOthers(id) { | |
| return id.indexOf('sg_trigger_') === 0 || id.indexOf('comic_lettering_tool_') === 0 || id.indexOf('text_fix_') === 0; | |
| } | |
| function dress(button, row) { | |
| var others = row.querySelectorAll('button'); | |
| var model = null; | |
| for (var i = 0; i < others.length; i++) { | |
| if (others[i] !== button && !ownedByOthers(others[i].id || '') && others[i].offsetWidth) { | |
| model = others[i]; | |
| break; | |
| } | |
| } | |
| if (!model) { | |
| return; | |
| } | |
| var classes = model.className.split(/\s+/).filter(function (name) { | |
| return name && name !== 'hidden'; | |
| }); | |
| button.className = 'textfix-tool-btn ' + classes.join(' '); | |
| var seen = window.getComputedStyle(model); | |
| for (var k = 0; k < LOOK.length; k++) { | |
| button.style[LOOK[k]] = seen[LOOK[k]]; | |
| } | |
| button.style.display = 'inline-flex'; | |
| button.style.alignItems = 'center'; | |
| button.style.justifyContent = 'center'; | |
| } | |
| // The icon in the row of tools under Generate: left of Comic Lettering's when that is | |
| // installed; without it, left of Style Grid's; else the last in the row. | |
| function placeTool(which) { | |
| var root = app(); | |
| var tools = root.querySelector('#' + which + '_tools'); | |
| if (!tools) { | |
| return; | |
| } | |
| var row = tools.querySelector(':scope > div.form, :scope > div[style*="flex"]') || tools; | |
| var button = root.querySelector('#text_fix_tool_' + which) || | |
| opener('text_fix_tool_' + which, which, 'Fix the text on this image (Text Fix)', icon()); | |
| var neighbour = root.querySelector('#comic_lettering_tool_' + which) || | |
| root.querySelector('#sg_trigger_' + which); | |
| if (neighbour && neighbour.parentNode) { | |
| if (neighbour.previousSibling !== button) { | |
| neighbour.parentNode.insertBefore(button, neighbour); | |
| } | |
| } else if (button.parentNode !== row) { | |
| row.appendChild(button); | |
| } | |
| dress(button, button.parentNode || row); | |
| } | |
| // The button under the gallery: after the host's own "send to extras". | |
| function placeGalleryButton(which) { | |
| var root = app(); | |
| var extras = root.querySelector('#' + which + '_send_to_extras'); | |
| if (!extras || !extras.parentNode) { | |
| return; | |
| } | |
| var button = root.querySelector('#text_fix_gallery_' + which) || | |
| opener('text_fix_gallery_' + which, which, 'Fix the text on this image (Text Fix)', '🔤'); | |
| if (extras.nextSibling !== button) { | |
| extras.parentNode.insertBefore(button, extras.nextSibling); | |
| } | |
| dress(button, extras.parentNode); | |
| } | |
| // The rows appear some time after the page script runs, and other extensions add their | |
| // buttons later still: look for a while, then stop. | |
| var tries = 0; | |
| var timer = setInterval(function () { | |
| tries += 1; | |
| placeTool('txt2img'); | |
| placeTool('img2img'); | |
| placeGalleryButton('txt2img'); | |
| placeGalleryButton('img2img'); | |
| if (tries > 120) { | |
| clearInterval(timer); | |
| } | |
| }, 500); | |
| })(); | |