Skip to content

Instantly share code, notes, and snippets.

View trevordixon's full-sized avatar

Trevor Dixon trevordixon

  • Google
  • Zürich, Switzerland
View GitHub Profile
@trevordixon
trevordixon / Routes.js
Last active May 9, 2016 16:59
Very simple Express-like routing for PhantomJS
var Routes = (function() {
var _ = {}, ctor = function(){};
_.bind = function bind(func, context) {
var bound, args, slice = Array.prototype.slice;
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
var result = func.apply(self, args.concat(slice.call(arguments)));
@trevordixon
trevordixon / renderElement.js
Created July 25, 2012 14:37
Function to render only a particular element in PhantomJS
function renderElement(page, selector) {
var prevClipRect = page.clipRect;
page.clipRect = page.evaluate(function(selector) {
return document.querySelector(selector).getBoundingClientRect();
}, selector);
var pic = page.renderBase64('png');
page.clipRect = prevClipRect;
@trevordixon
trevordixon / csv.pegjs
Created August 15, 2012 19:28
Javascript CSV Parser generated by PEG.js
{
var separator = ',';
}
start
= comma
comma
= & { return separator = ','; } sv:sv { return sv; }
@trevordixon
trevordixon / xlsxParser.js
Created August 17, 2012 04:10
Parse an Excel xlsx file with javascript, jquery, and zip.js
/*
Relies on jQuery, underscore.js, Async.js (https://github.com/caolan/async), and zip.js (http://gildas-lormeau.github.com/zip.js).
Tested only in Chrome on OS X.
Call xlsxParser.parse(file) where file is an instance of File. For example (untested):
document.ondrop = function(e) {
var file = e.dataTransfer.files[0];
excelParser.parse(file).then(function(data) {
console.log(data);
doc.addPage
size: 'legal'
layout: 'landscape'
@trevordixon
trevordixon / httplog.sublime-macro
Last active December 14, 2015 06:39
Macro to parse that web server log.
[
{
"args":
{
"characters": "\""
},
"command": "insert"
},
{
"args": null,
@trevordixon
trevordixon / static.php
Last active December 14, 2015 08:59
PHP Script to find non-static methods being called as if they were static. Uses https://github.com/nikic/PHP-Parser.
<?php
require 'PHP-Parser/lib/bootstrap.php';
ini_set('memory_limit', '512M');
// Feeds the given file to the PHP parser to be parsed and analyzed
function analyze_file($path, $parser, $traverser, $visitor) {
$code = file_get_contents($path);
$visitor->setFile($path);
$stmts = $parser->parse($code);
$stmts = $traverser->traverse($stmts);
@trevordixon
trevordixon / faster.js
Created June 10, 2013 07:05
Speeding up browserify with a huge lib. Using https://github.com/ForbesLindesay/browserify-middleware with Express.
// Each of these takes just a few milliseconds
app.get('/js/main.js', browserify('./public/js/main.js', {
external: ['three'],
detectGlobals: false
}));
app.get('/js/three.js', browserify(['three'], {
noParse: ['three'], // doesn't parse to look for global vars OR calls to require; just includes as-is
cache: true, // instructs browser to cache, even in dev, because this won't be changing
@trevordixon
trevordixon / FlyControls.js
Last active November 16, 2016 14:45
Three.js FlyControls. Controls yaw, pitch, and roll. Modified http://threejs.org/examples/js/controls/FlyControls.js to obey gamepad controls and control rotation only.
/**
* @author James Baicoianu / http://www.baicoianu.com/
* Originally from http://threejs.org/examples/js/controls/FlyControls.js
* Simplified to only obey gamepad
*/
THREE.FlyControls = function(object) {
this.object = object;
// API
@trevordixon
trevordixon / git-reset-to-date
Created August 2, 2013 21:39
Resets multiple git repositories to the commit each would have been at at a given time.
#!/bin/bash
# Usage: git-reset-to-date date repo1 repo2 ...
# Example: git-reset-to-date "7/14/2013 14:00" repo1 repo2 repo3
require_clean_work_tree () {
# Update the index
git update-index -q --ignore-submodules --refresh
err=0