Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
FlameWolf / ui-config.json
Last active June 7, 2023 13:40
AUTOMATIC1111 Web UI Configuration
{
"txt2img/Prompt/visible": true,
"txt2img/Prompt/value": "",
"txt2img/Negative prompt/visible": true,
"txt2img/Negative prompt/value": "poorly drawn, disfigured, deformed, cross-eyed, bad anatomy, bad proportions, mutated, extra limbs, extra legs, extra arms, missing limb, missing leg, missing arm, malformed limbs, asymmetrical, close-up, blurred, bad art, ugly, jewellery, hands, fingers",
"txt2img/Styles/visible": false,
"txt2img/Styles/value": [],
"txt2img/Sampling method/visible": true,
"txt2img/Sampling method/value": "DPM++ 2M",
"txt2img/Sampling steps/visible": true,
@FlameWolf
FlameWolf / getMaxZIndex.js
Created September 22, 2022 06:53
Find the largest z-index in the document
const getMaxZIndex = function () {
const elements = [...document.querySelectorAll("body *")];
return Math.max(...elements.map(x => parseInt(getComputedStyle(x, null).zIndex) || 0));
};
@FlameWolf
FlameWolf / setImmediate.js
Last active September 1, 2022 16:28
setImmediate polyfill
globalThis.setImmediate = globalThis.setImmediate || function (callback) {
return setTimeout(callback, 0, ...Array.prototype.slice.apply(arguments, [1]));
};
@FlameWolf
FlameWolf / express-cors.js
Created July 18, 2022 15:47
Configure CORS in Express using middleware
const app = express();
app.use(async (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", process.env.ALLOW_ORIGIN);
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Slug, X-UID");
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, POST, PUT, PATCH, GET, DELETE");
next();
});
@FlameWolf
FlameWolf / fastify-cors.js
Created July 18, 2022 15:45
Configure CORS in Fastify using onRequest hook
const server = fastify();
server.addHook("onRequest", async (request, reply) => {
reply.header("Access-Control-Allow-Origin", process.env.ALLOW_ORIGIN);
reply.header("Access-Control-Allow-Credentials", true);
reply.header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Slug, X-UID");
reply.header("Access-Control-Allow-Methods", "OPTIONS, POST, PUT, PATCH, GET, DELETE");
if (request.method === "OPTIONS") {
reply.send();
}
});
@FlameWolf
FlameWolf / String.prototype.format.js
Last active November 22, 2019 08:42
String.prototype.format
Object.defineProperty(String.prototype, "format", {
"value": function(...args) {
const replacer = function(match, index) {
return (args[parseInt(index)] || "");
};
return this.replace(/\${(\d*?)\}/g, replacer);
}
});
/***********************************************************************************************************/
/********************************************** Usage **********************************************/
@FlameWolf
FlameWolf / adding-immutable-functions-to-existing-classes.md
Last active October 15, 2018 11:23
Adding immutable functions to existing classes

Let's say you want to add a method to all the elements in your HTML document. Normally, to do this, you will use Element.prototype as below:

Element.prototype.foo = function() {
	return "foo";
};

Let's say now you want to include a library called bar.js in your document. But unbeknownst to to you, there is a piece of code in bar.js that overwrites the foo() method of Element.prototype as below:

The power of Object.assign

Let's say you have an array like this:

let y = [{"a": { "id": 1 } }, { "b": { "id": 2 } }, { "c": { "id": 3 } }];

You can see that this is a very inefficient way to store data. It would be so much better to have these three distinct JSON objects as properties of a single JSON object like this:

{ "a": { "id": 1 }, "b": { "id": 2 }, "c": { "id": 3 } }

@FlameWolf
FlameWolf / Performance-difference-between-insertAdjacentHTML-and-appendChild.md
Last active November 22, 2024 04:59
Performance difference between insertAdjacentHTML and appendChild

Element.prototype.insertAdjacentHTML can be up to 4 times slower than Node.prototype.appendChild. So use the latter wherever possible for best performance. (See comment from @frankbakulov below!)

Element.prototype.replaceWith = function(html) { this.insertAdjacentHTML("afterend", html); this.remove(); };