2022-12-19 06:30:39 -06:00
|
|
|
/**
|
|
|
|
* This file should only be actually loaded if we are in a trusted environment.
|
|
|
|
*/
|
|
|
|
(async () => {
|
2023-01-16 21:03:43 -06:00
|
|
|
let parentWindow = null;
|
|
|
|
const webui = {
|
|
|
|
/** @type {{name: string, id: string}[]} */
|
|
|
|
destinations: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a
|
|
|
|
*
|
|
|
|
* @param {HTMLCanvas} canvas Canvas to send the data of
|
|
|
|
* @param {string} destination The ID of the destination
|
|
|
|
*/
|
|
|
|
sendTo(canvas, destination) {
|
|
|
|
if (!this.destinations.find((d) => d.id === destination))
|
|
|
|
throw new Error("[webui] Given destination is not available");
|
|
|
|
|
|
|
|
parentWindow &&
|
|
|
|
parentWindow.postMessage({
|
|
|
|
type: "openoutpaint/sendto",
|
|
|
|
message: {
|
|
|
|
image: canvas.toDataURL(),
|
|
|
|
destination,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-12-19 06:30:39 -06:00
|
|
|
// Check if key file exists
|
|
|
|
const response = await fetch("key.json");
|
|
|
|
|
|
|
|
/** @type {{host?: string, trusted?: boolean, key: string}} */
|
|
|
|
let data = null;
|
|
|
|
if (response.status === 200) {
|
|
|
|
data = await response.json();
|
|
|
|
console.info("[webui] key.json loaded successfully");
|
|
|
|
}
|
|
|
|
if (response.status !== 200 || (!data.key && !data.trusted)) {
|
|
|
|
console.warn(
|
|
|
|
"[webui] An accessible key.json file with a 'key' or 'trusted' should be provided to allow for messaging"
|
|
|
|
);
|
|
|
|
console.warn(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = data.key;
|
|
|
|
|
|
|
|
// Check if we are running inside an iframe or embed
|
|
|
|
try {
|
|
|
|
const frame = window.frameElement;
|
|
|
|
|
|
|
|
if (frame === null) {
|
|
|
|
console.info("[webui] Not running inside a frame");
|
|
|
|
} else {
|
|
|
|
console.info(
|
|
|
|
`[webui] Window is child of '${window.parent.document.URL}'`
|
|
|
|
);
|
|
|
|
if (data.host && !window.parent.document.URL.startsWith(data.host)) {
|
|
|
|
console.warn(
|
|
|
|
`[webui] Window does not trust parent '${window.parent.document.URL}'`
|
|
|
|
);
|
|
|
|
console.warn("[webui] Will NOT setup message listeners");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
`[webui] Running in a third party iframe or embed, and blocked by CORS`
|
|
|
|
);
|
|
|
|
console.warn(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
2022-12-19 10:12:10 -06:00
|
|
|
if (!data.trusted) console.debug(`[webui] Loaded key`);
|
2022-12-19 06:30:39 -06:00
|
|
|
|
|
|
|
window.addEventListener("message", ({data, origin, source}) => {
|
|
|
|
if (!data.trusted && data.key !== key) {
|
|
|
|
console.warn(
|
|
|
|
`[webui] Message with incorrect key was received from '${origin}'`
|
|
|
|
);
|
|
|
|
console.warn(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:56:19 -06:00
|
|
|
if (!parentWindow && !data.type === "openoutpaint/init") {
|
2022-12-19 10:12:10 -06:00
|
|
|
console.warn(`[webui] Communication has not been initialized`);
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:03:43 -06:00
|
|
|
if (global.debug) {
|
|
|
|
console.debug("[webui] Received message:");
|
|
|
|
console.debug(data);
|
|
|
|
}
|
|
|
|
|
2022-12-19 06:30:39 -06:00
|
|
|
try {
|
|
|
|
switch (data.type) {
|
2022-12-19 10:12:10 -06:00
|
|
|
case "openoutpaint/init":
|
2022-12-19 06:30:39 -06:00
|
|
|
parentWindow = source;
|
2022-12-19 10:12:10 -06:00
|
|
|
console.debug(
|
|
|
|
`[webui] Communication with '${origin}' has been initialized`
|
|
|
|
);
|
2022-12-19 19:23:24 -06:00
|
|
|
if (data.host)
|
|
|
|
setFixedHost(
|
|
|
|
data.host,
|
2023-02-14 20:44:37 -06:00
|
|
|
`Are you sure you want to modify the host?<br>This configuration was provided by the hosting page:<br> - ${parentWindow.document.title} (${origin})`
|
2022-12-19 19:23:24 -06:00
|
|
|
);
|
2023-01-16 21:03:43 -06:00
|
|
|
if (data.destinations) webui.destinations = data.destinations;
|
2022-12-19 19:23:24 -06:00
|
|
|
|
2022-12-19 06:30:39 -06:00
|
|
|
break;
|
2022-12-19 10:12:10 -06:00
|
|
|
case "openoutpaint/add-resource":
|
2022-12-19 06:30:39 -06:00
|
|
|
{
|
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = data.image.dataURL;
|
2022-12-19 10:12:10 -06:00
|
|
|
image.onload = async () => {
|
|
|
|
await tools.stamp.state.addResource(
|
2022-12-19 06:30:39 -06:00
|
|
|
data.image.resourceName || "External Resource",
|
|
|
|
image
|
|
|
|
);
|
2023-02-14 21:03:16 -06:00
|
|
|
// Fit image on screen if too big
|
|
|
|
const wr = image.width / window.innerWidth;
|
|
|
|
const hr = image.height / window.innerHeight;
|
|
|
|
const mr = Math.max(wr, hr);
|
|
|
|
|
|
|
|
if (mr > viewport.zoom) {
|
|
|
|
viewport.zoom = mr * 1.3;
|
|
|
|
viewport.transform(imageCollection.element);
|
|
|
|
|
|
|
|
toolbar._current_tool.redrawui &&
|
|
|
|
toolbar._current_tool.redrawui();
|
|
|
|
}
|
|
|
|
|
2022-12-19 06:30:39 -06:00
|
|
|
tools.stamp.enable();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
break;
|
2022-12-20 20:26:52 -06:00
|
|
|
case "openoutpaint/set-prompt":
|
|
|
|
{
|
|
|
|
const promptEl = document.getElementById("prompt");
|
|
|
|
const negativePromptEl = document.getElementById("negPrompt");
|
|
|
|
|
|
|
|
if (data.prompt !== undefined) {
|
|
|
|
promptEl.value = data.prompt;
|
|
|
|
stableDiffusionData.prompt = promptEl.value;
|
|
|
|
promptEl.title = promptEl.value;
|
|
|
|
localStorage.setItem(
|
|
|
|
"openoutpaint/prompt",
|
|
|
|
stableDiffusionData.prompt
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.negPrompt !== undefined) {
|
|
|
|
negativePromptEl.value = data.negPrompt;
|
|
|
|
stableDiffusionData.negative_prompt = negativePromptEl.value;
|
|
|
|
negativePromptEl.title = negativePromptEl.value;
|
|
|
|
localStorage.setItem(
|
|
|
|
"openoutpaint/neg_prompt",
|
|
|
|
stableDiffusionData.negative_prompt
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.styles !== undefined) {
|
|
|
|
styleSelectElement.value = data.styles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-12-19 06:30:39 -06:00
|
|
|
default:
|
|
|
|
console.warn(`[webui] Unsupported message type: ${data.type}`);
|
|
|
|
break;
|
|
|
|
}
|
2022-12-19 10:12:10 -06:00
|
|
|
|
|
|
|
// Send acknowledgement
|
|
|
|
parentWindow &&
|
|
|
|
parentWindow.postMessage({
|
2022-12-19 11:56:19 -06:00
|
|
|
type: "openoutpaint/ack",
|
2022-12-19 10:12:10 -06:00
|
|
|
message: data,
|
|
|
|
});
|
2022-12-19 06:30:39 -06:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
`[webui] Message of type '${data.type}' has invalid format`
|
|
|
|
);
|
|
|
|
console.warn(e);
|
|
|
|
console.warn(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-01-16 21:03:43 -06:00
|
|
|
return webui;
|
|
|
|
})().then((value) => {
|
|
|
|
global.webui = value;
|
|
|
|
});
|