Skip to content

Instantly share code, notes, and snippets.

View samthor's full-sized avatar

Sam Thorogood samthor

View GitHub Profile
@samthor
samthor / autocert-server.go
Last active April 15, 2024 15:47
Demo autocert server in Go
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/user"
@samthor
samthor / safari-nomodule.js
Last active February 14, 2024 02:54
Safari 10.1 `nomodule` support
// UPDATE: In 2023, you should probably stop using this! The narrow version of Safari that
// does not support `nomodule` is probably not being used anywhere. The code below is left
// for posterity.
/**
* Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will
* load <script nomodule> anyway. This snippet solve this problem, but only for script
* tags that load external code, e.g.: <script nomodule src="nomodule.js"></script>
*
* Again: this will **not** prevent inline script, e.g.:
@samthor
samthor / importPolyfill.js
Last active February 6, 2024 02:18 — forked from surma/importPolyfill.js
Polyfill for dynamic module loading that supports onerror
// usage:
// importScript('./path/to/script.js').then((allExports) => { .... }));
function importScript(path) {
let entry = window.importScript.__db[path];
if (entry === undefined) {
const escape = path.replace(`'`, `\\'`);
const script = Object.assign(document.createElement('script'), {
type: 'module',
textContent: `import * as x from '${escape}'; importScript.__db['${escape}'].resolve(x);`,
});
@samthor
samthor / dialog-focus-restore.js
Last active December 1, 2023 14:39
Restore focus after a HTML dialog is shown modally
/**
* Updates the passed dialog to retain focus and restore it when the dialog is closed. Won't
* upgrade a dialog more than once. Supports IE11+ and is a no-op otherwise.
* @param {!HTMLDialogElement} dialog to upgrade
*/
var registerFocusRestoreDialog = (function() {
if (!window.WeakMap || !window.MutationObserver) {
return function() {};
}
var registered = new WeakMap();
@samthor
samthor / js-compile.py
Last active August 19, 2023 09:26
Shell interface to Closure compiler (in Python)
#!/usr/bin/env python
import json
import sys
try: # Py3
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.request import Request
except ImportError: # Py2
@samthor
samthor / eventlistener-signal-support.js
Last active February 22, 2023 12:19
Polyfill/code for the signal argument to addEventListener
// This is part of the blog post here: https://whistlr.info/2022/abortcontroller-is-your-friend/
// ...and can be used to detect/polyfill the `signal` argument to addEventListener.
//
// Note that at writing, 86%+ of active browsers already support it:
// https://caniuse.com/mdn-api_eventtarget_addeventlistener_options_parameter_options_signal_parameter
// ...but that 92% of browsers support `AbortController` and signal.
//
// So there's 6% of total browsers which will fail silently when adding the `signal` argument.
// Eyeballing it, this is mostly Safari 11-15 and Chrome 66-90. These snippets can help with those targets.
//
#!/bin/bash
# Useful bash functions. This is sourced by the environment file.
# These are available to scripts, but you shouldn't use them in scripts if you
# want them to be portable.
# Usage: pathremove /path/to/bin [PATH]
# Eg, to remove ~/bin from $PATH
# pathremove ~/bin PATH
function pathremove {
local IFS=':'
@samthor
samthor / daikin-update-wifi.js
Last active December 9, 2022 19:59
Update WiFi on Daikin BRP069A42
// If the device is already on a network and you need to change it, you can load:
// http://<device_ip>/common/get_wifi_setting
// It'll give you a result like... ret=OK,ssid=OldSSIDHere,security=mixed,key=%6e%65%76%65%72%67%6f%6e%6e%61%67%69%76%65%79%6f%75%75%70
// To update the same data, open that page and in the console, run this (modify as needed):
const ssid = 'NewSSIDHere';
const key = 'nevergonnagiveyouup';
const encoded = Array.from(key).map((c) => '%' + c.charCodeAt(0).toString(16).padStart(2, '0')).join('');
@samthor
samthor / Stacker Helper
Last active August 27, 2022 13:58
Stacker Helper
0eNrNVV1uozAQvko1z1AJ8gPhBH3fvlWVZfA0sUpsZA/RRhEH2Fv0bD3J2iZByTZpXakPKyHAzMw3M998xgeo2x47IxVBdQDZaGWhejqAlWvFW/+N9h1CBZJwCwkovvUrw2ULQwJSCfwNVTY8J4CKJEkc48Niz1S/rdE4hynSkotdbygNEAl02roorXwqh5RnRQJ7qOaFQxfSYDMa8yH5AJrHg66iQWfRoHkWDTqPB51Fgy7iQRfRoMuLEadHGVwb0/0RtLxfXMI6CCcjMrplNW74Tmrjoxppml4Sa1ptkZ309cJbi8lkNMjFZCPTO5NBwXRPXU/sgyh30lAf6jsxgangau06cTVp419vBgdFO7e1QVRfeA2hJzW2aL0587cQeq53KaAqhudhuEJtMVXpx6VSS7r7RKurf4b1BataiQnmRRr7bbb81ifu/wMh1bbjhpNPAu9/3oLDMRUqXrfIhLT+eRqTRSUYaRZ6m+YaVsx32qGIqSh4pI/fY3x5xrZfl34CriTiIyMAV8ZRRio9z/4HpZ/E+iOa/kwIP6H01SW1zSbI/SbBs9sE+3Ml9FSdHVQJtLxG1xD8It68orl7wLZziRPYobHjdimzebHKi8XSXfNyGP4Cjmpeag==
@samthor
samthor / css-modules-plugin.mjs
Last active August 2, 2022 14:01
CSS Modules plugin for Rollup
import fs from 'fs';
// as per https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/master/CSSModules/v1Explainer.md
export default function cssModules() {
return {
name: 'css-modules',
async load(id) {
if (!id.endsWith('.css')) {
return;