Skip to content

Instantly share code, notes, and snippets.

View dtao's full-sized avatar

Dan Tao dtao

View GitHub Profile
@dtao
dtao / eachAsync.js
Created April 10, 2012 14:52
Function to asynchronously iterate over a collection
function eachAsync(collection, iterator, callback) {
var iterate = function(i) {
setTimeout(function() {
iterator(collection[i]);
if (i < collection.length) {
iterate(i + 1);
} else {
callback();
}
}, 0);
@dtao
dtao / userChrome.css
Created April 17, 2012 16:59
Custom userChrome.css file to increase the font size in Thunderbird's thread tree
#threadTree treechildren {
font-size: 16pt;
}
#threadTree treechildren:-moz-tree-row {
border-bottom: 1px solid #E1E1E1;
height: 20pt !important;
}
@dtao
dtao / throttle.js
Created May 17, 2012 20:17
Function to throttle a callback so that it only executes after a specified amount of time has elapsed since the last invocation
function throttle(callback, delay, before) {
var busy = false;
var hold = false;
function callAfterDelay(self, args) {
setTimeout(function() {
if (hold) {
hold = false;
callAfterDelay(self, args);
return;
@dtao
dtao / example.sublime-keymap
Last active December 19, 2017 22:58
Sublime Text plugin to replace selected text with random letters/numbers
[
{ "keys": ["ctrl+alt+r"], "command": "randomize" }
]
@dtao
dtao / increment_decrement.py
Created May 25, 2012 16:10
Sublime Text plugin to increment/decrement all selected numbers
import sublime
import sublime_plugin
class NumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
for region in selection:
try:
value = int(self.view.substr(region))
self.view.replace(edit, region, str(self.op(value)))
@dtao
dtao / expand_window.js
Created August 1, 2012 22:49
Function to automatically zoom in any website to fill available screen width
(function() {
function expand($) {
function text($element) {
return $.trim($element.clone().children().remove().end().text());
}
function isFixedOrFloating($element) {
try {
if ($element.css("position") === "fixed") {
return true;
@dtao
dtao / EvilStringHelper.cs
Created August 16, 2012 17:01
Devious C# class that makes .NET strings mutable without requiring the /unsafe compiler switch
public static class EvilStringHelper
{
private static readonly Action<string, int, char> _setChar;
private static readonly Action<string, int> _setLength;
static EvilStringHelper()
{
if (Environment.Version.Major < 4)
{
MethodInfo setCharMethod = typeof(string).GetMethod(
@dtao
dtao / FastString.cs
Created August 25, 2012 22:35
Proof of concept for an immutable string class optimized for concatenation with the + operator
using System;
using System.Text;
namespace FastStringConcatenation
{
public class FastString
{
const int DefaultCapacity = 32;
readonly StringBuilder buffer;
@dtao
dtao / disable_xml_parameter_parsing.rb
Last active December 11, 2015 00:09
Rails initializer to disable XML parameter parsing to protect against newly discovered exploit
# Disable XML parameter parsing completely as a stopgap measure until upgrading to Rails 2.3.16
# to defend against arbitrary code execution vulnerability; see:
# https://groups.google.com/forum/#!topic/rubyonrails-security/61bkgvnSGTQ/discussion
ActionController::Base.param_parsers.delete(Mime::XML)
@dtao
dtao / agent.js
Created May 21, 2013 16:55
How 'this' in JavaScript is like 'instance_eval' in Ruby
function Agent(name) {
this.name = name;
this.command = function(subordinate, description, instructions) {
console.log(this.name + " tells " + subordinate.name + ": '" + description + "'");
instructions.apply(subordinate);
};
this.getCoffee = function() {
var recipients = Array.prototype.slice.call(arguments);