document.addEventListener('DOMContentLoaded', function() { var d = document, analyzeHeaders = d.getElementById('analyze-headers'), detectedApps = d.getElementById('detected-apps') ; var popup = { pollHeaders: null, init: function() { d.getElementById('options').addEventListener('click', function() { window.open(chrome.extension.getURL('options.html')); }); analyzeHeaders.innerHTML = chrome.i18n.getMessage('analyzeHeaders'); analyzeHeaders.removeAttribute('disabled'); chrome.tabs.getSelected(null, function(tab) { if ( tab.url.match(/https?:\/\//) ) { detectedApps.innerHTML = '
' + chrome.i18n.getMessage('noAppsDetected') + '
'; analyzeHeaders.addEventListener('click', function() { analyzeHeaders.setAttribute('disabled', 'disabled'); chrome.extension.sendRequest({ id: 'fetch_headers', tab: tab }); popup.pollHeaders = setInterval(popup.displayApps, 100); }); if ( parseInt(localStorage['autoAnalyzeHeaders']) ) { analyzeHeaders.click(); } } else { detectedApps.innerHTML = '
' + chrome.i18n.getMessage('nothingToDo') + '
'; analyzeHeaders.setAttribute('disabled', 'disabled'); } }); popup.displayApps(); }, displayApps: function() { var appName, confidence, version; chrome.tabs.getSelected(null, function(tab) { chrome.extension.sendRequest({ id: 'get_apps', tab: tab }, function(response) { if ( response.tabCache.analyzed.indexOf('headers') > 0 ) { if ( popup.pollHeaders != null ) { clearTimeout(popup.pollHeaders); analyzeHeaders.innerHTML = chrome.i18n.getMessage('analyzeHeadersDone'); } } if ( response.tabCache.count > 0 ) { detectedApps.innerHTML = ''; for ( appName in response.tabCache.appsDetected ) { confidence = response.tabCache.appsDetected[appName].confidenceTotal; version = response.tabCache.appsDetected[appName].version; html = '
' + '' + '' + '' + appName + ( version ? ' ' + version : '' ) + ( confidence < 100 ? ' (' + confidence + '% sure)' : '' ) + '' + ''; response.apps[appName].cats.forEach(function(cat) { html += '' + '' + chrome.i18n.getMessage('categoryName' + cat) + '' + ''; }); html += '' + '
'; detectedApps.innerHTML = detectedApps.innerHTML + html; } } }); }); } }; popup.init(); });