Skip to content

Instantly share code, notes, and snippets.

@itsbalamurali
Created March 10, 2014 17:14
Show Gist options
  • Save itsbalamurali/9469442 to your computer and use it in GitHub Desktop.
Save itsbalamurali/9469442 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2011 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* Performs an XMLHttpRequest to Twitter's API to get trending topics.
*
* @param callback Function If the response from fetching url has a
* HTTP status of 200, this function is called with a JSON decoded
* response. Otherwise, this function is called with null.
*/
function fetchTwitterFeed(callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = JSON.parse(xhr.responseText);
callback(data);
} else {
callback(null);
}
}
}
// Note that any URL fetched here must be matched by a permission in
// the manifest.json file!
var url = 'https://gist.githubusercontent.com/itsbalamurali/9465080/raw/af362325fb307cb288b744bb28824bbad40e36b3/gistfile1.json';
xhr.open('GET', url, true);
xhr.send();
};
/**
* Parses text from Twitter's API and generates a bar with trending topics at
* the top of the current page
*
* @param data Object JSON decoded response. Null if the request failed.
*/
function onText(data) {
// Only render the bar if the data is parsed into a format we recognize.
if (data.trends) {
// Create the overlay at the top of the page and fill it with data.
var trends_dom = document.createElement('div');
var title_dom = document.createElement('strong');
title_dom.innerText = 'Trending on HackerNews';
trends_dom.appendChild(title_dom);
for (var key in data.trends) {
for (var i=0,trend; trend = data.trends[key][i]; i++) {
var link_dom = document.createElement('a');
link_dom.setAttribute('href', trend.url)
link_dom.innerText = trend.name;
link_dom.style.color = '#000';
trends_dom.appendChild(document.createTextNode(' '));
trends_dom.appendChild(link_dom);
}
break;
}
trends_dom.style.cssText = [
'background-color: #f6f6ee;',
'color: #000;',
'padding: 10px;',
'font: 14px Arial;'
].join(' ');
document.body.style.cssText = 'position: relative';
document.body.parentElement.insertBefore(trends_dom, document.body);
}
};
fetchTwitterFeed(onText);
/*
var trends_dom = document.createElement('div');
trends_dom.className = "divRss";
$('#divRss').FeedEk({
FeedUrl : 'https://news.ycombinator.com/rss',
MaxCount : 5,
ShowDesc : true,
//ShowPubDate:true,
DescCharacterLimit:100,
TitleLinkTarget:'_blank',
//DateFormat: 'MM/DD/YYYY',
//DateFormatLang:'en'
});
document.body.parentElement.insertBefore(trends_dom, document.body);
*/
/*
var trends_dom = document.createElement('div');
trends_dom.className = "divRss";
var $FeEDiV = $( "<img src='https://i.imgur.com/Q8037QQ.png'>" );
$("#divRss").append($FeEDiV);
$(document).ready(function() {
$('#divRss').stickySidebar();
});
document.body.parentElement.insertBefore(trends_dom, document.body);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment