Skip to content

Instantly share code, notes, and snippets.

@clarkdave
clarkdave / parseAndModifyHtml.js
Created May 6, 2011 21:24
Using node.js to parse HTML with jsdom and modify it with jQuery
/**
* npm install jsdom
* npm install jquery
*/
var html = "<!doctype html><html><body><h1>Hello world!</h1></body></html>";
/* parse the html and create a dom window */
var window = require('jsdom').jsdom(html, null, {
// standard options: disable loading other assets
@clarkdave
clarkdave / vows-phantom.js
Created November 24, 2011 10:43
Testing with Vows and PhantomJS
var phantom = require('phantom'),
vows = require('vows'),
assert = require('assert');
// nesting tests inside phantom callback so we only
// have to create it once
phantom.create(function(ph) {
var get_page_result = function(url, fn, result) {
ph.createPage(function(page) {
@clarkdave
clarkdave / index.html
Created February 7, 2012 11:38
ZombieJS ignoring jQuery $.delegate
<!doctype html>
<html>
<head>
<title>ZombieJS test</title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
</head>
<body>
<a href='#' id='clicker1'>Clicker one</a>
<a href='#' id='clicker2'>Clicker two</a>
<a href='#' id='clicker3'>Clicker three</a>
@clarkdave
clarkdave / widget.js
Created October 28, 2012 03:14
widget stuff
function loadScript(url, fn) {
var script = document.createElement('script');
script.async = true;
script.src = '//' + url;
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(script, entry);
script.onload = script.onreadystatechange = function() {
var rdyState = script.readyState;
@clarkdave
clarkdave / pygments.css
Created March 8, 2013 16:51
pygments styles for clarkdave.net
code { background: #f8f8f8; }
code .hll { background-color: #ffffcc }
code .c { color: #8f5902; font-style: italic } /* Comment */
code .err { color: #a40000; border: 1px solid #ef2929 } /* Error */
code .g { color: #000000 } /* Generic */
code .k { color: #204a87; font-weight: bold } /* Keyword */
code .l { color: #000000 } /* Literal */
code .n { color: #000000 } /* Name */
code .o { color: #ce5c00; font-weight: bold } /* Operator */
code .x { color: #000000 } /* Other */
@clarkdave
clarkdave / database.rb
Last active March 12, 2018 04:21
A Chef recipe for creating EBS volumes. See: http://clarkdave.net/2013/04/managing-ebs-volumes-with-chef/
# The database recipe should be included by any server running a DB. It creates
# a /data directory and, if on EC2, will mount an EBS volume here
directory '/data' do
mode '0755'
end
if node[:app][:ec2] || node[:cloud][:provider] == 'ec2'
aws = data_bag_item('aws', 'main')
include_recipe 'aws'
@clarkdave
clarkdave / log-user-out-rails.coffee
Created May 12, 2013 12:56
Log user out in Rails via JS
$('.logout').on 'click', (e) ->
link = $(e.currentTarget)
csrf_token = $('meta[name="csrf-token"]').attr('content')
form = $('<form>')
form.hide()
form.attr('method', 'post').attr('action', link.attr('href'))
$("<input type='hidden' name='authenticity_token' value='#{csrf_token}'>").appendTo(form)
@clarkdave
clarkdave / toggle-popup.coffee
Created May 12, 2013 13:27
Toggle a popup menu and close it when clicking outside (example using Backbone)
toggleUserMenu: (e) ->
e.preventDefault() if e
container = @$('.user')
# remove body listener if present
$('body').off 'click.menu'
$('.menu', container).toggleClass('visible')
container.toggleClass('open')
@clarkdave
clarkdave / chef-insert-line-if-no-match.rb
Created June 13, 2013 11:12
[CHEF] Insert a line in a file if it doesn't already exist
line = '127.0.0.1 gateway.internal gateway'
file = Chef::Util::FileEdit.new('/etc/hosts')
file.insert_line_if_no_match(/#{line}/, line)
file.write_file
@clarkdave
clarkdave / zombie-post-request.js
Last active August 1, 2019 14:44
How to make arbitrary POST requests using Zombie
var browser = new Browser();
browser.visit('/hello', function() {
// do some checks
// I'm not sure if browser.resources is an official part of the Zombie API (I found it by searching the src)
// so be wary in case it changes
browser.resources.post(
'/authenticate?email=hello@example.com&password=' + auth_token,