Skip to content

Instantly share code, notes, and snippets.

@robertcdawson
robertcdawson / color_contrast_checker.js
Last active September 16, 2020 07:55
Quickly visually estimate color contrast ratio of text within or over images.
// Run in console:
const images = document.querySelectorAll('img');
images.forEach(image => {
image.style.filter = "blur(5px) grayscale(1)";
});
// Or, make a bookmarklet:
javascript:const images = document.querySelectorAll('img'); images.forEach(image => { image.style.filter = "blur(5px) grayscale(1)"; });
@robertcdawson
robertcdawson / index.html
Created July 21, 2017 05:33
Recursion 101 Recursive function example // source https://jsbin.com/putuluv
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Recursive function example">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Recursion 101</title>
</head>
<body>
// exercism.io exercise
// This function takes a date and returns a new date set one billion seconds (or gigasecond; ~31.558 years) in the future.
var Gigasecond = function(birthdate) {
this.birthdate = birthdate;
};
Gigasecond.prototype.date = function() {
var gigasecondBirthday = new Date( this.birthdate.setSeconds( this.birthdate.getSeconds() + Math.pow(10, 9) ) );
var gigasecondBirthdayMidnight = gigasecondBirthday.setHours( 0, 0, 0, 0 );
var getDate = new Date(gigasecondBirthdayMidnight);
// exercism.io exercise
var toRna = function (dna) {
'use strict';
var rnaMap = {
'G' : 'C',
'C' : 'G',
'T' : 'A',
'A' : 'U'
};
var rna = dna.replace(/G|C|T|A/gi, function(match){
// ref 1: http://toddmotto.com/mastering-the-module-pattern/
// ref 2: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillText
// create basic diagram module (w/ "revealing module" pattern from ref 1)
var D_ = (function() {
// draw container
var draw = function(label) {
var canvas = document.getElementById('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
@robertcdawson
robertcdawson / lazyload_nojs.html
Created April 15, 2015 23:42
Lazy-load images without JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Lazy Loading without JavaScript</title>
<style type="text/css">
/* Hide images on page load. */
#img1, #img2, #img3 {
display: none;
}
/* Show images when targeted. */
@robertcdawson
robertcdawson / gist:e9e49e30027e66c19b23
Created October 2, 2014 18:00
Tampermonkey script to close Amazon preview window with Esc key
// ==UserScript==
// @name Close Amazon Preview with Esc Key
// @namespace http://www.therobbiedshow.com/
// @version 0.1
// @description Close Amazon preview window when clicking the Esc key!
// @match http://www.amazon.com
// @include http://*/*
// @copyright 2014+, Robert Dawson
// @require http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==
@robertcdawson
robertcdawson / calories_burned.js
Last active August 29, 2015 14:02
Unminified script to get calories burned during exercise
/*
* Instructions:
* - Create a new bookmark and name it "Get Calories Burned!"
* - Then, paste the following code into the URL field.
* - At the prompt, enter your sex, age, weight, average heart rate during exercise, and exercise duration.
* - E.g.: m 38 150 150 30
* Data source: http://fitnowtraining.com/2012/01/formula-for-calories-burned/
*/
var getCalories = function() {
var questions = prompt("Enter (separated by spaces):\n- Sex\n- Age (yrs)\n- Weight (lbs)\n- Avg heart rate (rpm)\n- Workout time (min)");
@robertcdawson
robertcdawson / calories_burned_bookmarklet.js
Last active August 29, 2015 14:02
Bookmarklet to get calories burned
javascript:var getCalories=function(){var e=prompt("Enter (separated by spaces):\n- Sex\n- Age (yrs)\n- Weight (lbs)\n- Avg heart rate (rpm)\n- Workout time (min)");var t=e.split(" ");var n=t[0];var r=t[1];var i=t[2];var s=t[3];var o=t[4];var u=((r*.2017+i*.09036+s*.6309-55.0969)*(o/4.184)).toFixed(2);var a=((r*.074+i*.05741+s*.4472-20.4022)*(o/4.184)).toFixed(2);var f=n=="m"?u:a;return f};var setCalories=function(){alert("You burned "+getCalories()+" calories today!")}();