Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/script.js
3048 views
1
function gradioApp() {
2
const elems = document.getElementsByTagName('gradio-app');
3
const elem = elems.length == 0 ? document : elems[0];
4
5
if (elem !== document) {
6
elem.getElementById = function(id) {
7
return document.getElementById(id);
8
};
9
}
10
return elem.shadowRoot ? elem.shadowRoot : elem;
11
}
12
13
/**
14
* Get the currently selected top-level UI tab button (e.g. the button that says "Extras").
15
*/
16
function get_uiCurrentTab() {
17
return gradioApp().querySelector('#tabs > .tab-nav > button.selected');
18
}
19
20
/**
21
* Get the first currently visible top-level UI tab content (e.g. the div hosting the "txt2img" UI).
22
*/
23
function get_uiCurrentTabContent() {
24
return gradioApp().querySelector('#tabs > .tabitem[id^=tab_]:not([style*="display: none"])');
25
}
26
27
var uiUpdateCallbacks = [];
28
var uiAfterUpdateCallbacks = [];
29
var uiLoadedCallbacks = [];
30
var uiTabChangeCallbacks = [];
31
var optionsChangedCallbacks = [];
32
var optionsAvailableCallbacks = [];
33
var uiAfterUpdateTimeout = null;
34
var uiCurrentTab = null;
35
36
/**
37
* Register callback to be called at each UI update.
38
* The callback receives an array of MutationRecords as an argument.
39
*/
40
function onUiUpdate(callback) {
41
uiUpdateCallbacks.push(callback);
42
}
43
44
/**
45
* Register callback to be called soon after UI updates.
46
* The callback receives no arguments.
47
*
48
* This is preferred over `onUiUpdate` if you don't need
49
* access to the MutationRecords, as your function will
50
* not be called quite as often.
51
*/
52
function onAfterUiUpdate(callback) {
53
uiAfterUpdateCallbacks.push(callback);
54
}
55
56
/**
57
* Register callback to be called when the UI is loaded.
58
* The callback receives no arguments.
59
*/
60
function onUiLoaded(callback) {
61
uiLoadedCallbacks.push(callback);
62
}
63
64
/**
65
* Register callback to be called when the UI tab is changed.
66
* The callback receives no arguments.
67
*/
68
function onUiTabChange(callback) {
69
uiTabChangeCallbacks.push(callback);
70
}
71
72
/**
73
* Register callback to be called when the options are changed.
74
* The callback receives no arguments.
75
* @param callback
76
*/
77
function onOptionsChanged(callback) {
78
optionsChangedCallbacks.push(callback);
79
}
80
81
/**
82
* Register callback to be called when the options (in opts global variable) are available.
83
* The callback receives no arguments.
84
* If you register the callback after the options are available, it's just immediately called.
85
*/
86
function onOptionsAvailable(callback) {
87
if (Object.keys(opts).length != 0) {
88
callback();
89
return;
90
}
91
92
optionsAvailableCallbacks.push(callback);
93
}
94
95
function executeCallbacks(queue, arg) {
96
for (const callback of queue) {
97
try {
98
callback(arg);
99
} catch (e) {
100
console.error("error running callback", callback, ":", e);
101
}
102
}
103
}
104
105
/**
106
* Schedule the execution of the callbacks registered with onAfterUiUpdate.
107
* The callbacks are executed after a short while, unless another call to this function
108
* is made before that time. IOW, the callbacks are executed only once, even
109
* when there are multiple mutations observed.
110
*/
111
function scheduleAfterUiUpdateCallbacks() {
112
clearTimeout(uiAfterUpdateTimeout);
113
uiAfterUpdateTimeout = setTimeout(function() {
114
executeCallbacks(uiAfterUpdateCallbacks);
115
}, 200);
116
}
117
118
var executedOnLoaded = false;
119
120
document.addEventListener("DOMContentLoaded", function() {
121
var mutationObserver = new MutationObserver(function(m) {
122
if (!executedOnLoaded && gradioApp().querySelector('#txt2img_prompt')) {
123
executedOnLoaded = true;
124
executeCallbacks(uiLoadedCallbacks);
125
}
126
127
executeCallbacks(uiUpdateCallbacks, m);
128
scheduleAfterUiUpdateCallbacks();
129
const newTab = get_uiCurrentTab();
130
if (newTab && (newTab !== uiCurrentTab)) {
131
uiCurrentTab = newTab;
132
executeCallbacks(uiTabChangeCallbacks);
133
}
134
});
135
mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
136
});
137
138
/**
139
* Add keyboard shortcuts:
140
* Ctrl+Enter to start/restart a generation
141
* Alt/Option+Enter to skip a generation
142
* Esc to interrupt a generation
143
*/
144
document.addEventListener('keydown', function(e) {
145
const isEnter = e.key === 'Enter' || e.keyCode === 13;
146
const isCtrlKey = e.metaKey || e.ctrlKey;
147
const isAltKey = e.altKey;
148
const isEsc = e.key === 'Escape';
149
150
const generateButton = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
151
const interruptButton = get_uiCurrentTabContent().querySelector('button[id$=_interrupt]');
152
const skipButton = get_uiCurrentTabContent().querySelector('button[id$=_skip]');
153
154
if (isCtrlKey && isEnter) {
155
if (interruptButton.style.display === 'block') {
156
interruptButton.click();
157
const callback = (mutationList) => {
158
for (const mutation of mutationList) {
159
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
160
if (interruptButton.style.display === 'none') {
161
generateButton.click();
162
observer.disconnect();
163
}
164
}
165
}
166
};
167
const observer = new MutationObserver(callback);
168
observer.observe(interruptButton, {attributes: true});
169
} else {
170
generateButton.click();
171
}
172
e.preventDefault();
173
}
174
175
if (isAltKey && isEnter) {
176
skipButton.click();
177
e.preventDefault();
178
}
179
180
if (isEsc) {
181
const globalPopup = document.querySelector('.global-popup');
182
const lightboxModal = document.querySelector('#lightboxModal');
183
if (!globalPopup || globalPopup.style.display === 'none') {
184
if (document.activeElement === lightboxModal) return;
185
if (interruptButton.style.display === 'block') {
186
interruptButton.click();
187
e.preventDefault();
188
}
189
}
190
}
191
});
192
193
/**
194
* checks that a UI element is not in another hidden element or tab content
195
*/
196
function uiElementIsVisible(el) {
197
if (el === document) {
198
return true;
199
}
200
201
const computedStyle = getComputedStyle(el);
202
const isVisible = computedStyle.display !== 'none';
203
204
if (!isVisible) return false;
205
return uiElementIsVisible(el.parentNode);
206
}
207
208
function uiElementInSight(el) {
209
const clRect = el.getBoundingClientRect();
210
const windowHeight = window.innerHeight;
211
const isOnScreen = clRect.bottom > 0 && clRect.top < windowHeight;
212
213
return isOnScreen;
214
}
215
216