Skip to content

Instantly share code, notes, and snippets.

@namklabs
namklabs / find-replace-multiple-preserve-eol.sh
Created July 19, 2012 16:15
find and replace on multiple files, preserve line endings
# This file operates on every file within the current directory and below, recursively.
# The original command is from http://rushi.vishavadia.com/blog/find-replace-across-multiple-files-in-linux
# but has been modified to not mess with end of line characters (-b option in sed) according to http://stackoverflow.com/a/11508669
find . -name "*.html" -print | xargs sed -i -b 's/find/replacewith/g'
# This example command searches only .html files. This can be changed to anything.
# You need to specify what to find and what to replace it with (find/replacewith). These are regular expressions, so be careful of special characters.
@namklabs
namklabs / prevent-image-downloads.js
Created September 14, 2012 15:33
Prevent Image Downloads
$(function(){
$.each( $("img"), function(){
$(this).parents().show().end().show().css({
"background-image": "url(" + $(this).attr("src") + ")",
"background-size": "100%",
"width": $(this).width(),
"height": $(this).height()
}); // show all images so that their size isn't 0x0 - showing parents enables children to be shown too. Then replace src with decoy image and make background the real image with the proper scale in case the image is scaled.
$(this).attr("src","./image/cache/empty.png");
});
@namklabs
namklabs / hide-on-scroll.jquery.js
Last active October 15, 2023 12:50
fade out and hide a fixed element when you scroll to the bottom of the page (jQuery)
//requires jQuery
$(window).scroll(function(){
var threshold = 200; // number of pixels before bottom of page that you want to start fading
var op = (($(document).height() - $(window).height()) - $(window).scrollTop()) / threshold;
if( op <= 0 ){
$("#thing-to-hide").hide();
} else {
$("#thing-to-hide").show();
}
$("#thing-to-hide").css("opacity", op );
@namklabs
namklabs / object.properties.js
Created January 30, 2013 19:56
list all properties of a JavaScript object
for(var key in x){
document.write( key + ": " + x[key] + "<br>" );
}
@namklabs
namklabs / octet-training.bash
Last active December 15, 2015 18:58
output a random octet, show the values of each bit in an octet, and output the actual decimal number in bash currently in use on a geeklet on my desktop :)
# | sed "s/^\([0-1]\)\([0-1]\)\([0-1]\)\([0-1]\)\([0-1]\)\([0-1]\)\([0-1]\)\([0-1]\)$/\1 \2 \3 \4 \5 \6 \7 \8/g" |
r=$(($RANDOM % 255 + 1)); echo "obase=2;${r}" | bc | sed "s/\$/ \\`echo -e '\n\r'`128 64 32 16 8 4 2 1\\`echo -e '\n\r'`${r}/"
@namklabs
namklabs / enquire-load.js
Created April 9, 2013 22:45
loading enquire.js with modernizr.js
/* Modernizr and Enquire make a great team when it comes to responsive design.
Modernizr checks for features and Enquire checkes for the current media query,
so you can always load the right stuff at the right time and keep your mobile
footprint nice and slim.
*/
// This snippet below would go into your app.js file. Modernizr.js needs to be called from your template before app.js is.
/* Pre-requisite files:
- Enquire.js http://wicky.nillia.ms/enquire.js
@namklabs
namklabs / untar-to-and-strip-dir.sh
Created April 18, 2013 16:53
untar a tar.gz file from one directory into another directory AND remove the containing folder of the untarred archive
# inspired from http://drupal.org/node/207095
tar -C /extract/archive/here/ -zxvf ~/Downloads/myarchive.tar.gz --strip 1
# tar is the command
# -C lets you choose what directory to put the untarred archive into.
# The target directory is specified just like any other command in shell.
# -zxvf does a bunch of stuff. Just use it, or take out the v because that makes the command display the filename of each file that is extracted.
# The second path is where your archive is located.
# --strip 1 removes the directory that will be containing your files. There is always a 'myarchive' folder around the files in a myarchive.tar.gz. This will prevent that. So, be sure to set the first path of the command to a dir that you want all your files to be dumped into!
@namklabs
namklabs / math-captcha.js
Created April 22, 2013 18:18
simple JavaScript math-based CAPTCHA
// This script should be loaded or embedded after the form.
// There must be an element with an ID of "answer" and a form with an ID of "myform".
// Beginners: you can change what IDs the script looks for by replacing all instances of #answer or #myform
function set_question(){
operation = Math.floor( Math.random() * 2 ); // 1 or 0
constant1 = Math.floor( Math.random() * 11 ); // 0 through 10
constant2 = Math.floor( Math.random() * 11 ); // 0 through 10
if( operation == 1 ){
operation_symbol = "+";
@namklabs
namklabs / index.html
Created October 5, 2013 05:50
Quick HTML5 template based on HTML5 Boilerplate
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
@namklabs
namklabs / copyright.js
Created October 30, 2013 21:48
Puts current year into a <span> in the footer of your website