Skip to content

Instantly share code, notes, and snippets.

View mathiasbynens's full-sized avatar

Mathias Bynens mathiasbynens

View GitHub Profile
@mathiasbynens
mathiasbynens / programming-is-like-doing-a-massive-sudoku.md
Created June 28, 2011 12:46
Why programming is like solving a massive sudoku, by @peterbraden

Ok, I came up with an example.

Programming is like doing a massive sudoku. But you’re not just doing your own square; you have to line up the edges with squares that you’ve already done, or squares other people in your team are working on.

And it’s not just squares that you’ve done — you have to anticipate the sudokus you’ll be doing days, weeks or months from now, and leave easy numbers at the edges so it isn’t impossible to do those squares.

And that’s why some programmers are so engrossed in it, and get all worked up, because they’re like “You left a 5 in the middle of the square, what kind of asshole does that, now I’m gonna have to line all my squares up with that.”

And then someone points out a bug, and you have to trace it back to the square it came from, and then redo that square without screwing up all the other ones.

@mathiasbynens
mathiasbynens / unsafeWindow.user.js
Created August 13, 2011 13:21
`unsafeWindow` polyfill (for use in user scripts)
// ==UserScript==
// @name Emulate `unsafeWindow` in browsers that don’t support it.
// ==/UserScript==
// http://mths.be/unsafewindow
window.unsafeWindow || (
unsafeWindow = (function() {
var el = document.createElement('p');
el.setAttribute('onclick', 'return window;');
return el.onclick();
@mathiasbynens
mathiasbynens / lolwat.js
Created August 18, 2011 10:48
Fun with v8’s Number#toString bug
var number = 0,
increment = 0.00000000000001, // smallest value that makes a difference
result,
matches,
match;
for (; number < 100; number += increment) {
result = number.toString(33);
matches = result.match(/[a-z]+/g) || [];
match = matches.indexOf('wtf');
@mathiasbynens
mathiasbynens / jquery.loadasync.js
Created September 6, 2011 09:44
Use jQuery to load scripts asynchronously
// Load scripts asynchronously
jQuery.loadAsync = function(url, callback) {
// Don't use $.getScript since it disables caching
jQuery.ajax({
'url': url,
'dataType': 'script',
'cache': true,
'success': callback || jQuery.noop
});
};
@mathiasbynens
mathiasbynens / jsfiddle-defaults.user.js
Created September 8, 2011 12:25
Userscript that enables sensible defaults for jsFiddle.
// ==UserScript==
// @name Sensible defaults for jsFiddle.
// @author Mathias Bynens <http://mathiasbynens.be/>
// @link http://mths.be/bde
// @match http://jsfiddle.net/*
// ==/UserScript==
// Ignore existing fiddles
if (window.location.pathname == '/') {
@mathiasbynens
mathiasbynens / unicodeEscape.js
Created September 26, 2011 19:50
Escape all characters in a string using both Unicode and hexadecimal escape sequences
// Ever needed to escape '\n' as '\\n'? This function does that for any character,
// using hex and/or Unicode escape sequences (whichever are shortest).
// Demo: http://mothereff.in/js-escapes
function unicodeEscape(str) {
return str.replace(/[\s\S]/g, function(character) {
var escape = character.charCodeAt().toString(16),
longhand = escape.length > 2;
return '\\' + (longhand ? 'u' : 'x') + ('0000' + escape).slice(longhand ? -4 : -2);
});
}
@mathiasbynens
mathiasbynens / hide-jimmy-wales.user.js
Created November 21, 2011 19:43
Userscript that hides Jimmy Wales on Wikipedia
// ==UserScript==
// @name Hide Jimmy Wales.
// @match http://*.wikipedia.org/*
// ==/UserScript==
(function() {
var banner = document.getElementById('siteNotice');
banner && banner.style.display = 'none';
}());
@mathiasbynens
mathiasbynens / wikipedia-remove-sopa-warning.user.js
Created January 18, 2012 07:35
Userscript that removes the SOPA overlay on English Wikipedia
// ==UserScript==
// @name Disable the SOPA overlay on English Wikipedia
// @author Mathias Bynens <http://mathiasbynens.be/>
// @match http://en.wikipedia.org/*
// ==/UserScript==
// http://mths.be/unsafewindow
window.unsafeWindow || (
unsafeWindow = (function() {
var el = document.createElement('p');
@mathiasbynens
mathiasbynens / md5-collision.js
Last active October 22, 2021 23:36
Verify the most famous MD5 collision example in JavaScript, using nothing but built-in Node libraries.
#!/usr/bin/env node
// Verify the most famous MD5 collision example in JavaScript, using nothing but
// built-in Node modules.
var crypto = require('crypto');
var ucs2encode = require('punycode').ucs2.encode;
var assert = require('assert');
var md5 = function(string) {
@mathiasbynens
mathiasbynens / jsonp.php
Last active June 25, 2016 02:03
Basic JSON/JSON-P service in PHP
<?php
// Prevent content sniffing attacks such as http://mths.be/bst.
header('X-Content-Type-Options: nosniff');
// Note: The user-provided callback name must be filtered to prevent attack
// vectors. This script simply removes any symbols other than `[a-zA-Z0-9$_]`
// from the input. Sadly, this blocks the use of some valid JavaScript
// identifiers, and also accepts a few invalid ones. See
// http://mathiasbynens.be/notes/javascript-identifiers for details.