Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
crazy4groovy / observeResize.js
Created March 25, 2023 16:42
Detect when an element resizes with ResizeObserver (JavaScript)
const myObserver = new ResizeObserver(entries => {
entries.forEach(entry => {
console.log('width', entry.contentRect.width);
console.log('height', entry.contentRect.height);
});
});
const someEl = document.querySelector('.some-element');
myObserver.observe(someEl);
@crazy4groovy
crazy4groovy / mutation-parent-children.js
Created March 24, 2023 19:44
Detect changes to an element/parent or its children with MutationObserver (JavaScript)
const parentEl = document.querySelector('.my-parent')
const callback = (mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === "childList") {
console.log("A child node has been added or removed.", mutation);
}
if (mutation.type === "attributes") {
console.log(`The ${mutation.attributeName} attribute was modified.`, mutation.target[mutation.attributeName]);
}
@crazy4groovy
crazy4groovy / email.ts
Last active March 16, 2023 03:30
Astro Endpoint for sending emails via Gmail SMTP username + app password, via nodemailer (NodeJS)
import type { APIRoute } from "astro";
import type SMTPTransport from "nodemailer/lib/smtp-transport";
import nodemailer from "nodemailer";
/****
envs:
GMAIL_APP_PW=
GMAIL_ACCOUNT=
GMAIL_TO=
*/
@crazy4groovy
crazy4groovy / dl.deno.ts
Last active March 10, 2023 00:08
a throttled file downloader (Deno)
import { Timeout, TimeoutError } from "https://deno.land/x/timeout/mod.ts";
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
function newThrottler({ isBusy, lock, unlock, waitMs, size }) {
async function throttler(cb, ...args) {
size(1);
await Promise.resolve();
while (!!isBusy()) {
await delay(waitMs()); // waits in event loop queue, until it interrupts for another attempt!
@crazy4groovy
crazy4groovy / batch-subfolder-move.ps1
Last active March 11, 2023 06:03
various mass file manipulation scripts (PowerShell)
## Move files from a large-sized folder into smaller-sized subfolders
# Define variables
$sourceFolder = "C:\Path\To\SrcFolder"
$destinationFolder = "C:\Path\To\DestFolder"
$batchSize = 10000
$folderCounter = 1
# Create new subfolder in destination folder
$folderName = $folderCounter.ToString().PadLeft(4, '0')
@crazy4groovy
crazy4groovy / jotai.ts
Last active March 7, 2023 07:20
simple "atomic" react state management (jotai clone) (JavaScript)
import { useSyncExternalStore } from "react";
interface Atom<AtomType> {
get: () => AtomType;
set: (newValue: AtomType) => void;
subscribe: (callback: (newValue: AtomType) => void) => () => void;
_subscribers: () => number;
}
type AtomGetter<AtomType> = (
@crazy4groovy
crazy4groovy / find-overflowed-element.js
Created March 3, 2023 00:15
identify an overflowing element on the page (JavaScript)
/*
Real devs know that centering a div is easy, but what makes even the most senior developers cry - that random overflowing element that makes the whole page scroll horizontally.
How could you find that element? Answer below.
*/
document.querySelectorAll("*").forEach(el => {
if(el.offsetWidth > document.documentElement.offsetWidth) {
console.log(el);
}
})
@crazy4groovy
crazy4groovy / matchall.js
Last active January 28, 2023 18:08
regex multi matching (JavaScript)
const html ='<div><a title="123" href="http://xyz.com">link</a></div>';
const regexGroups = /title="([^"]+)" href="([^"]+)"/gm;
const matches = html.matchAll(regexGroups);
for (const match of matches) {
let [, title, url] = match;
// ...
}
@crazy4groovy
crazy4groovy / index.html
Created January 24, 2023 23:54
reveal-scroll animations via clip-path: inset (CSS)
<section class="section">
<div class="image-box" data-reveal="left">
<img src="https://img.freepik.com/premium-photo/turkish-baklava-dessert_127657-21789.jpg?w=2000" alt="" class="img">
</div>
<div class="content-box">
<h2 class="title" data-reveal="left">
Baklava
</h2>
<p class="text" data-reveal="left">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Id laborum iste doloremque ab facere unde alias sit commodi accusamus? Eius ut molestiae nemo perspiciatis, pariatur numquam accusamus voluptatem libero sint.
@crazy4groovy
crazy4groovy / confetti-burst.html
Last active December 17, 2022 17:56
A website's small confetti burst of joy! 🎉 (JavaScript)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script>
<script async>
confetti.create(null, {
resize: true,
useWorker: true,
})({ particleCount: 200, spread: 200 });
</script>