1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5var PERMISSIONS = {origins: ['http://api.stackoverflow.com/']}; 6var URL = 'http://api.stackoverflow.com/1.1/questions?max=10&sort=votes&tagged=google-chrome-extension'; 7var ROOT = 'http://stackoverflow.com'; 8 9chrome.permissions.contains(PERMISSIONS, function(result) { 10 if (!result) { 11 // Open options page to request permissions. 12 document.querySelector('#title').innerText = 13 'Requires Stack Overflow permission'; 14 chrome.tabs.create({url: 'options.html'}); 15 } else { 16 // Make the request to SO. 17 makeRequest(function(data) { 18 // Render the results. 19 renderQuestions(JSON.parse(data)); 20 }); 21 } 22}); 23 24function makeRequest(callback) { 25 var xhr = new XMLHttpRequest(); 26 xhr.open('GET', URL); 27 xhr.addEventListener('load', function(e) { 28 var result = xhr.responseText; 29 callback(result); 30 }); 31 xhr.send(); 32} 33 34function renderQuestions(data) { 35 var $results = document.querySelector('#results'); 36 var questions = data.questions; 37 for (var i = 0; i < Math.min(10, questions.length); i++) { 38 var question = questions[i]; 39 var $question = document.createElement('li'); 40 var url = ROOT + question.question_answers_url; 41 $question.innerHTML = '<a href="' + url + '" target="_blank">' + 42 question.title + '</a>'; 43 results.appendChild($question); 44 } 45 // Update title too. 46 document.querySelector('#title').innerText = 'Top Chrome Extension Questions'; 47} 48