Skip to content

Instantly share code, notes, and snippets.

View Gergling's full-sized avatar
🦇
Behind a keyboard

Greg Gergling

🦇
Behind a keyboard
View GitHub Profile
@Gergling
Gergling / PackageLoader.js
Created May 23, 2013 15:57
This is a requirejs-based preloader progress bar using jquery. Download the files into a directory and run index.html in a browser to see the effects.
var PackageLoader = function() {
this.cb = Math.random(0, 100000000000);
this.current = 0;
this.batches = [];
// Load kicks off the entire loading process.
this.load = function(config, loadMap, onload) {
var scope = this;
for(label in loadMap) {
scope.batches.push(new PackageBatch(label, loadMap[label]));
@Gergling
Gergling / auto.sh
Created October 13, 2017 13:00
Bash scripts for restarting wifi when it fails.
#!/bin/bash
recent_restart=1
while true; do
printf $(date +"%Y-%m-%d")
printf " "
printf $(date +"%H:%M:%S")
printf " "
if [[ $(ping -c 1 google.com) ]] > /dev/null 2>&1 ; then
if [ $recent_restart -eq 0 ] ;
@Gergling
Gergling / index.js
Created October 20, 2018 19:56
Star Trek Engineering Crisis
const getRandomItem = list => list[Math.floor(Math.random() * list.length)];
const components = [
'matter/antimatter manifold',
'main deflector dish',
'kettle',
'Captain\'s copy of TMNT Issue #1 signed by Roger Moore',
'baffle plate',
'navigational laser',
'secondary plasma relay'
@Gergling
Gergling / file-size-listing.js
Last active October 10, 2018 13:48
Script to print the sizes of files in order of largest to smallest within their parent folders. Useful for investigating large folders for things which should be deleted or archived for space.
var fs = require("fs"); //Load the filesystem module
const { join } = require('path');
class FileNode {
constructor(path) {
const isDirectory = fs.lstatSync(path).isDirectory();
this.path = path;
if (isDirectory) {
function stringifyExplicitObject(subject) {
return '{' + Object.entries(subject).map(function (entry) {
return entry[0] + ':' + stringify(entry[1]);
}).join(',') + '}';
}
function stringifyArray(subject) {
return '[' + subject.map(function (element) {
return stringify(element);
}).join(',') + ']';
}
@Gergling
Gergling / MutableObject.js
Created April 12, 2018 09:46
A mutable object for functional programming.
// Deliberately not using class because I want data to be private.
function MutableObject() {
const data = {};
const get = name => (name === undefined) ? data : data[name];
const setObject = value => Object.assign(data, value);
const setByFunction = fnc => fnc(this);
const setNameValue = (name, value) => data[name] = value;
const set = (a, b) => {
if (typeof a === 'object') {
@Gergling
Gergling / mischief.js
Created April 9, 2018 13:50
Canvas attachment for mischief
(function (document) {
var canvas = document.createElement('canvas');
canvas.style.width='100%';
canvas.style.height='100%';
canvas.style.top=0;
canvas.style.left=0;
canvas.style.position='absolute';
document.body.appendChild(canvas);
return canvas;
}(document));
@Gergling
Gergling / functionalise.js
Last active April 9, 2018 13:26
Putting the FUN in FUNctional programming.
// Takes a function with two parameters and allows them to be used in the format fnc(x)(y)().
function functionalise(fnc) {
return function functionalised(x) {
return function (y) {
if (y === undefined) {
return x;
} else {
return functionalised(fnc(x, y));
}
};
@Gergling
Gergling / pdf-fields.php
Last active April 9, 2018 13:22
Takes a `pdftk` field dump and turns it into a PHP-copyable output.
<?php
// Usage example: pdftk stuff.pdf dump_data_fields | php pdf-fields.php
$stdin = file('php://stdin');
// print_r($stdin);
$data = [];
$i = 0;
foreach($stdin as $idx => $line) {
$chunks = explode(': ', $line);
@Gergling
Gergling / console.require.js
Last active September 7, 2016 11:39
Script for copying into a console to include jquery and requirejs.
// Usage:
//document.write('<scr'+'ipt src="https://requirejs.org/docs/release/2.1.8/comments/require.js"></scr'+'ipt>');
var script = document.createElement('script');script.src = "https://requirejs.org/docs/release/2.1.8/comments/require.js";
document.head.appendChild(script);
var oldDollar = $;requirejs(["https://code.jquery.com/jquery-1.10.2.js"], function() {
var jQuery = $;
var $ = oldDollar;
});