1/*
2 * Copyright (c) 2010 The Chromium Authors. All rights reserved.  Use of this
3 * source code is governed by a BSD-style license that can be found in the
4 * LICENSE file.
5 */
6
7/**
8 * Parses text from Twitter's API and generates a bar with trending topics at
9 * the top of the current page
10 * @param data Object JSON decoded response.  Null if the request failed.
11 */
12function onText(data) {
13  // Only render the bar if the data is parsed into a format we recognize.
14  if (data.trends) {
15    var trend_names = []
16    for (var date in data.trends) {
17      if (data.trends.hasOwnProperty(date)) {
18        var trends = data.trends[date];
19        for (var i=0,trend; trend = trends[i]; i++) {
20          trend_names.push(trend.name);
21        }
22      }
23    }
24
25    // Create the overlay at the top of the page and fill it with data.
26    var trends_dom = document.createElement('div');
27    var title_dom = document.createElement('strong');
28    var text_dom = document.createTextNode(trend_names.join(', '));
29    title_dom.innerText = 'Topics currently trending on Twitter ';
30    trends_dom.appendChild(title_dom);
31    trends_dom.appendChild(text_dom);
32    trends_dom.style.background = '#36b';
33    trends_dom.style.color = '#fff';
34    trends_dom.style.padding = '10px';
35    trends_dom.style.position = 'relative';
36    trends_dom.style.zIndex = '123456';
37    trends_dom.style.font = '14px Arial';
38    document.body.insertBefore(trends_dom, document.body.firstChild);
39  }
40};
41
42// Send a request to fetch data from Twitter's API to the background page.
43// Specify that onText should be called with the result.
44chrome.extension.sendRequest({'action' : 'fetchTwitterFeed'}, onText);
45