Skip to content

Instantly share code, notes, and snippets.

View samthor's full-sized avatar

Sam Thorogood samthor

View GitHub Profile
@samthor
samthor / undoer.js
Last active March 9, 2019 02:26
Native Undo & Redo helper
// This code now lives at https://github.com/samthor/undoer (or 'undoer' on NPM).
@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 / fixer.js
Created March 15, 2018 06:02
removes all print-specific styles from stylesheets
Array.from(document.styleSheets).forEach((ss) => {
const toDelete = [];
const toReplace = new Map();
for (let i = 0; i < ss.rules.length; ++i) {
const rule = ss.rules[i];
if (!(rule instanceof CSSMediaRule)) {
continue;
}
const functionMatch = /^function\s*\w*\(([\w\s,]+)\) {/;
/**
* @param {!Function} fn
* @return {!Array<string>} array of simple arg names (no =, ... etc)
*/
function simpleArgNames(fn) {
const match = functionRe.exec(fn.toString());
if (!match) { return []; }
const args = match[1].split(',').map((x) => x.trim());
@samthor
samthor / module-hacks.md
Last active September 7, 2017 03:16
ES6 module hacks and nuances

TODO

Hashed modules are different

import './banana.js';
import './banana.js#foo';

These are both run by Chrome 61, but are only fetched from the network once!

@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 / orientation.js
Created June 23, 2017 05:43
isLandscape function to determine whether device is landscape orientation
function isLandscape() {
var object = window.screen.orientation || window.screen.msOrientation || window.screen.mozOrientation || null;
if (object) {
if (object.type.indexOf('landscape') !== -1) { return true; }
if (object.type.indexOf('portrait') !== -1) { return false; }
}
if ('orientation' in window) {
var value = window.orientation;
if (value === 0 || value === 180) {
return false;
@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 / cwdtool.c
Created June 13, 2017 14:05
proc_getcwd, get the working dir of arbitrary process (BSD/macOS)
/*
* Copyright 2008 Sam Thorogood. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@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.: