Skip to content

Instantly share code, notes, and snippets.

View liamnewmarch's full-sized avatar
🤖
beep boop

Liam Newmarch liamnewmarch

🤖
beep boop
View GitHub Profile
@liamnewmarch
liamnewmarch / hue-rotate.js
Last active September 25, 2018 12:11
Hue rotate bookmarklet for Chrome.
javascript:((h,s)=>{(loop=()=>{requestAnimationFrame(loop),s.filter=`hue-rotate(${h=(h+10)%360}deg)`})()})(0,document.all[0].style);
@liamnewmarch
liamnewmarch / konami.js
Last active February 14, 2020 09:45
Listens for the Konami code, runs a callback when detected.
/**
* Konami
*
* Listens for the [Konami code][1], runs a callback when detected.
*
* [1]: https://en.wikipedia.org/wiki/Konami_Code
*
* Example usage:
*
* import { konami } from './konami.js/';
@liamnewmarch
liamnewmarch / git-fix-author
Last active August 13, 2021 14:04
Correct git author name and email for a branch by rewriting its history.
#!/bin/bash
# shellcheck disable=SC2016
# Rewrite git history.
readonly VERSION='2.0.5'
# Exit immediately if a command exits with a non-zero status.
set -e
@liamnewmarch
liamnewmarch / binary-text.js
Last active November 9, 2016 14:42
Convert ascii text to and from binary.
class BinaryText {
static encode(string) {
return string.split('').map(char => {
const charCode = char.charCodeAt(0);
return charCode.toString(2);
}).join('');
}
static decode(binary) {
return binary.match(/.{8}/g).map(byte => {
@liamnewmarch
liamnewmarch / roman-numerals.js
Last active April 7, 2021 08:13
Convert numbers to roman numerals.
/**
* RomanNumerals singleton class.
*
* Usage:
*
* RomanNumerals.convert(12); // 'XII'
*/
class RomanNumerals {
static numeralValues = {
M: 1000,
@liamnewmarch
liamnewmarch / date-format.js
Created November 10, 2016 16:24
A simple class for formatting dates.
class DateFormat {
constructor(format) {
this.format = format instanceof String ? format : String(format);
}
get _formatMap() {
return ['yyyy', 'yy', 'mm', 'm', 'dd', 'd', 'hh', 'h', 'ii', 'i', 'ss', 's'];
}
format(date) {
@liamnewmarch
liamnewmarch / dom-parse.js
Last active July 1, 2021 16:23
Utility function parse a string as HTML, SVG or XML.
/**
* Utility function parse a string as HTML, SVG or XML.
*
* Example usage:
* parse('<a href="https://www.google.com">')[0].click();
*
* @param {string} string The string to parse [1]
* @param {type='text/html'} string The mime type to parse the string as [2]
* @returns {HTMLCollection} A HTML collection of the parsed elements
*
@liamnewmarch
liamnewmarch / convert.js
Created March 27, 2018 15:30
Conversion between camelCase and kebab-case, using the DOM.
/**
* Convert kebab-case to camelCase.
* @param {string} kebabString The kebab-case string to convert.
* @return {string} The string converted to camelCase.
*/
function camelCase(kebabString) {
const div = document.createElement('div');
div.setAttribute(`data-${kebabString}`, '');
return Object.keys(div.dataset)[0];
}
@liamnewmarch
liamnewmarch / crosshairs.js
Created September 25, 2018 11:30
Bookmarklet for quickly adding crosshairs to a page for visually checking alignment
// 1. Create a new bookmark – usually by right-clicking the bookmark toolbar.
// 2. Name it (e.g. ‘crosshair’) and paste the following line in the URL field:
javascript:((d,p)=>{c=d.body.appendChild(d.createElement('canvas')),a=c.getContext('2d'),d.onmousemove=(e)=>{c.width=w=innerWidth;c.height=h=innerHeight;with(c.style)position='absolute',top=left=0,width=w+p,height=h+p,pointerEvents='none';with(a)clearRect(0,0,w,h),fillStyle='#0002',fillRect(0,e.pageY,w,1),fillRect(e.pageX,0,1,h)}})(document, 'px')
@liamnewmarch
liamnewmarch / format-relative.js
Last active November 3, 2023 15:29
Relative time strings using the web platform
/**
* The target language. [Browser support is good][1] but "en-US" is a safe default.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language
*
* @type {string}
*/
const { language = "en-US" } = navigator;
/**