Skip to content

Instantly share code, notes, and snippets.

View alunny's full-sized avatar

Andrew Lunny alunny

  • Twitter
  • San Francisco, CA
View GitHub Profile
@alunny
alunny / uglyNoSuchMethod.js
Created December 10, 2009 18:15
noSuchMethod / methodMissing -ish interface in vanilla javascript
var Foo = function Foo() {
var someValue = 1;
this.noSuchMethodHandler(id, args) {
if (id == "increment") {
return someValue++;
}
}
};
Foo.prototype.exec = function(id,args) {
// courtesy https://twitter.com/tomdevdiary/status/8946331934
("foo" + + "bar") === "fooNaN" // true
@alunny
alunny / node-crockford.js
Created April 14, 2010 17:35
print out a crockford fact, just like that!
// using node 1.90
var sys = require('sys'), http = require('http');
var crockfordator = http.createClient(80, 'crockfordfacts.com');
var req = crockfordator.request('GET', '/',
{
'accept':'application/json',
'host':'crockfordfacts.com'
}
);
@alunny
alunny / file-api-sample.js
Created April 14, 2010 18:41
sample usage of the phonegap file api
var writeData = function (filename, data, callback) {
var writeOutData = function () {
var fw = new FileWriter();
fw.oncomplete = callback;
fw.onerror = function () {
alert("Failed to save update");
}
fw.writeAsText("myDir/" + filename, data);
}
@alunny
alunny / client-side-xauth.js
Created June 21, 2010 22:39
accessing twitter xAuth through client-side JS
/*****
To authorize on Twitter API through xAuth, you need HMAC-SHA1
I'm using the following lib for that:
http://jssha.sourceforge.net
Make sure you have sha.js included!
<script src="http://jssha.sourceforge.net/sha.js"></script>
Also, you need to email api@twitter.com to get xAuth access
I cannot do that for you - see http://dev.twitter.com/pages/xauth
@alunny
alunny / convert.rb
Created July 12, 2010 18:42
convert image with image_science
#! /usr/bin/ruby
# usage: ruby convert.rb icon.jpg png
require 'rubygems'
require 'image_science'
if ARGV.length == 2
source_file = ARGV.first
target_extension = ARGV.last
target_file = source_file.sub /[\w\d]+$/, target_extension
@alunny
alunny / appActive.m
Created July 26, 2010 23:50
appActive event for phonegap-iphone
// insert this into your AppDelegate.m file
// this will fire an "appActive" event on the webview every time the app is launched
// whether through fast-app switching or a cold launch
- (void)applicationDidBecomeActive:(UIApplication *)application {
// delay of 1ms ensures code will be executed in new stack trace
// that way, event listener can't block applicationDidBecomeActive
// and crash the app
NSString *fireActiveEvent = @"window.setTimeout(function() { \n"
"var appActive = document.createEvent('Events'); \n"
@alunny
alunny / xui-new-wrap.js
Created August 13, 2010 18:50
a stab at a new wrap function for xui
// private method
// Wraps the HTML in a TAG, Tag is optional
// If the html starts with a Tag, it will wrap the context in that tag.
// doesn't ACTUALLY work
function wrap(xhtml, tag) {
// new approach
// createDocumentFragment
var docFrag = document.createDocumentFragment(),
dummy,
wrapTag;
(higgins={bot:function(){return Math.random()>0.5?"it's just JavaScript":"Dojo already did it"}}).bot()
@alunny
alunny / fakegap.js
Created October 22, 2010 00:24
first (very rough) stab at faking touch events
document.addEventListener('click', function (evt) {
if (typeof evt.target.ontouchend == 'function') {
evt.target.ontouchend.call(evt.target, evt);
}
}, false);