Skip to content

Instantly share code, notes, and snippets.

@ninabreznik
Created February 15, 2016 05:16
Show Gist options
  • Save ninabreznik/a082740fae90774661bd to your computer and use it in GitHub Desktop.
Save ninabreznik/a082740fae90774661bd to your computer and use it in GitHub Desktop.
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var curl = require("minixhr")
var ETAG = "dfe1b5b52924c418f4eddcb293fb149f"
//var URL = 'https://api.github.com/users/ninabreznik'
var URL = "http://requestb.in/1comjar1"
var first = false
if (first)
curl({
url: URL
}, print)
else
curl({
url: URL,
header: {
"If-None-Match": ETAG
}
},print)
function print (data, _, __, header) {
var xmp = document.createElement("xmp")
xmp.innerHTML = JSON.stringify(JSON.parse(data),null,4) + "\n=======\n" + JSON.stringify(header,null,4)
document.body.appendChild(xmp)
}
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var xhrFactory=function getXHRfactory(factories){for(var i=0,xhr,X,len=factories.length;i<len;i++){try{X=factories[i];xhr=X();return window.XMLHttpRequest?X:window.XMLHttpRequest=X}catch(e){continue}}}([function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml3.")},function(){return new ActiveXObject("Msxml2.XMLHTTP.6.0")},function(){return new ActiveXObject("Msxml2.XMLHTTP.3.0")},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Microsoft.XMLHTTP")},function(){return null}]);module.exports=function getXHR(){return xhrFactory()}},{}],minixhr:[function(require,module,exports){var XMLHttpRequest=require("xhrpolyfill");module.exports=function xhr2(params,callback){var args={url:typeof params==="string"?params:params.url,method:params.method||params.data?"POST":"GET",body:params.data,headers:function(){var header={"X-Requested-With":"XMLHttpRequest","Content-Type":"application/x-www-form-urlencoded"};return params.headers?params.headers:params.body?header:{}}()};var xhr=XMLHttpRequest();if(!xhr){throw new Error("No AJAX support")}xhr.open(args.method,args.url);for(var field in args.headers){xhr.setRequestHeader(field,args.headers[field])}xhr.onload=xhr.onerror=function responseHandler(response){var headerJSON={},h=xhr.getAllResponseHeaders();(h.match(/([^\n\r:]+):([^\n\r]+)/g)||[]).forEach(function(item){var tmp=item.split(": ");headerJSON[tmp[0]]=tmp[1]});if(callback){callback(this.response,response,xhr,headerJSON)}};xhr.send(args.body||null)}},{xhrpolyfill:1}]},{},[]);var curl=require("minixhr");var ETAG="dfe1b5b52924c418f4eddcb293fb149f";var URL="http://requestb.in/1comjar1";var first=false;if(first)curl({url:URL},print);else curl({url:URL,header:{"If-None-Match":ETAG}},print);function print(data,_,__,header){var xmp=document.createElement("xmp");xmp.innerHTML=JSON.stringify(JSON.parse(data),null,4)+"\n=======\n"+JSON.stringify(header,null,4);document.body.appendChild(xmp)}
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"minixhr": "1.3.7"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment