Skip to content

Instantly share code, notes, and snippets.

@jaredly
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredly/ca1e4fd16fb3231dc3a1 to your computer and use it in GitHub Desktop.
Save jaredly/ca1e4fd16fb3231dc3a1 to your computer and use it in GitHub Desktop.
Playing in the JS Sandbox
{
"created": 1416954835215,
"modified": 1416954835215,
"content": "Playing in the JS Sandbox",
"children": [
{
"content": "Notification API",
"children": [
{
"content": "var notification = new Notification(\"Execution Finished\", {\n body: \"Time taken: 20:00\"\n})",
"children": [],
"created": 1416690203020,
"modified": 1416954835304,
"type": "ipython",
"language": "javascript",
"waiting": false,
"started": 1416690513269,
"session": "8eml0reb1jbwxs3i03ebysbrz0yiguyc",
"executed": "var notification = new Notification(\"Execution Finished\", {\n body: \"Time taken: 20:00\"\n})",
"finished": 1416690513269,
"display_collapsed": false,
"outputs": []
},
{
"content": "Notification.requestPermission()",
"children": [],
"created": 1416690254627,
"modified": 1416954835304,
"type": "ipython",
"language": "javascript",
"waiting": false,
"started": 1416690708104,
"session": "8eml0reb1jbwxs3i03ebysbrz0yiguyc",
"executed": "Notification.requestPermission()",
"finished": 1416690708105,
"display_collapsed": false,
"outputs": []
},
{
"content": "Notification.permission",
"children": [],
"created": 1416690389498,
"modified": 1416954835304,
"type": "ipython",
"language": "javascript",
"waiting": false,
"started": 1416690709721,
"session": "8eml0reb1jbwxs3i03ebysbrz0yiguyc",
"executed": "Notification.permission",
"finished": 1416690709721,
"display_collapsed": false,
"outputs": [
{
"text/plain": "\"granted\"",
"type": "output",
"suppressable": false
}
]
},
{
"content": "notification.close()",
"children": [],
"created": 1416690215979,
"modified": 1416954835305,
"type": "ipython",
"language": "javascript",
"waiting": false,
"started": 1416690517578,
"session": "8eml0reb1jbwxs3i03ebysbrz0yiguyc",
"executed": "notification.close()",
"finished": 1416690517580,
"display_collapsed": false,
"outputs": []
},
{
"content": "2+2",
"children": [],
"created": 1416691150826,
"modified": 1416954835305,
"type": "ipython",
"language": "javascript",
"waiting": false,
"started": 1416691235236,
"session": "739ub3goi5z4xb3odugydev5e89xhjbi",
"executed": "2+2",
"finished": 1416691235238,
"display_collapsed": false,
"outputs": [
{
"text/plain": "4",
"type": "output",
"suppressable": false
}
]
},
{
"content": "%% ww\nvar s = Date.now()\nwhile (Date.now() - s < 11000);\n10",
"children": [],
"created": 1416691152946,
"modified": 1416954835305,
"type": "ipython",
"language": "javascript",
"waiting": false,
"started": 1416691839307,
"session": "a6fsj93ste6m3lhkshn07166w303pnz1",
"executed": "%% ww\nvar s = Date.now()\nwhile (Date.now() - s < 11000);\n10",
"finished": 1416691850309,
"display_collapsed": false,
"outputs": [
{
"text/plain": "10",
"type": "output",
"suppressable": false
}
]
}
],
"created": 1416690197259,
"modified": 1416954835304,
"type": "base"
},
{
"content": "Github OAuth",
"children": [
{
"content": "OAuth Definitions",
"children": [
{
"content": "var chrs = '0123456789abcdefghijklmnopqrtsuvwxyz'\nfunction uuid(num) {\n num = num || 32\n var res = ''\n for (var i=0; i<num; i++) {\n res += chrs[parseInt(Math.random() * chrs.length)]\n }\n return res\n}",
"children": [],
"type": "ipython",
"finished": 1417247767750,
"executed": "var chrs = '0123456789abcdefghijklmnopqrtsuvwxyz'\nfunction uuid(num) {\n num = num || 32\n var res = ''\n for (var i=0; i<num; i++) {\n res += chrs[parseInt(Math.random() * chrs.length)]\n }\n return res\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247767655,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "function params(what) {\n if ('string' === typeof what) return parseParams(what)\n return Object.keys(what).map(function (name) {return name + '=' + encodeURIComponent(what[name])}).join('&');\n}",
"children": [],
"type": "ipython",
"finished": 1417247767768,
"executed": "function params(what) {\n if ('string' === typeof what) return parseParams(what)\n return Object.keys(what).map(function (name) {return name + '=' + encodeURIComponent(what[name])}).join('&');\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247767753,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "function parseParams(what) {\n var obj = {}\n what.split('&').forEach(function (part) {\n var subs = part.split('=')\n obj[subs[0]] = decodeURIComponent(subs.slice(1).join('='))\n })\n return obj\n}",
"children": [],
"type": "ipython",
"finished": 1417247767779,
"executed": "function parseParams(what) {\n var obj = {}\n what.split('&').forEach(function (part) {\n var subs = part.split('=')\n obj[subs[0]] = decodeURIComponent(subs.slice(1).join('='))\n })\n return obj\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247767769,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "/**\n * Expect an external window to be done sometime soon\n */\nfunction waitForWindow(window, initial, step, done) {\n if (arguments.length === 2) {\n done = initial\n initial = 500\n step = 100\n }\n setTimeout(function () {\n var check = setInterval(function () {\n try {\n var m = window.location.search;\n } catch (e) {\n return;\n }\n clearInterval(check)\n done()\n }, step)\n }, initial)\n}",
"children": [],
"type": "ipython",
"finished": 1417248394487,
"executed": "/**\n * Expect an external window to be done sometime soon\n */\nfunction waitForWindow(window, initial, step, done) {\n if (arguments.length === 2) {\n done = initial\n initial = 500\n step = 100\n }\n setTimeout(function () {\n var check = setInterval(function () {\n try {\n var m = window.location.search;\n } catch (e) {\n return;\n }\n clearInterval(check)\n done()\n }, step)\n }, initial)\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417248394461,
"display_collapsed": false,
"outputs": [],
"modified": 1417248394453
},
{
"content": "function firstStep(CONFIG, rand, done) {\n var url = CONFIG.authorize + '?' + params({\n client_id: CONFIG.client_id,\n scope: 'gist',\n redirect_uri: CONFIG.redirect_uri,\n state: JSON.stringify({\n client_id: CONFIG.client_id,\n rand: rand,\n oauth_proxy: CONFIG.proxy,\n redirect_uri: CONFIG.redirect_uri,\n oauth: {\n version : 2,\n auth : 'https://github.com/login/oauth/authorize',\n grant : 'https://github.com/login/oauth/access_token',\n response_type : 'code'\n }\n })\n })\n \n var created = window.open(url, 'Github Auth', 'width=400,height=400')\n waitForWindow(created, function () {\n done(created, params(created.location.search.slice(1)))\n })\n}",
"children": [],
"type": "ipython",
"finished": 1417248653958,
"executed": "function firstStep(CONFIG, rand, done) {\n var url = CONFIG.authorize + '?' + params({\n client_id: CONFIG.client_id,\n scope: 'gist',\n redirect_uri: CONFIG.redirect_uri,\n state: JSON.stringify({\n client_id: CONFIG.client_id,\n rand: rand,\n oauth_proxy: CONFIG.proxy,\n redirect_uri: CONFIG.redirect_uri,\n oauth: {\n version : 2,\n auth : 'https://github.com/login/oauth/authorize',\n grant : 'https://github.com/login/oauth/access_token',\n response_type : 'code'\n }\n })\n })\n \n var created = window.open(url, 'Github Auth', 'width=400,height=400')\n waitForWindow(created, function () {\n done(created, params(created.location.search.slice(1)))\n })\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417248653910,
"display_collapsed": false,
"outputs": [],
"modified": 1417248653899
},
{
"content": "function secondStep(window, searchParams, done) {\n var state = JSON.parse(searchParams.state);\n searchParams.redirect_uri = state.redirect_uri;\n var path = state.oauth_proxy + \"?\" + params(searchParams);\n window.location.assign(path);\n \n waitForWindow(window, function () {\n setTimeout(function () {\n done(params(window.location.hash.slice(1)))\n }, 10);\n })\n}",
"children": [],
"type": "ipython",
"finished": 1417248655150,
"executed": "function secondStep(window, searchParams, done) {\n var state = JSON.parse(searchParams.state);\n searchParams.redirect_uri = state.redirect_uri;\n var path = state.oauth_proxy + \"?\" + params(searchParams);\n window.location.assign(path);\n \n waitForWindow(window, function () {\n setTimeout(function () {\n done(params(window.location.hash.slice(1)))\n }, 10);\n })\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417248655118,
"display_collapsed": false,
"outputs": [],
"modified": 1417248648150
},
{
"content": "function authenticate(CONFIG, done) {\n var rand = uuid()\n firstStep(CONFIG, rand, function (created, params) {\n // console.log('first done', params)\n secondStep(created, params, function (params) {\n // console.log('second dome', params)\n created.close()\n done(params, created)\n })\n })\n}",
"children": [],
"type": "ipython",
"finished": 1417248671770,
"executed": "function authenticate(CONFIG, done) {\n var rand = uuid()\n firstStep(CONFIG, rand, function (created, params) {\n // console.log('first done', params)\n secondStep(created, params, function (params) {\n // console.log('second dome', params)\n created.close()\n done(params, created)\n })\n })\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417248671735,
"display_collapsed": false,
"outputs": [],
"modified": 1417248671728
}
],
"type": "base",
"collapsed": false,
"modified": 1417247879903
},
{
"content": "Gisting",
"children": [
{
"content": "function post(url, headers, data, done) {\n var x = new XMLHttpRequest()\n x.open('POST', url)\n for (var name in headers) {\n x.setRequestHeader(name, headers[name])\n }\n x.send(JSON.stringify(data))\n x.onreadystatechange = function () {\n if (this.readyState === 4) {\n done(JSON.parse(this.responseText))\n }\n }\n}",
"children": [],
"type": "ipython",
"finished": 1417247775662,
"executed": "function post(url, headers, data, done) {\n var x = new XMLHttpRequest()\n x.open('POST', url)\n for (var name in headers) {\n x.setRequestHeader(name, headers[name])\n }\n x.send(JSON.stringify(data))\n x.onreadystatechange = function () {\n if (this.readyState === 4) {\n done(JSON.parse(this.responseText))\n }\n }\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247775643,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "function patch(url, headers, data, done) {\n var x = new XMLHttpRequest()\n x.open('PATCH', url)\n for (var name in headers) {\n x.setRequestHeader(name, headers[name])\n }\n x.send(JSON.stringify(data))\n x.onreadystatechange = function () {\n if (this.readyState === 4) {\n done(JSON.parse(this.responseText))\n }\n }\n}",
"children": [],
"type": "ipython",
"finished": 1417247778999,
"executed": "function patch(url, headers, data, done) {\n var x = new XMLHttpRequest()\n x.open('PATCH', url)\n for (var name in headers) {\n x.setRequestHeader(name, headers[name])\n }\n x.send(JSON.stringify(data))\n x.onreadystatechange = function () {\n if (this.readyState === 4) {\n done(JSON.parse(this.responseText))\n }\n }\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247778987,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "function newGist(access_token, description, files, done) {\n // create a gist out of the current document :D\n post('https://api.github.com/gists', {\n 'Authorization': 'token ' + access_token,\n }, {\n description: description,\n public: true,\n files: files\n }, function (result) {\n done && done(result)\n })\n}",
"children": [],
"type": "ipython",
"finished": 1417247782225,
"executed": "function newGist(access_token, description, files, done) {\n // create a gist out of the current document :D\n post('https://api.github.com/gists', {\n 'Authorization': 'token ' + access_token,\n }, {\n description: description,\n public: true,\n files: files\n }, function (result) {\n done && done(result)\n })\n}",
"output": [],
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"error": null,
"language": "javascript",
"waiting": false,
"started": 1417247782213,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "function upGist(access_token, id, description, files, done) {\n // create a gist out of the current document :D\n post('https://api.github.com/gists/' + id, {\n 'Authorization': 'token ' + access_token,\n }, {\n description: description,\n public: true,\n files: files\n }, function (result) {\n done && done(result)\n })\n}",
"children": [],
"type": "ipython",
"finished": 1417247783555,
"executed": "function upGist(access_token, id, description, files, done) {\n // create a gist out of the current document :D\n post('https://api.github.com/gists/' + id, {\n 'Authorization': 'token ' + access_token,\n }, {\n description: description,\n public: true,\n files: files\n }, function (result) {\n done && done(result)\n })\n}",
"output": [],
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"error": null,
"language": "javascript",
"waiting": false,
"started": 1417247783549,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "function saveToGist(access_token, store, done) {\n var files = {\n 'Document.nm': {\n content: JSON.stringify(store.db.exportTree(), null, 2)\n }\n }\n var description = store.db.nodes[store.db.root].content\n if (!store._globals.gistId) {\n newGist(access_token, description, files, function (result) {\n store._globals.gistId = result.id\n done && done()\n })\n } else {\n upGist(access_token, store._globals.gistId, description, files, done)\n }\n}",
"children": [],
"type": "ipython",
"finished": 1417247786960,
"executed": "function saveToGist(access_token, store, done) {\n var files = {\n 'Document.nm': {\n content: JSON.stringify(store.db.exportTree(), null, 2)\n }\n }\n var description = store.db.nodes[store.db.root].content\n if (!store._globals.gistId) {\n newGist(access_token, description, files, function (result) {\n store._globals.gistId = result.id\n done && done()\n })\n } else {\n upGist(access_token, store._globals.gistId, description, files, done)\n }\n}",
"output": [],
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"error": null,
"language": "javascript",
"waiting": false,
"started": 1417247786946,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
}
],
"type": "base",
"collapsed": true,
"modified": 1417247966595
},
{
"content": "Messing Around",
"children": [
{
"content": "var CONFIG = {\n authorize: 'https://github.com/login/oauth/authorize',\n proxy: 'https://auth-server.herokuapp.com/proxy',\n redirect_uri: parent.location.origin + '/connect.html',\n client_id: 'a15ba5cf761a832d0b25',\n}",
"children": [],
"type": "ipython",
"finished": 1417247790195,
"executed": "var CONFIG = {\n authorize: 'https://github.com/login/oauth/authorize',\n proxy: 'https://auth-server.herokuapp.com/proxy',\n redirect_uri: parent.location.origin + '/connect.html',\n client_id: 'a15ba5cf761a832d0b25',\n}",
"output": [],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247790188,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
},
{
"content": "var rand = uuid();rand",
"children": [],
"type": "ipython",
"finished": 1417247911837,
"executed": "var rand = uuid();rand",
"output": [
{
"text/plain": "\"nzhuq32zzple6wyub3xuzx7v9ga8ql1s\"",
"value": "nzhuq32zzple6wyub3xuzx7v9ga8ql1s"
}
],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417247911818,
"display_collapsed": false,
"outputs": [
{
"text/plain": "\"pcm2ue8nvagkmvfm2xzxxz22hn86m9n1\"",
"type": "output",
"suppressable": false
}
],
"modified": 1417247911837
},
{
"content": "// authenticate with github for gists\nauthenticate(CONFIG, function (data, win) {\n console.log('Authorized!');\n window.access_token = data.access_token\n});",
"children": [],
"type": "ipython",
"finished": 1417248673963,
"executed": "// authenticate with github for gists\nauthenticate(CONFIG, function (data, win) {\n console.log('Authorized!');\n window.access_token = data.access_token\n});",
"output": [
{
"text/plain": "undefined"
}
],
"error": null,
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"language": "javascript",
"waiting": false,
"started": 1417248673947,
"display_collapsed": false,
"outputs": [
{
"text/plain": "[\n \"Authorized!\"\n]",
"type": "output",
"suppressable": false
}
],
"modified": 1417248678794
},
{
"content": "parent.store._globals.gistId = 'ca1e4fd16fb3231dc3a1'",
"children": [],
"type": "ipython",
"finished": 1417248689423,
"executed": "parent.store._globals.gistId = 'ca1e4fd16fb3231dc3a1'",
"output": [
{
"text/plain": "\"ca1e4fd16fb3231dc3a1\"",
"value": "ca1e4fd16fb3231dc3a1"
}
],
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"error": null,
"language": "javascript",
"waiting": false,
"started": 1417248689417,
"display_collapsed": false,
"outputs": [
{
"text/plain": "\"ca1e4fd16fb3231dc3a1\"",
"type": "output",
"suppressable": false
}
],
"modified": 1417248689423
},
{
"content": "display({\n text: \"View gist\",\n href: \"http://gist.github.com/\" + parent.store._globals.gistId,\n title: \"This Gist\"\n}, 'json/link')",
"children": [],
"type": "ipython",
"finished": 1415512161322,
"executed": "display({\n text: \"View gist\",\n href: \"http://gist.github.com/\" + parent.store._globals.gistId,\n title: \"This Gist\"\n}, 'json/link')",
"output": [
{
"__treed_display": true,
"json/link": {
"text": "View gist",
"href": "http://gist.github.com/ca1e4fd16fb3231dc3a1",
"title": "This Gist"
}
}
],
"session": "xj0yy8le4k87qs235qlxa65aciirkpei",
"error": null,
"language": "javascript",
"waiting": false,
"started": 1415512161321,
"display_collapsed": false,
"outputs": [
{
"text/plain": "{\n \"text\": \"View gist\",\n \"href\": \"http://gist.github.com/ca1e4fd16fb3231dc3a1\",\n \"title\": \"This Gist\"\n}",
"json/link": {
"text": "View gist",
"href": "http://gist.github.com/ca1e4fd16fb3231dc3a1",
"title": "This Gist"
},
"type": "output",
"suppressable": false
}
],
"modified": 1416954835305
},
{
"content": "saveToGist(access_token, parent.store, function () {\n console.log('Done!')\n})",
"children": [],
"type": "ipython",
"finished": null,
"executed": "saveToGist(access_token, parent.store, function () {\n console.log('Done!')\n})",
"output": [
{
"text/plain": "undefined"
}
],
"session": "xk9so8uppnv9fdl7qhz0yj538sy8pzb3",
"error": null,
"language": "javascript",
"waiting": false,
"started": 1417248714334,
"display_collapsed": false,
"outputs": [],
"modified": 1416954835305
}
],
"type": "base",
"collapsed": false,
"modified": 1417248686143
}
],
"collapsed": false,
"modified": 1416954835305
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment