Skip to content

Instantly share code, notes, and snippets.

View dtao's full-sized avatar

Dan Tao dtao

View GitHub Profile
@dtao
dtao / frp.example.js
Created May 5, 2014 15:58
Lazy.js jQuery Event Stream
window.addEventListener('load', function() {
// This 'createWrapper' method is super weird and awkward and I'm not proud of
// it at all. But right now it's the only option for defining custom sequence
// types that wrap arbitrary sources. In a future version I will come up with
// something much better, swear to God.
var jqueryWrapper = Lazy.createWrapper(function(selector, eventName) {
var source = this;
$(selector).on(eventName, function(e) {
source.emit(e);
});
@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 / 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 / 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);
@dtao
dtao / convert_to_markdown.rb
Last active December 20, 2015 06:59
A very basic script to translate a simple HTML file (consisting only of paragraphs with bold & italic text) to Markdown.
require 'nokogiri'
file = ARGV[0]
if !File.exist?(file)
puts "File #{file} does not exist."
exit
end
html = File.read(file)
@dtao
dtao / createGeneratorIdea.js
Created January 11, 2014 21:24
An idea for how generators could be implemented pre-ES6
/**
* Using yield to create a generator -- only possible in ES6/Harmony.
*/
function fibonacci(limit) {
var fn1 = 1;
var fn2 = 1;
while (1) {
var current = fn2;
fn2 = fn1;
fn1 = fn1 + current;
@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 / 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;