Skip to content

Instantly share code, notes, and snippets.

@dvdbng
dvdbng / gist:4db30f4e3f4c9ad69d5b
Created June 15, 2015 05:25
Find regexp of valid attribute names
var valid_first = [];
var valid_second = [];
for (var i = 0, len = 0xFFFF; i < len; i++) {
try{
var char = String.fromCharCode(i);
if(char.toLowerCase() != char.toUpperCase() && valid_second.indexOf(char.toLowerCase().charCodeAt(0)) >= 0){
continue;
}
document.body.setAttribute('a' + char, '');
document.body.removeAttribute('a' + char);
@dvdbng
dvdbng / enigma.py
Created July 20, 2015 16:25
Number of possible Enigma machine combinations
# Number of possible starting configurations of an enigma machine
letters = 26
rotors_total = 5
rotors_machine = 3
cables = 10 # Must be < letters/2
def f(n): # Factorial
s = 1;
for i in xrange(2, n+1):
s *= i
;(function(){
function makeStorage(){
var keys = [];
var storage = Object.create(Object, {
getItem: {value: function(k) {
if(this.hasOwnProperty(k)) {
return this[k];
}
}},
@dvdbng
dvdbng / pruneTree.py
Created February 6, 2012 19:30
Prune DOM tree keeping structure and nodes for wich a function is true
import xml.etree.cElementTree as etree
def traverseEtree(tree,traverser):
traverser.start(tree)
traverser.data(tree.text)
for child in tree:
traverseEtree(child,traverser)
traverser.end(tree)
traverser.data(tree.tail)
@dvdbng
dvdbng / headers.lua
Created October 30, 2015 08:04
Splash normalize headers
-- x-FOO-bAr -> X-Foo-Bar
function normalize_header(name)
name = upper(sub(name, 1, 1)) .. lower(sub(name, 2))
while true do
local start,fin = find(name, '-[a-z]')
if start ~= nil then
name = sub(name, 1, start) .. upper(sub(name, fin, fin)) .. sub(name, fin+1)
else
break
end
@dvdbng
dvdbng / xpath.js
Created October 30, 2015 08:05
Query XPath
function queryXPath(expr, ctx, type){
ctx = ctx || document;
type = type || XPathResult.ANY_TYPE;
var doc = ctx.nodeType === Node.DOCUMENT_NODE ? ctx : ctx.ownerDocument;
var nsResolver = doc.createNSResolver(doc.documentElement);
var arr = [], i = null;
try {
expr = ("" + expr).replace(/\bhasClass\(["'](\w+)["']\)/, function(expr, className){
return "contains(concat(' ', normalize-space(@class), ' '), ' " + className + " ')";
});
function getAllProperties(obj){
var allProps = Object.getOwnPropertyNames(obj);
Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).forEach(function add(prop){
if (allProps.indexOf(prop) === -1)
allProps.push(prop)
});
return allProps
}
function jsrecursivegrep(obj, value, length){
/**
* Levenshtein distance between two arrays or strings
*/
function arrDistance(a, b) {
if(a.length === 0) { return b.length; }
if(b.length === 0) { return a.length; }
var matrix = [];
// increment along the first column of each row
# Download every episode of How It's Made from Youtube
# Needs: youtube-dl
urlencode() {
python -c "import sys, urllib; print urllib.quote_plus(sys.stdin.read())"
}
dl(){
q=`echo How its made $2 $3 $4 | urlencode`
youtube-dl --max-downloads=1 -o "HowItsMade_${1}_%(title)s_%(format)s.%(ext)s" "https://www.youtube.com/results?search_query=$q"
from collections import Counter
import heapq
class SummaryDict(Counter):
"""
A counting dict that aggregates uncommon values in a "Others" key.
The full set of keys is kept in a separate counter, in case they need to
be added back to the main counter.