popup.html (1520B)
1 <html> 2 <style> 3 body { 4 width: 500px; 5 font-size: 16px; 6 } 7 ul { 8 margin: 20px; 9 padding: 0; 10 } 11 li { 12 margin-bottom: 10px; 13 } 14 </style> 15 <body> 16 ... 17 <script> 18 var getPageUrl = function (callback) { 19 chrome.tabs.getSelected(null, function (tab) { callback(tab.url); }); 20 }; 21 var getRedditLinks = function (url, callback) { 22 var xhr = new XMLHttpRequest(); 23 xhr.open( 24 'GET', 25 'http://www.reddit.com/api/info.json?url=' + encodeURIComponent(url) 26 ); 27 xhr.onload = function () { 28 var response = JSON.parse(xhr.responseText); 29 callback(response.data.children); 30 }; 31 xhr.send(null); 32 }; 33 var displayLinks = function (links, callback) { 34 if (links.length == 0) { 35 document.body.textContent = 'nada'; 36 return; 37 } 38 var ul = document.createElement('ul'); 39 links.forEach(function (link) { 40 var a = document.createElement('a'); 41 a.href = 'http://www.reddit.com' + link.data.permalink; 42 a.textContent = link.data.title; 43 a.onclick = function () { 44 callback(a.href); 45 return false; 46 }; 47 var li = document.createElement('li'); 48 li.appendChild(a); 49 ul.appendChild(li); 50 }); 51 document.body.appendChild(ul); 52 document.body.removeChild(document.body.firstChild); 53 }; 54 getPageUrl(function (url) { 55 getRedditLinks(url, function (links) { 56 displayLinks(links, function (url) { chrome.tabs.create({ url: url }); }); 57 }); 58 }); 59 </script> 60 </body> 61 </html>