Skip to content

Instantly share code, notes, and snippets.

@tsileo
Created December 18, 2013 15:43
Show Gist options
  • Save tsileo/8024507 to your computer and use it in GitHub Desktop.
Save tsileo/8024507 to your computer and use it in GitHub Desktop.
(function() {
var getJSON = function(options, callback) {
var xhttp = new XMLHttpRequest();
options.url = options.url || location.href;
options.data = options.data || null;
callback = callback || function() {};
options.type = options.type || 'json';
var url = options.url;
xhttp.open('GET', options.url, true);
xhttp.send(options.data);
xhttp.onreadystatechange = function() {
if (xhttp.status == 200 && xhttp.readyState == 4) {
callback(xhttp.responseText);
}
};
};
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var callback = function(data) {
data = eval('('+data+')');
console.log(data);
$ref_node = document.querySelector('.article_meta');
stat = document.createElement('div');
stat.id = 'stat_ui';
widget_content = '<h3>Analytics</h3><div id="ui_content"></div>';
stat.innerHTML = widget_content;
insertAfter($ref_node, stat);
$ui_content = document.querySelector('#ui_content');
$ui_content.innerHTML = '<div><h4>Today</h4><p>Pageviews: <strong>' + data.today.total + '</strong></p></div>';
//$btc_system.innerHTML = content;
};
var api = 'http://host.com/api/post'
getJSON({'url': api+'?url=' + location.href}, callback);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment