Skip to content

Instantly share code, notes, and snippets.

View mathiasbynens's full-sized avatar

Mathias Bynens mathiasbynens

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / jsconsole.user.js
Created June 24, 2011 13:51
User script to enable the large side-by-side view on jsconsole.com.
// ==UserScript==
// @name Enable the large side-by-side view on jsconsole.com. This is only useful until https://github.com/remy/jsconsole/issues/22 is fixed.
// @author Mathias Bynens <http://mathiasbynens.be/>
// @match http://jsconsole.com/*
// ==/UserScript==
document.body.className = 'large';