Skip to content

Instantly share code, notes, and snippets.

View mattConn's full-sized avatar
👾
UX engineer @ Harbour

Matt mattConn

👾
UX engineer @ Harbour
View GitHub Profile
@mattConn
mattConn / get_any_element_on_click.js
Last active August 27, 2016 23:26
Get any element in a document on click, hover, etc. Can access properties of "event.target" like you would typically access an element's properties, i.e. "event.target.style".
// getting an event's target
function getElementByEvent(event){
console.log(event.target);
}
// get the target
var element = document.getElementsByClassName('element')[0];
//adding event listener to target
element.addEventListener('click', function(event){
var newHTML = [
'<div class="foo">',
'<p>Readable HTML in JavaScript.</p>',
'</div>'
].join('');
@mattConn
mattConn / import_JSON_into_jade_gulpfile.js
Created September 19, 2016 01:27
Import JSON into Jade via Gulp.
var gulp = require('gulp'),
jade = require('gulp-jade'),
data = require('gulp-data'),
fs = require('fs');
gulp.task('jade', function(){
return gulp.src('src/jade/*.jade')
.pipe(data( function(file) {
return JSON.parse(fs.readFileSync('src/data.json'));
} ))
@mattConn
mattConn / jquery_keybinding.js
Last active November 22, 2016 03:11
JQuery switch statement for key binding.
$(document).keyup(function(e) { //keydown can be used as well.
switch(e.which) {
case 37: //left; any keycode can be used for each case.
alert('keycode: '+e.which);
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
@mattConn
mattConn / format_multiline_exec.php
Created January 1, 2017 00:56
Preserve formatting when displaying exec's returned string.
<?php
exec("$command 2>&1 &", $output);
foreach ($output as $line) {
echo "$line\n";
}
?>
@mattConn
mattConn / get_all_text_elements.js
Last active March 11, 2017 05:06
Get all text elements in a document.
// simple tag getting fn
var getTag = function(tag){
return document.getElementsByTagName(tag);
}
// wanted: elements we want to get by tag.
// caught: tags caught; where we will put the wanted elements, if they exist.
// missing: if tag not present in document, list here.
var textCatcher = {
wanted : ['p','ul','ol','li','a','span','strong','i','b','h1','h2','h3','h4','h5','h6'],
@mattConn
mattConn / loop_over_json_array_with_nodejs.js
Created March 13, 2017 06:19
Loop over an array in JSON file with Node. Hypothetical file here is "data.json", with an array named "array" located immediately within the object (not nested).
var fs = require('fs'),
util = require('util'),
exec = require('child_process').exec,
json = JSON.parse(fs.readFileSync('data.json'));
function puts(error, stdout, stderr){console.log(stdout, stderr)}
for (var i in json.array){
exec('echo '+json.array[i], puts);
}
@mattConn
mattConn / simple_watch_scripts.sh
Last active March 19, 2017 03:47
Simple watch scripts; one for Linux that uses inotifytools, one for Mac that uses fswatch. On file change, concat.sh is run and concatenates listed files.
# concat.sh
cat \
file1 \
file3 \
> file3
@mattConn
mattConn / simple_loop.sh
Last active March 22, 2017 21:42
Simple shell script to repeatedly run a command every 2 seconds.
#!/bin/sh
read input;
while true; do clear; $input; sleep 2; clear; done;
@mattConn
mattConn / quick_git_commit_push.sh
Created June 6, 2017 18:59
Quick git "commit -m" and push script
read input;
git add .; git commit -m "$input"; git push;