Skip to content

Instantly share code, notes, and snippets.

View hlfbt's full-sized avatar
🤔

Alexander Schulz hlfbt

🤔
View GitHub Profile
@hlfbt
hlfbt / fc2int.js
Created November 25, 2019 04:07
Convert Nintendo friend codes to plain old integers, for whatever reason you might want to do that anyway
function fc2int(fc) {
const codes = '0123456789ABCDEFGHJKLMNPQRSTUVWXY';
fc = fc.toUpperCase().match(new RegExp(`[${codes}]`, 'g'));
return -1 + fc
.map((a, i) => (codes.indexOf(a) + 1) * Math.pow(codes.length - 1, i))
.reduce((a, b) => a + b);
}
@hlfbt
hlfbt / getopts.sh
Created May 3, 2019 13:46
Simple somewhat advanced getopts only dependent on bash and expr
#!/bin/bash
# Simple more advanced arguments parsing than builtin tools like getopts or a simple while/case looping over the arguments
#
# Features include handling of:
# - multiple grouped shorthands (f.i. -abc becoming -a -b -c),
# - equal sign value assignment (f.i. --arg=value becoming --arg value, and also -a=value becoming -a value)
# - dashless value assignment (f.i. arg=value becoming arg value)
#
# The only dependencies are bash (for arrays) and expr (included in coreutils) (for regexp matching and extraction) which are likely available everywhere (even in a busybox or on a mac!)
#
@hlfbt
hlfbt / debug_modifiers.java
Created March 21, 2019 23:22
Get a human readable list of all modifiers on a given Method or Field
/*
This isn't supposed to be pretty or ever be used in production code (don't even think about it), but has proven useful for quick debugging when working with reflections.
*/
// Example Method with some more interesting modifiers
Method meth = Arrays.stream(data.getClass().getDeclaredMethods()).filter(m -> "getChronology".equals(m.getName())).toArray()[0];
// Should return "isSynthetic, isPublic, isVolatile"
String modifiers = Arrays.stream(Modifier.class.getDeclaredMethods())
.map(m -> {
@hlfbt
hlfbt / twitch_color_switcher.js
Last active April 10, 2022 01:28
Twitch Color Switcher for you bad bad kids out there. Enable with `enableColorSwitcher(<switching_interval_in_seconds>)` and disable again by reloading or calling `disableColorSwitcher()`.
let oauthToken = document.cookie.match(/auth-token=([0-9a-z]+);/)[1];
const updateChatColor = function (chatColor) {
fetch('https://gql.twitch.tv/gql', {
method: 'POST',
body: JSON.stringify({
query:
`mutation Chat_UpdateChatColor(\$input: UpdateChatColorInput!) {
updateChatColor(input: \$input) {
user {
@hlfbt
hlfbt / amazon_total_tally.js
Created October 14, 2018 00:48
Does a tally of amount spent, orders and items ordered on amazon. To be executed on any amazon order history page.
function fetchPage(link, callback) {
var xhr = new XMLHttpRequest();
xhr.open('get', link);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200)
callback.call(xhr, xhr);
};
xhr.send();
@hlfbt
hlfbt / guid.js
Created September 16, 2018 17:31
Generate random GUIDs usable as element IDs (first character is always a letter)
(function () {
var S4 = function (n, d) {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (n > 1 ? (d || '') + S4(n - 1, d) : '');
};
return (String.fromCharCode(97 + Math.floor(Math.random() * 26)) + S4().substring(1) + S4(5, '-') + S4(2));
})();
@hlfbt
hlfbt / objectify.js
Last active February 5, 2018 13:57
Generic object class construction
/**
* GenericObject / objectify.js
*
* Creating new objects with neat setters and getters over again is annoying, so let's automate it.
*
* @author Alexander Schulz (alex@nope.bz)
*/
var Objectify = (function () {
"use strict";
@hlfbt
hlfbt / object_search.js
Last active February 22, 2017 15:28
Searches through an object for an identifier
/*
* Recurses through an object to search for an identifier.
* Identifier is a RegExp.
* Returns either the first match or false.
*/
function search (obj, term) {
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
if (k == term) return k;
@hlfbt
hlfbt / twig-repl.php
Last active September 21, 2016 15:11 — forked from arnaud-lb/twig-repl.php
Twig REPL
<?php
/**
* Twig REPL hack
*
* Arnaud Le Blanc <arnaud.lb@gmail.com>
*/
require 'vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
@hlfbt
hlfbt / cache.js
Last active August 29, 2015 14:19
A simple cache for javascript. Useful to have a simple interface to share variables over between pageloads.
function Cache () {
"use strict";
if (this.constructor != Cache) throw new TypeError("Constructor Cache requires 'new'");
/*
A dumb, simple cache, to make access to cross-pageload variables easy and manageable.
All cache times are in milliseconds.
*/
var storage = window['localStorage'];