OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this | 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 * source code is governed by a BSD-style license that can be found in the | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 /** |
| 8 * Performs an XMLHttpRequest to Twitter's API to get trending topics. |
| 9 * |
| 10 * @param callback Function If the response from fetching url has a |
| 11 * HTTP status of 200, this function is called with a JSON decoded |
| 12 * response. Otherwise, this function is called with null. |
5 */ | 13 */ |
| 14 function fetchTwitterFeed(callback) { |
| 15 var xhr = new XMLHttpRequest(); |
| 16 xhr.onreadystatechange = function(data) { |
| 17 if (xhr.readyState == 4) { |
| 18 if (xhr.status == 200) { |
| 19 var data = JSON.parse(xhr.responseText); |
| 20 callback(data); |
| 21 } else { |
| 22 callback(null); |
| 23 } |
| 24 } |
| 25 } |
| 26 // Note that any URL fetched here must be matched by a permission in |
| 27 // the manifest.json file! |
| 28 var url = 'https://api.twitter.com/1/trends.json?exclude=hashtags'; |
| 29 xhr.open('GET', url, true); |
| 30 xhr.send(); |
| 31 }; |
6 | 32 |
7 /** | 33 /** |
8 * Parses text from Twitter's API and generates a bar with trending topics at | 34 * Parses text from Twitter's API and generates a bar with trending topics at |
9 * the top of the current page | 35 * the top of the current page |
| 36 * |
10 * @param data Object JSON decoded response. Null if the request failed. | 37 * @param data Object JSON decoded response. Null if the request failed. |
11 */ | 38 */ |
12 function onText(data) { | 39 function onText(data) { |
13 // Only render the bar if the data is parsed into a format we recognize. | 40 // Only render the bar if the data is parsed into a format we recognize. |
14 if (data.trends) { | 41 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. | 42 // Create the overlay at the top of the page and fill it with data. |
26 var trends_dom = document.createElement('div'); | 43 var trends_dom = document.createElement('div'); |
27 var title_dom = document.createElement('strong'); | 44 var title_dom = document.createElement('strong'); |
28 var text_dom = document.createTextNode(trend_names.join(', ')); | 45 title_dom.innerText = 'Topics currently trending on Twitter:'; |
29 title_dom.innerText = 'Topics currently trending on Twitter '; | |
30 trends_dom.appendChild(title_dom); | 46 trends_dom.appendChild(title_dom); |
31 trends_dom.appendChild(text_dom); | 47 for (var i=0,trend; trend = data.trends[i]; i++) { |
32 trends_dom.style.background = '#36b'; | 48 var link_dom = document.createElement('a'); |
33 trends_dom.style.color = '#fff'; | 49 link_dom.setAttribute('href', trend.url) |
34 trends_dom.style.padding = '10px'; | 50 link_dom.innerText = trend.name; |
35 trends_dom.style.position = 'relative'; | 51 link_dom.style.color = '#000'; |
36 trends_dom.style.zIndex = '123456'; | 52 trends_dom.appendChild(document.createTextNode(' ')); |
37 trends_dom.style.font = '14px Arial'; | 53 trends_dom.appendChild(link_dom); |
38 document.body.insertBefore(trends_dom, document.body.firstChild); | 54 } |
| 55 trends_dom.style.cssText = [ |
| 56 'background-color: #ffd700;', |
| 57 'background-image: -webkit-repeating-linear-gradient(' + |
| 58 '45deg, transparent, transparent 35px,' + |
| 59 'rgba(0,0,0,.1) 35px, rgba(0,0,0,.1) 70px);', |
| 60 'color: #000;', |
| 61 'padding: 10px;', |
| 62 'font: 14px Arial;' |
| 63 ].join(' '); |
| 64 document.body.style.cssText = 'position: relative'; |
| 65 document.body.parentElement.insertBefore(trends_dom, document.body); |
39 } | 66 } |
40 }; | 67 }; |
41 | 68 |
42 // Send a request to fetch data from Twitter's API to the background page. | 69 fetchTwitterFeed(onText); |
43 // Specify that onText should be called with the result. | |
44 chrome.extension.sendRequest({'action' : 'fetchTwitterFeed'}, onText); | |
OLD | NEW |