Skip to content

Instantly share code, notes, and snippets.

View endymion1818's full-sized avatar
🦈
47

Ben Read endymion1818

🦈
47
View GitHub Profile
@endymion1818
endymion1818 / asciiartgenerator.jsx
Last active August 29, 2024 11:17
Generate animated asciiart. Looks like the original Doctor Who intro
// shamelessly stolen from https://glama.ai/blog/2024-08-29-reverse-engineering-minified-code-using-openai
import React, { useEffect, useRef, useState } from 'react';
const selectedCharacterSet =
"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. .:â–‘â–’â–“â–ˆ";
const characterSetLength = selectedCharacterSet.length;
const calculateCharacter = (
x: number,
@endymion1818
endymion1818 / wait-for-element.js
Last active December 16, 2024 10:41
Wait for an element to appear in the DOM
/**
* Wait for an element to be added to the DOM or shadow DOM.
* @param {ShadowRoot | Document} root - The root to observe.
* @param {string} selector - The selector of the element to wait for.
* @param {number} [timeout] - The time in milliseconds to wait before rejecting the promise.
* @return {Promise<HTMLElement | Node>} - A promise that resolves with the element when it is added to the DOM or shadow DOM.
*/
function waitForElement(root, selector, timeout = 1000) {
let timeoutId;
return new Promise((resolve, reject) => {
@endymion1818
endymion1818 / milliseconds-to-timestamp.js
Last active June 19, 2024 09:01
Change milliseconds to a timestamp in the format `HH:MM:SS`
/**
*
* @param {number} milliseconds - time in milliseconds
* @returns {string} - time in hours, minutes, seconds
*/
function millisecondsToTimestamp(milliseconds) {
if(milliseconds < 0) {
return '00:00:00';
}
if(milliseconds === 0) {
@endymion1818
endymion1818 / fetch.js
Last active February 5, 2024 10:44
fetch
async function getStuff(token) {
const body = {}
// wrap in try / catch so we can resolve any network failures
try {
// initial request
const result = await fetch(`https://example.com/api/v1`, {
headers: {
"x-CSRF-Token": token,
"Content-Type": "application/json"
},
@endymion1818
endymion1818 / zathras.zsh-theme
Last active June 5, 2024 11:18
ZHS theme (forked from "amuse")
# vim:ft=zsh ts=2 sw=2 sts=2
# Must use Powerline font, for \uE0A0 to render.
ZSH_THEME_GIT_PROMPT_PREFIX=" on %{$fg[magenta]%}\uE0A0 "
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}!"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?"
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_RUBY_PROMPT_PREFIX="%{$fg_bold[red]%}‹"
@endymion1818
endymion1818 / matchWeatherCode.js
Last active November 3, 2023 12:13
matchWeatherCode.js
// @see https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM
function matchWeatherCode(code) {
switch (code) {
case 0:
return "🌥️"
case 1:
return "🌥️"
case 2:
return "🌥️"
case 3:
@endymion1818
endymion1818 / pre-push
Created October 18, 2023 15:01
git pre-push hook
# ~/.git_templates/hooks/pre-push
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
echo "${protected_branch} is a protected branch, create PR to merge"
@endymion1818
endymion1818 / restore-blog-scraper.js
Created September 7, 2023 15:08
restore blog scraper
const { parse } = require('rss-to-json');
const cheerio = require('cheerio');
const fs = require('fs');
const TurndownService = require('turndown')
const turndownService = new TurndownService({
codeBlockStyle: 'fenced',
bulletListMarker: '-',
headingStyle: 'atx',
})
@endymion1818
endymion1818 / index.d.ts
Created July 27, 2023 14:20
Pick only one
type RequireOnlyOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>>
& {
[K in Keys]-?:
Required<Pick<T, K>>
& Partial<Record<Exclude<Keys, K>, undefined>>
}[Keys]
type Events = 'play' | 'pause' | 'ready' | 'seek' | 'end';