Category: Code

Jun2

 

Time and time again people would send me perfectly idiotic code, and when I asked why they had done it that way the answer was not that they were idiots, but that there was some issue I had not appreciated, some problem they were trying to solve that was not apparent. Not to say that the solutions were not inept, or badly engineered, or just plain wrong. But there is a difference between a solution that is inept and one that is utterly insane. These appeared at first to be insane, but on investigation turned out to be sane but clumsy.

Mark Dominus

Jan19

 

Mock geolocation

Useful if you’re working on a website meant for mobile devices. Firefox 3.5 has geolocation, but I use this with Safari and GreaseKit. Replace with whatever latitude and longitude you prefer, naturally.

if (!('geolocation' in navigator)) {
  navigator.geolocation = {
    watchPosition: function(success, f, options) {
      var broadcast = function() {
        var position = {
          coords: {
            latitude: 30.2696384,
            longitude: -97.74947,
            accuracy: 10000,
          },
          timestamp: (new Date()).valueOf()
        };
        success(position);
      };

      broadcast();
      window.setInterval(broadcast, 10000);
    }
  };
}

Oct7

 

So I was in Mountain View last week, helping the Mozilla Labs team re-architect Bespin. Toward the end of the week, as we were doing some collaborative coding, I opened a terminal and typed j bespin to bring me straight to my checkout of the Bespin source — and Kevin and Joe simultaneously went, “Wait, what’d you just do?” I realized I’d been using autojump — the cd substitute that guesses what you mean — for over a year, and hadn’t yet pimped it to anyone. Use it! You’ll wonder where it’s been all your life.

Nov4

 

Auto-format Tweets

Used on my “About” page.

(function() {  
  var USERNAMES = /@([A-Za-z0-9_]*)\b/;
  var URLS      = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/;
  
  function getInnerText(element) {
    element = $(element);
    return element.innerText && !window.opera ? element.innerText :
     element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
  }
  
  function linkifyTweet(li) {
    var html = li.innerHTML, text = getInnerText(li);

    text.scan(URLS, function(match) {
      html = html.sub(match[0], '<a href="#{0}">#{0}');
    });    
    html = html.gsub(USERNAMES, '<a href="http://twitter.com/#{1}/">#{0}');
    li.update(html);
  }
  
  function init() {
    $('twitter').select('li > span.tweet').each(linkifyTweet);
  }  

  document.observe('dom:loaded', init);  
})();

Jan17

 

Code: Prototype Auto-build TextMate Command

Bind to ⌘S. Will execute rake dist automatically if you just saved a file in the src directory.

#!/usr/bin/env ruby

# Save: Current File
# Input: None
# Output: Discard
# Scope: source.js.prototype

FILE = ENV['TM_FILEPATH']
DIR  = File.dirname(FILE)
if (DIR =~ /\/src$/)
  basedir = DIR.gsub(/\/src$/, '')
  Dir.chdir(basedir)
  `/usr/local/bin/rake dist -f #{basedir}/Rakefile`
end

Sep9

 

Viewport Dimensions in JavaScript

Adapted from the functions posted on QuirksMode.

var Client = {
  viewportWidth: function() {
    return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
  },

  viewportHeight: function() {
    return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
  },

  viewportSize: function() {
    return { width: this.viewportWidth(), height: this.viewportHeight() };
  }
};

Painfully Obvious was built with WordPress, Prototype, Slicehost, and other accoutrements. Colophon →