Skip to content

Instantly share code, notes, and snippets.

@alanmquach
alanmquach / post.js
Created April 22, 2020 02:12
POST JSON using fetch
const post = (url, body) => fetch(url, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(body)
});
@alanmquach
alanmquach / plexpip.js
Last active April 25, 2018 20:35
JavaScript snippet to hid overlay from the Plex mini-player so that the native right-click menu for the video element can be accessed (to enable PIP on Mac OS)
document.querySelector("button[title='Expand Player']").style.display = "none"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
source ~/.bashrc
nvm install stable
npm install rx request
@alanmquach
alanmquach / .bootstrap_git
Last active February 7, 2016 20:03
Bootstrap Mac shell with my bash prompt and git tab completion
echo "Downloading tab completion to ~/.tab_completion_git"
curl -s "https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash" > ~/.tab_completion_git
echo "Downloading bash prompt to ~/.bash_prompt"
curl -s "https://gist.githubusercontent.com/alanmquach/31d9088cf7f4126c59d1/raw/5ce769a35513a98997e5dda32b259e9ac3ece0a1/.bash_prompt" > ~/.bash_prompt
echo "Adding .tab_completion_git, .bash_prompt to .bash_profile"
echo 'for file in ~/.{tab_completion_git,bash_prompt}; do [ -r "$file" ] && [ -f "$file" ] && source "$file"; done' >> ~/.bash_profile
echo 'export BASH_PROFILE_SOURCED=true' >> ~/.bash_profile
@alanmquach
alanmquach / squash.js
Created May 13, 2015 22:10
Async composition
// Pyramid of doom
function go(input) {
// step 1
doSomethingAsync(input, function (result) {
// step 2
doSomethingElse(result, function (result2) {
// step 3
doYetAnotherThing(result2, function (result3) {
moveOnWithLife(result3);
});
@alanmquach
alanmquach / uniq.js
Last active August 29, 2015 14:19
Find/count unique lines (i.e. 'cat filewithduplicates.log | sort | uniq -c | sort -rn')
#!/usr/bin/env node
// $ cat filewithduplicates.log | uniq.js | sort -rn | cut -f 2-
var uniq = {};
require('readline').createInterface({
input: process.stdin,
terminal: false
}).on('line', function(line){
uniq[line] = ++uniq[line] || 1;
}).on('close', function () {
@alanmquach
alanmquach / chaintimer.sh
Created February 16, 2015 23:27
Breakdown the times of a redirect chain by redirect
NEXTURL="http://bit.ly"
while [ -n "$NEXTURL" ]; do
RESP=$(curl -o /dev/null -s -w "%{time_total},%{redirect_url}" $NEXTURL);
THISTIME=`echo -n "${RESP}" | cut -f 1 -d ","`;
echo -ne "${THISTIME} : ${NEXTURL} \n";
NEXTURL=`echo -n "${RESP}" | cut -f 2 -d ","`;
done
@alanmquach
alanmquach / RxZipper.js
Last active November 25, 2015 12:59
RxJS equivalent of async.parallel
var Rx = require('rx');
var zipper = function () {
// Turning arguments from an object into an actual array so we can use things like map()
return Array.prototype.slice.call(arguments);
};
var array; // Given some array of Observables that you want zipped together
// Or the more classical case where given an array of data to operate on, simply map them into observables
@alanmquach
alanmquach / plumber.js
Last active August 29, 2015 14:12
Process unix streams with a node script
#!/usr/bin/env node
// $ echo "madam im adam" | ./plumber.js
require('readline').createInterface({
input: process.stdin,
terminal: false
}).on('line', function(line){
process.stdout.write(line.split("").reverse().join("") + '\n');
});
@alanmquach
alanmquach / SSHKeyer
Created July 17, 2014 06:44
Expect wrapped ssh command to put public key onto a server
/usr/bin/expect -c "set timeout 60; spawn ssh ${SSH_OPTS} ${USERID}@${HOST} \"mkdir -p ~/.ssh && echo \\\"`cat ~/.ssh/id_rsa.pub`\\\" >> ~/.ssh/authorized_keys\"; expect \"*?assword:*\"; send -- \"${PASSWD}\r\"; expect eof"