Skip to content

Instantly share code, notes, and snippets.

View prettycode's full-sized avatar

Chris prettycode

  • Seattle, WA
View GitHub Profile
// Working example: http://www.jsfiddle.net/mUrVB/22/
(function($) {
$.fn.selection = function() {
var start = this[0].selectionStart,
end = this[0].selectionEnd;
if (start === end) {
return null;
@prettycode
prettycode / js2xml.htm
Created April 16, 2013 05:16
JavaScript/JSON -> XHTML serializer.
<!DOCTYPE html>
<html xmlns:pretty="http://prettycode.org/xmlns/json2xml">
<head>
<title>Proof of Concept</title>
<script>
var XML = {
defaultNamespace: undefined,
stringify: function(ns, value, name) {
if (typeof ns === 'undefined') {
ns = this.defaultNamespace;
@prettycode
prettycode / js2markup.htm
Last active December 16, 2015 06:49
Generate HTML from JavaScript object literals.
<!DOCTYPE html>
<html>
<head>
<title>Proof of Concept</title>
<script>
// TODO how should Arrays be handled?
// TODO strict mode (no '\n' or padding) to allow for significant whitespace (e.g. for <pre>, <textarea>)
var toHTML = function(value, name, indent) {
var pad = new Array((indent = indent || 0)).join(' ');
@prettycode
prettycode / submitForm.js
Last active December 16, 2015 07:49
Use this function to submit an HTML form without actually having a form on the page. Instead of an existing form, pass the function a *flat* JavaScript object with the form values to submit. This is a synchronous operation; the browser loads the response as its new window location. Requests with "Content-Type: application/x-www-form-urlencoded".
var submitForm = function(config) {
if (!config) {
throw new Error();
}
else {
config.method = config.method || 'post';
config.data = config.data || {};
config.url = config.url || (function() {
throw new Error('Missing required `url` argument');
@prettycode
prettycode / example-tests.js
Last active December 16, 2015 07:49
Module loader for testing
var example = {
//js: 'https://gist.github.com/prettycode/5401842/raw/example.js'
js: 'http://fiddle.jshell.net/js/heyoffline.js?StillNoSpring'
}
loadScript(example.js, {
document: {
createElement: function(tag) {
return {
setAttribute: function(key, value) {
@prettycode
prettycode / fork-your-own-gist.js
Last active December 16, 2015 08:58
A function that forks a Gist. You can use this to fork your own Gists.
function forkGist(user, gist) {
var path = (arguments.length ? ('/' + user + '/' + gist) : location.pathname);
var form = document.createElement('form');
form.method = 'post';
form.action = 'https://gist.github.com' + path + '/fork',
form.submit();
}
// Paste into browser console when viewing a Gist (including your own) to fork it
forkGist();
@prettycode
prettycode / textarea-maxlength.ie.shim.js
Last active August 23, 2023 14:48
jQuery shim for supporting <textarea> "maxlength" attribute in IE < 10.
// jQuery shim for supporting <textarea> `maxlength` attribute in IE < 10
// Author: Chris O'Brien, prettycode.org
// License: MIT
(function ($) {
// Target only IE browsers that don't support `maxlength`
if (typeof document.selection === 'undefined' ||
'maxLength' in document.createElement('textarea')
@prettycode
prettycode / CryptoRandom.cs
Last active November 10, 2021 15:27
Cryptographically-strong random number generator in C#.
// Cryptographically-strong random number generator
// Source: MSDN Magazine > 2007 > September > .NET Matters: Tales from the CryptoRandom
// Source URL: http://msdn.microsoft.com/en-us/magazine/cc163367.aspx
// Authors: Stephen Toub & Shawn Farkas
public class CryptoRandom : Random
{
private RNGCryptoServiceProvider cryptoProvider = new RNGCryptoServiceProvider();
private byte[] uint32Buffer = new byte[4];
@prettycode
prettycode / toast.js
Last active December 16, 2015 23:59
Create toast with Windows.UI (Windows 8 JavaScript app).
// 1
(function(undefined) {
"use strict";
var Notifications = Windows.UI.Notifications;
var Xml = Windows.Data.Xml;
var ToastController = {
configToXML: function(config) {
config.version = config.version || "1";
@prettycode
prettycode / EventLogProgram.cs
Created May 8, 2013 02:43
Monitor for new Event Log entries. Basic proof-of-concept C# console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Security.Cryptography;
using System.Xml.Linq;
using System.Security;