Home

Blog

Wiki

Labs

Code Snippets

HTML

Theme-based stylesheets

<link rel="stylesheet" href="light.css" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="dark.css" media="(prefers-color-scheme: dark)">

This is not supported by IE and other lesser known browsers.

JavaScript

Asynchronous functions in loops

const start = 0;
const end = 5;

for (i = start; i < end; i++) {
    setTimeout(() => {
        console.log(i); // -> 5, 5, 5, 5, 5
    });
};

function forSync(i) {
    setTimeout(() => {
        console.log(i); // -> 0, 1, 2, 3, 4
        if (i + 1 < end) forSync(i + 1);
    });
};
forSync(start);

This allows for asynchronous functions by placing the callback within that function.

Check if executable exists in PATH

const path = require("path");
const fs = require("fs");

process.env.PATH.split(path.delimiter).some(x => fs.existsSync(path.join(x, "node")));

Expand Bootstrap accordions via URL

$(".collapsed[data-target='" + window.location.hash + "']")?.click();

Did I mention that optional chaining absolutely rocks?

Export promise with arguments

const module = require("./module");
module("Hello!").then(console.log); // -> Hello!

module.exports = (input) => {
    return new Promise((resolve, reject) => resolve(input));
};

exports won’t work, only module.exports will!

Get DOM element from jQuery element

$("#foo")[0];
$("#foo").get(0);

Use either, see the jQuery FAQ.

insertAdjacentHTML() positions

<!-- beforebegin -->
<parent>
    <!-- afterbegin -->
    <child></child>
    <!-- beforeend -->
</parent>
<!-- afterend -->

Copied from the MDN documentation.

Swap out variable values

var a = 1;
var b = 2;

[a, b] = [b, a];

console.log(a); // -> 2
console.log(b); // -> 1

You can also specify more (or less) than two variables.

Write to clipboard

navigator.clipboard.writeText("Text").then(function() {
    // Success!
}, function() {
    // Error!
});

This is not supported in IE, and Firefox hides it behind a flag in some cases.

PHP

Temporarily disable Composer cache

COMPOSER_CACHE_DIR=/dev/null composer

See the Composer documentation for details.

Use PHP variables in variable names

$foo = "bar";
$foo = "baz";
echo $bar; // -> baz

Here, $foo resolves to $bar!

Other

Connect via ZOC from WinSCP

WinSCP → Options → Preferences → Applications → PuTTY/Terminal client path:

"zoc.exe" /CONNECT:!U:!P@!@:!# "/RUN:%USERPROFILE%\Documents\ZOC8 Files\REXX\cd.zrx" "/RUNARG:!/"

%USERPROFILE%\Documents\ZOC8 Files\REXX\cd.zrx:

CALL ZocSend "cd '"ARG(1)"'^M"

Change video framerate without interpolation or re-encoding

To convert a video with X FPS to a video with Y FPS, divide X by Y, and use the result like so:

ffmpeg -itsscale X/Y -i input.mp4 -c copy output.mp4

This only accounts for video frames; the audio will not speed up or slow down.

Create new PostgreSQL user and database

CREATE DATABASE database;
CREATE USER user WITH ENCRYPTED PASSWORD password;
GRANT ALL PRIVILEGES ON DATABASE database TO user;

Don’t kill process when logging out

First, press CTRL+Z on the running command. Then:

bg
disown -h

To bring it back after, for example, logging out and back in:

fg

Don’t kill user systemd units when logging out

loginctl enable-linger

Fix error 0x80073D05 when installing Ubuntu on Windows

Remove-Item -Recurse $env:LOCALAPPDATA/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc

Install Docker on Raspberry Pi

curl https://get.docker.com | sh
sudo usermod -aG docker $USER
sudo reboot

Open .URL files with default browser on Linux via Nemo

Right-click on any .URL file, select “Open With”, “Other Application” and enter the following in the “Enter a custom command…” field:

sh -c "cat %F | grep URL= | cut -d '=' -f2- | xargs xdg-open"

Pin retweets to Twitter profile

  1. If applicable, undo the retweet of the respective tweet.
  2. Open the browser’s network inspector and retweet it again.
  3. Open the response of the POST request to retweet.json.
  4. Copy the root id_str.
  5. Run twurl -d "tweet_mode=extended&id=id_str" /1.1/account/pin_tweet.json, replacing the id_str placeholder.

Re-use blocks in Caddyfile

(foo) {
    log
}

example.com {
    import foo
}

See the Caddy documentation for details.

TwitterRedditMastodonMerveillesGitHubLast.fmSpotifyTraktLetterboxdSteamWebring