Skip to content

Instantly share code, notes, and snippets.

@guywithnose
Last active March 10, 2023 19:54
Show Gist options
  • Save guywithnose/4477976 to your computer and use it in GitHub Desktop.
Save guywithnose/4477976 to your computer and use it in GitHub Desktop.
TamperMonkey script to automatically authenticate with Nebulous. Currently it only works in Chrome with JSONView installed.
// ==UserScript==
// @name Nebulous OAuth
// @namespace autoForm
// @include https://apis.traderonline.com/*
// @include https://api-dev.traderonline.com/*
// @include http://apis.traderonline.com/*
// @include http://api-dev.traderonline.com/*
// @include http://nebulous.local/*
// @include http://nebulous.testing/*
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// @version 1
// ==/UserScript==
$(function() {
setTimeout(function() {
if ($('span.property:first').text() == 'error') {
reauthenticate();
}
}, 1000);
});
function reauthenticate()
{
$.ajax({
url: '/vLatest/token',
type: 'POST',
data: {
'client_id': '{CLIENT_ID}',
'client_secret': '{CLIENT_SECRET}',
'grant_type': 'client_credentials'
},
dataType: 'json',
success: function(data) {
var access_token = data.access_token;
$.ajax({
url: location.pathname + location.search,
cache: false,
headers: {
'Authorization': 'Bearer ' + access_token
},
complete: function(xhr, status) {
try {
data = $.parseJSON(xhr.responseText);
$('#json').html(JSONFormatter.prototype.jsonToHTML(data));
} catch (e) {
$('html').html('<pre>' + xhr.responseText + '</pre>');
}
}
});
}
});
}
/**
* This came from the JSONView plugin
*
* @author Benjamin Hollis
*/
function JSONFormatter() {
}
JSONFormatter.prototype = {
htmlEncode: function(t) {
return t ? t.toString().replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;') : '';
},
// Completely escape strings, taking care to return common escape codes to their short forms
jsString: function(s) {
// the JSON serializer escapes everything to the long-form \uXXXX
// escape code. This is a map of characters to return to the short-escaped
// form after escaping.
var has = {
'\b': 'b',
'\f': 'f',
'\r': 'r',
'\n': 'n',
'\t': 't'
}, ws;
for (ws in has) {
if (-1 === s.indexOf(ws)) {
delete has[ws];
}
}
// The old nsIJSON can't encode just a string...
s = JSON.stringify({a: s});
s = s.slice(6, -2);
for (ws in has) {
s = s.replace(new RegExp('\\\\u000' + (ws.charCodeAt().toString(16)), 'ig'),
'\\' + has[ws]);
}
return this.htmlEncode(s);
},
decorateWithSpan: function(value, className) {
return '<span class="' + className + '">' + this.htmlEncode(value) + '</span>';
},
// Convert a basic JSON datatype (number, string, boolean, null, object, array) into an HTML fragment.
valueToHTML: function(value) {
var valueType = typeof value;
var output = '';
if (value === null) {
output += this.decorateWithSpan('null', 'type-null');
}
else if (value && value.constructor == Array || value.constructor.toString().search('Array()') > 0) {
output += this.arrayToHTML(value);
}
else if (valueType == 'object') {
output += this.objectToHTML(value);
}
else if (valueType == 'number') {
output += this.decorateWithSpan(value, 'type-number');
}
else if (valueType == 'string') {
if (/^(http|https):\/\/[^\s]+$/i.test(value)) {
output += '<a href="' + value + '"><span class="q">"</span>' + this.jsString(value) + '<span class="q">"</span></a>';
} else {
output += '<span class="type-string">"' + this.jsString(value) + '"</span>';
}
}
else if (valueType == 'boolean') {
output += this.decorateWithSpan(value, 'type-boolean');
}
return output;
},
// Convert an array into an HTML fragment
arrayToHTML: function(json) {
var hasContents = false;
var output = '';
var numProps = 0;
for (var prop in json) {
numProps++;
}
for (var prop2 in json) {
hasContents = true;
output += '<li><div class="hoverable"><div class="collapser"></div>' + this.valueToHTML(json[prop2]);
if (numProps > 1) {
output += ',';
}
output += '</div></li>';
numProps--;
}
if (hasContents) {
output = '[<ul class="array collapsible">' + output + '</ul>]';
} else {
output = '[ ]';
}
return output;
},
// Convert a JSON object to an HTML fragment
objectToHTML: function(json) {
var hasContents = false;
var output = '';
var numProps = 0;
for (var prop in json) {
numProps++;
}
for (var prop3 in json) {
hasContents = true;
output += '<li><div class="hoverable"><div class="collapser"></div><span class="property"><span class="q">"</span>' + this.jsString(prop3) +
'<span class="q">"</span></span>: ' + this.valueToHTML(json[prop3]);
if (numProps > 1) {
output += ',';
}
output += '</div></li>';
numProps--;
}
if (hasContents) {
output = '{<ul class="obj collapsible">' + output + '</ul>}';
} else {
output = '{ }';
}
return output;
},
// Convert a whole JSON value / JSONP response into a formatted HTML document
jsonToHTML: function(json, callback, uri) {
var output = '<div id="json">' +
this.valueToHTML(json) +
'</div>';
if (callback) {
output = '<div class="callback">' + callback + '(</div>' +
output +
'<div class="callback">)</div>';
}
return this.toHTML(output, uri);
},
// Produce an error document for when parsing fails.
errorPage: function(error, data, uri) {
var stringbundle = this.getStringBundle();
// Escape unicode nulls
data = data.replace('\u0000', '\uFFFD');
var output = '<div id="error">' + stringbundle.GetStringFromName('errorParsing') + '</div>' +
'<h1>' + stringbundle.GetStringFromName('docContents') + ':</h1>' +
'<div id="json">' + this.htmlEncode(data) + '</div>';
return this.toHTML(output, uri + ' - Error');
},
// Wrap the HTML fragment in a full document. Used by jsonToHTML and errorPage.
toHTML: function(content, title) {
return '<!DOCTYPE html>\n' +
'<html><head><title>' + this.htmlEncode(title) + '</title>' +
'<link rel="stylesheet" type="text/css" href="chrome://jsonview/content/default.css">' +
'<script type="text/javascript" src="chrome://jsonview/content/default.js"></script>' +
'</head><body>' +
content +
'</body></html>';
}
};
@nubs
Copy link

nubs commented Jan 8, 2013

also take a look at https://github.com/dominionenterprises/tol-api-clients/tree/js for a client that we will be maintaining going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment