Skip to content

Instantly share code, notes, and snippets.

View holloway's full-sized avatar

Matthew Holloway holloway

View GitHub Profile
@holloway
holloway / jsdom-styled-components.js
Created February 12, 2019 21:31
JSDOM Styled-Components/Emotion CSS Rules scraper/tester that implements sheet.insertRule
import jsdom from 'jsdom';
export const beforeParse = (window: any) => {
// Styled-Components doesn't write textNodes within ie `<style> .thing { font-weight: bold } </style>`
// instead it access the style element's `sheet` property and calls `insertRule`
// which is much faster, but that means it doesn't make innerText for us to scrape.
// These `insertRule` rules are available at `(theStyleElement).sheet.cssRules` and
// that's an array of `cssText` strings.
//
// See https://spectrum.chat/styled-components/help/why-is-the-inline-style-tag-empty~c43006f6-50d5-4d7d-857d-ca7fd6b68de3
/*!
* Bootstrap v4.0.0-beta (https://getbootstrap.com)
* Copyright 2011-2017 The Bootstrap Authors
* Copyright 2011-2017 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
@media print {
*,
*::before,
*::after {
/*!
* Bootstrap v4.0.0-beta (https://getbootstrap.com)
* Copyright 2011-2017 The Bootstrap Authors
* Copyright 2011-2017 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
@media print {
*,
*::before,
*::after {
## Cache your selectors
Don't do this:
```JavaScript
$("body").on("click", function() {
$("body").addClass("clicked");
});
```
Do this:
export function querySelectArray(selector, el = document) {
// Usage: fn(selector, document.body)
// Returns [el1, el2, ...]
return Array.prototype.slice.call(el.querySelectorAll(selector));
}
export function querySelectArrayByElementArray(selector, els, removeDuplicates=true){
// Usage: fn(selector, [document.body, document.head])
// Returns [elementFromBody1, elementFromBody2, elementFromHead1, ...]
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
/* An HTML template in 180 bytes (minified)
Features: Escapes HTML, accepts either strings or functions as values, and that's all (it doesn't handle looping).
Usage:
OneEightyT(
"<h1>{name}</h1><div>{content} @ {currentTime}</div>",
{
name: "Stella",
@holloway
holloway / gist:5e3c424194550dae7450
Created November 21, 2014 05:12
Extracting images from PDFs
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
pdf = open(sys.argv[1], "rb").read()
minimum_seek = 20
startfix = 0
endfix = 2
i = 0
var is = {
get_type: function(thing){
if(thing===null) return "[object Null]";
else if(thing!==thing) return "[object NaN]";
return Object.prototype.toString.call(thing);
},
compare: function(type){
return function(value){
return this.get_type(value) === "[object " + type + "]";
};
@holloway
holloway / get_type.js
Created May 8, 2014 05:22
get object type
var get_type = function(thing){
if(thing===null) return "[object Null]";
else if(thing!==thing) return "[object NaN]";
return Object.prototype.toString.call(thing);
};
get_type([1]) // "[object Array]"
get_type({a:1}) // "[object Object]"
get_type(1) // "[object Number]"
get_type("test") // "[object String]"