Skip to content

Instantly share code, notes, and snippets.

@moodysalem
moodysalem / CSSInliner.java
Last active November 23, 2023 08:12
Attempt to inline CSS using Java HTML parsing library jsoup
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
@moodysalem
moodysalem / swapfile.config
Last active March 21, 2017 13:32
ebextensions swapfile creation script ec2 swap
commands:
000_dd:
test: test ! -e /swapfile
command: dd if=/dev/zero of=/swapfile bs=1M count=2048 && chmod 600 /swapfile
001_mkswap:
command: mkswap /swapfile
ignoreErrors: true
002_swapon:
command: swapon /swapfile
ignoreErrors: true
@moodysalem
moodysalem / rasterize-letter-pdf.js
Created March 2, 2016 16:24
Render a page to PDF using phantomjs 2.1
"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length !== 3) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');
console.log(' "800px*600px" window, clipped to 800x600');
@moodysalem
moodysalem / React Autoresize Textarea
Last active March 17, 2016 16:57
A React TextArea component that automatically resizes to its content
React.createClass({
displayName: 'Autoresizing TextArea',
propTypes: {
height: React.PropTypes.number.isRequired,
onChangeHeight: React.PropTypes.func.isRequired
},
getDefaultProps: function () {
return {
@moodysalem
moodysalem / promise-cancellable.js
Last active April 9, 2024 18:06
ES6 Cancellable Promise Wrapper
/**
* Returns a promise that has a cancelled method that will cause the callbacks not to fire when it resolves or rejects
* @param promise to wrap
* @returns new promise that will only resolve or reject if cancel is not called
*/
export default function cancellable(promise) {
var cancelled = false;
const toReturn = new Promise((resolve, reject) => {
promise.then(() => {
@moodysalem
moodysalem / queuePromises.jsx
Created November 7, 2016 21:11
JS function that calls a set of functions that generate promises (called generators) in batches and resolves when they have all completed
import _ from 'underscore';
/**
* Takes an array of generators, i.e. functions that return promises, and calls them such that there are only ever
* 5 requests that are waiting on responses. Returns a promise that resolves to all the resulting promises only after
* they have all been executed
*
* @param generators array of functions that
* @param batchSize max number of requests to execute at a time
* @param throttle how often the queue is checked for more requests to process
@moodysalem
moodysalem / react-portal.jsx
Last active November 10, 2016 21:02
Render children to a div tag at the end of the body
import React, { Children, PureComponent, PropTypes } from 'react';
import { unmountComponentAtNode, unstable_renderSubtreeIntoContainer } from 'react-dom';
// renders children at the end of the body
export default class Portal extends PureComponent {
static propTypes = {
children: PropTypes.node.isRequired
};
componentDidMount() {
@moodysalem
moodysalem / render-text.jsx
Created November 11, 2016 19:18
Render React component to text
import { render, unmountComponentAtNode } from 'react-dom';
export function renderText(component) {
const _el = document.createElement('div');
document.body.appendChild(_el);
render(component, _el);
const text = _el.innerText;
unmountComponentAtNode(_el);
document.body.removeChild(_el);
return text;
@moodysalem
moodysalem / render-component-to-print-html.jsx
Created November 22, 2016 16:09
Render a react component to an html document that shares all the links and styles for printing, and convert it to a data URI for window.open
import React, { PureComponent, PropTypes } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Base64 } from 'js-base64';
import Promise from 'bluebird';
const htmlDocTemplate = ({ links, pageStyles, body, title = 'Print' }) => (`
<html>
<head>
<title>${title}</title>
${links}
@moodysalem
moodysalem / PreventLeaveRoute.jsx
Created January 22, 2017 18:52
React HOC to prevent leaving a page
import React, { Component, PropTypes } from 'react';
/**
* Only ever render one of these per page
*
* TODO: improve with react-side-effect to better handle multiple instances of this being rendered at once so we can
* nest the component in forms
*/
export default class PreventLeaveRoute extends Component {
static contextTypes = {