Skip to content

Instantly share code, notes, and snippets.

View bennettmcelwee's full-sized avatar

Bennett McElwee bennettmcelwee

View GitHub Profile
@bennettmcelwee
bennettmcelwee / stringify.js
Last active November 19, 2022 09:20
Version of JSON.stringify limitied to a specific depth.
// Similar to JSON.stringify but limited to a specified depth (default 1)
// The approach is to prune the object first, then just call JSON.stringify to do the formatting
const prune = (obj, depth = 1) => {
if (Array.isArray(obj) && obj.length > 0) {
return (depth === 0) ? ['???'] : obj.map(e => prune(e, depth - 1))
} else if (obj && typeof obj === 'object' && Object.keys(obj).length > 0) {
return (depth === 0) ? {'???':''} : Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: prune(obj[key], depth - 1)}), {})
} else {
return obj
@bennettmcelwee
bennettmcelwee / google-link-cleaner.user.js
Created December 4, 2012 04:02
Google link cleaner
var digitToNum = digit => ({v:digit, f:`${digit}`})
var partitions = x => times(i => [take(i+1, x), drop(i+1, x)], x.length - 1)
const negop = {o:(a) => ({o:negop, a, v:-a.v, f:`-${a.f}`})}
const unops = [negop]
const addop = {o:curry((a,b) => ({o:addop, a, b, v:a.v+b.v, f:`${a.f}+${b.f}`}))}
const subop = {o:curry((a,b) => ({o:subop, a, b, v:a.v-b.v, f:`${a.f}-${b.f}`}))}
const mulop = {o:curry((a,b) => ({o:mulop, a, b, v:a.v*b.v, f:`${a.f}x${b.f}`}))}
const divop = {o:curry((a,b) => (b.v != 0 ? {o:divop, a, b, v:a.v/b.v, f:`${a.f}/${b.f}`} : null))}
const biops = [addop, subop, mulop, divop]
$(() => {
const observer = new MutationObserver(addFormatControls)
observer.observe(document.querySelector('body'), {attributes: true, subtree: true})
function getCommentInput (optionsElement) {
return $(optionsElement).closest('.comment-box').find('textarea').get(0)
}
function wrapSelection (input, wrap) {
const selection = input.value.slice(input.selectionStart, input.selectionEnd)
@bennettmcelwee
bennettmcelwee / freegal-tweaks.js
Last active January 23, 2018 02:41
Snippet to inject into Freegal pages to make them more readable
// Change New Releases page to a complete text listing with hover images
if (window.location.pathname === '/homes/new_releases') {
$('<style>\
.videos-shadow-container { display: none; }\
.my-top-100-page { height: auto; }\
.album-shadow-container > .album-scrollable { display: none; }\
.album-shadow-container > button { display: none; }\
.album-shadow-container + h3 { display: none; }\
.hnr-at { border-spacing: 3px; border-collapse: separate; margin-left: -3px; font-family: "Helvetica Neue", sans-serif; }\
.hnr-at .album-link img {position: absolute; }\
$(`<style>
.fp-album-wrapper:not(:hover) .fp-track-listing {
display: none;
margin-left: -0.5em;
}
.album-title .fp-track-listing {
position: absolute;
z-index: 1;
}
.fp-track-listing {
if [ -f ~/bin/sensible.bash ]; then
source ~/bin/sensible.bash
fi
if [ -f ~/bin/git-completion.bash ]; then
source ~/bin/git-completion.bash
fi
export EDITOR="open -t -n -W"
@bennettmcelwee
bennettmcelwee / search.html
Last active February 11, 2016 00:29
Show a search menu
<title>Search</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style>
.hide { display: none; }
.search, #term, #go {
display: block;
font: 20px/50px sans-serif;
width: 100%;
@bennettmcelwee
bennettmcelwee / allmusic-search.html
Last active February 10, 2016 23:21
Allmusic search
<title>Search</title>
<script>
const name = 'AllMusic.com';
const baseUrl = 'http://www.allmusic.com/';
const searchRel = 'search/all/';
var s = window.prompt('Search ' + name, '');
window.location.href = s ? baseUrl + searchRel + s : baseUrl;
</script>
@bennettmcelwee
bennettmcelwee / bookmarkleteer.html
Last active December 19, 2015 20:58
Web page that can turn Javascript into a bookmarklet
<!DOCTYPE html>
<html>
<head>
<title>Bookmarkleteer</title>
<script>
/**
* Bookmarklet Compiler
*
* Inspired by Moxley Stratton - http://www.moxleystratton.com/
*/