Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / mock-axios.js
Last active April 14, 2024 05:40
axios mocking via interceptors
import axios from 'axios'
let mockingEnabled = false
const mocks = {}
export function addMock(url, data) {
mocks[url] = data
}
@cowboy
cowboy / HEY-YOU.md
Last active April 9, 2024 15:54
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@cowboy
cowboy / sudo-keepalive-example.sh
Created July 15, 2012 20:55
Bash: Sudo keep-alive (good for long-running scripts that need sudo internally but shouldn't be run with sudo)
#!/bin/bash
# Might as well ask for password up-front, right?
sudo -v
# Keep-alive: update existing sudo time stamp if set, otherwise do nothing.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Example: do stuff over the next 30+ mins that requires sudo here or there.
function wait() {
@cowboy
cowboy / stringify.js
Created September 19, 2012 13:45
JavaScript: like JSON.stringify but handles functions, good for creating arbitrary .js objects?
var stringify = function(obj, prop) {
var placeholder = '____PLACEHOLDER____';
var fns = [];
var json = JSON.stringify(obj, function(key, value) {
if (typeof value === 'function') {
fns.push(value);
return placeholder;
}
return value;
}, 2);
@cowboy
cowboy / isprimitive-no-strict.js
Created September 18, 2012 12:23
JavaScript: isPrimitive
var isPrimitive = function(val) {
return val !== function() { return this; }.call(val);
};
@cowboy
cowboy / name_of_script.cmd
Last active February 22, 2024 02:29
bash script in a Windows cmd file, so you can double-click and run right from Explorer
:<<"::CMDLITERAL"
@echo off
FOR /F "tokens=* USEBACKQ" %%F IN (`wsl wslpath '%~f0'`) DO (
SET script_path=%%F
)
bash -c "'%script_path%'"
exit /b
::CMDLITERAL
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
@cowboy
cowboy / umd-shuffle.js
Last active February 11, 2024 22:42
The "UMD returnExportsGlobal shuffle"
// Re. https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js
// ==================================================
// Q. Does all that UMD stuff need to be at the top?
// ==================================================
(function (root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
@cowboy
cowboy / jrs-syncatron.c
Last active February 3, 2024 17:42
JRS syncatron
// To give your project a unique name, this code must be
// placed into a .c file (its own tab). It can not be in
// a .cpp file or your main sketch (the .ino file).
#include "usb_names.h"
// Edit these lines to create your own name. The length must
// match the number of characters in your custom name.
#define MIDI_NAME {'S','y','n','c','a','t','r','o','n'}
@cowboy
cowboy / github_post_recieve.php
Created October 11, 2010 02:04
GitHub PHP webhook to auto-pull on repo push
<?php
// Use in the "Post-Receive URLs" section of your GitHub repo.
if ( $_POST['payload'] ) {
shell_exec( 'cd /srv/www/git-repo/ && git reset --hard HEAD && git pull' );
}
?>hi
@cowboy
cowboy / bookmarklet.js
Last active January 9, 2024 14:50
reddit megalinks: attempt to base64 decode all words 16+ chars in length
javascript:(function()%7B%5B...document.querySelectorAll('.usertext-body%20*')%5D.forEach(p%20%3D%3E%5B...p.childNodes%5D.filter(n%20%3D%3E%20n.nodeType%20%3D%3D%3D%203).forEach(n%20%3D%3E%20%7Bconst%20re%20%3D%20%2F%5CS%7B16%2C%7D%2Fg%3Bif%20(re.test(n.nodeValue))%20%7Btry%20%7Blet%20isHtml%2C%20child%3Bconst%20s%20%3D%20n.nodeValue.replace(re%2C%20s%20%3D%3E%20%7Bs%20%3D%20atob(s)%3Bif%20(%2F%5Ehttps%3F%3A%5C%2F%5C%2F%2F.test(s))%20%7Bs%20%3D%20%60%3Ca%20href%3D%22%24%7Bs%7D%22%20target%3D_blank%3E%24%7Bs%7D%3C%2Fa%3E%60%3BisHtml%20%3D%20true%3B%7Dreturn%20s%3B%7D)%3Bif%20(isHtml)%20%7Bconst%20tmp%20%3D%20document.createElement('div')%3Btmp.innerHTML%20%3D%20s%3Bchild%20%3D%20document.createDocumentFragment()%3B%5B...tmp.childNodes%5D.forEach(c%20%3D%3E%20child.append(c))%3B%7D%20else%20%7Bchild%20%3D%20document.createTextNode(s)%3B%7Dp.replaceChild(child%2C%20n)%3B%7D%20catch%20(e)%20%7B%7D%7D%7D))%7D)()