Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
FlameWolf / PressAndRelease.js
Last active May 31, 2023 12:25
Global mouse hold event
(function () {
let isPressed = false;
let pressTimer = false;
const pressedEvent = new CustomEvent("press", {
bubbles: true,
cancelable: true,
composed: true
});
const releasedEvent = new CustomEvent("release", {
bubbles: true,
@FlameWolf
FlameWolf / getRandomIdString.js
Last active May 26, 2023 13:13
Generate random ID string
const getRandomIdString = (len = 32) => {
let retVal = "";
for (let i = 0; i < len; i++) {
const randVal = Math.random();
retVal += String.fromCharCode((randVal < 0.5 ? ((randVal * 26) + 65) : ((randVal * 10) + 48)) >> 0);
}
return retVal;
};
@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
Last active July 10, 2026 04:12
Find the largest z-index in the document
const getMaxZIndex = () => Array.from(document.querySelectorAll("body *")).reduce((max, elem) => Math.max(max, parseInt(getComputedStyle(elem).zIndex) || 0), 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: