Refactor popup.js and popup.html, improved logging

main
Elbert Filius 8 years ago
parent f5705171bb
commit 70ecdf0899

@ -5,25 +5,25 @@
init: function() { init: function() {
var html = document.documentElement.outerHTML; var html = document.documentElement.outerHTML;
c.log('init'); c.log('Function call: init()');
if ( html.length > 50000 ) { if ( html.length > 50000 ) {
html = html.substring(0, 25000) + html.substring(html.length - 25000, html.length); html = html.substring(0, 25000) + html.substring(html.length - 25000, html.length);
} }
browser.runtime.sendMessage({ id: 'analyze', subject: { html: html } }); browser.runtime.sendMessage({ id: 'analyze', subject: { html: html }, source: 'content.js' });
c.getEnvironmentVars(); c.getEnvironmentVars();
}, },
log: function(message) { log: function(message) {
browser.runtime.sendMessage({ id: 'log', message: '[ content.js ] ' + message }); browser.runtime.sendMessage({ id: 'log', message: message, source: 'content.js' });
}, },
getEnvironmentVars: function() { getEnvironmentVars: function() {
var container, script; var container, script;
c.log('getEnvironmentVars'); c.log('Function call: getEnvironmentVars()');
if ( typeof document.documentElement.innerHTML === 'undefined' ) { if ( typeof document.documentElement.innerHTML === 'undefined' ) {
return; return;
@ -48,7 +48,7 @@
environmentVars = environmentVars.split(' ').slice(0, 500); environmentVars = environmentVars.split(' ').slice(0, 500);
browser.runtime.sendMessage({ id: 'analyze', subject: { env: environmentVars } }); browser.runtime.sendMessage({ id: 'analyze', subject: { env: environmentVars }, source: 'content.js' });
}), true); }), true);
document.documentElement.appendChild(container); document.documentElement.appendChild(container);

@ -26,7 +26,7 @@
* Log messages to console * Log messages to console
*/ */
log: function(args) { log: function(args) {
console.log('[wappalyzer ' + args.type + '] ' + args.message); console.log('[wappalyzer ' + args.type + ']', '[' + args.source + ']', JSON.parse(args.message));
}, },
/** /**
@ -53,7 +53,7 @@
* Initialize * Initialize
*/ */
init: function() { init: function() {
w.log('init'); w.log('Function call: w.driver.init()', 'driver');
// Load apps.json // Load apps.json
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -122,8 +122,6 @@
} }
browser.tabs.onRemoved.addListener(function(tabId) { browser.tabs.onRemoved.addListener(function(tabId) {
w.log('remove tab');
tabCache[tabId] = null; tabCache[tabId] = null;
}); });
@ -153,8 +151,6 @@
} }
} }
} }
w.log(JSON.stringify({ uri: uri, headers: responseHeaders }));
} }
}, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]); }, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]);
@ -178,11 +174,13 @@
a = document.createElement('a'); a = document.createElement('a');
if ( typeof message.id != 'undefined' ) { if ( typeof message.id != 'undefined' ) {
w.log('message: ' + message.id); if ( message.id !== 'log' ) {
w.log('Message received from ' + message.source + ': ' + message.id, 'driver');
}
switch ( message.id ) { switch ( message.id ) {
case 'log': case 'log':
w.log(message.message); w.log(message.message, message.source);
break; break;
case 'analyze': case 'analyze':
@ -295,8 +293,6 @@
if ( Object.keys(w.ping.hostnames).length && tracking ) { if ( Object.keys(w.ping.hostnames).length && tracking ) {
w.driver.post('http://ping.wappalyzer.com/v2/', w.ping); w.driver.post('http://ping.wappalyzer.com/v2/', w.ping);
w.log('w.driver.ping: ' + JSON.stringify(w.ping));
w.ping = { hostnames: {} }; w.ping = { hostnames: {} };
w.driver.post('https://ad.wappalyzer.com/log/wp/', w.adCache); w.driver.post('https://ad.wappalyzer.com/log/wp/', w.adCache);
@ -318,7 +314,7 @@
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if ( xhr.readyState == 4 ) { if ( xhr.readyState == 4 ) {
w.log('w.driver.post: status ' + xhr.status + ' (' + url + ')'); w.log({ 'POST request': { url: url, status: xhr.status, data: data } }, 'driver');
} }
}; };

@ -1,24 +1,17 @@
/** global: chrome */ /** global: chrome */
/** global: browser */ /** global: browser */
document.addEventListener('DOMContentLoaded', function() { (function() {
var var popup = {
slugify, popup,
d = document,
detectedApps = d.getElementById('detected-apps');
slugify = function(string) {
return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
};
popup = {
init: function() { init: function() {
var callback = function(tabs) { var callback = function(tabs) {
if ( tabs[0].url.match(/https?:\/\//) ) { ( chrome || browser ).runtime.sendMessage({ id: 'get_apps', tab: tabs[0], source: 'popup.js' }, function(response) {
detectedApps.innerHTML = '<div class="empty">' + browser.i18n.getMessage('noAppsDetected') + '</div>'; if ( /complete|interacrive|loaded/.test(document.readyState) ) {
} else { popup.displayApps(response)
detectedApps.innerHTML = '<div class="empty">' + browser.i18n.getMessage('nothingToDo') + '</div>'; } else {
} document.addEventListener('DOMContentLoaded', function() { popup.displayApps(response) });
}
});
}; };
try { try {
@ -28,62 +21,56 @@ document.addEventListener('DOMContentLoaded', function() {
// Edge // Edge
browser.tabs.query({ active: true, currentWindow: true }, callback); browser.tabs.query({ active: true, currentWindow: true }, callback);
} }
popup.displayApps();
}, },
displayApps: function() { displayApps: function(response) {
var appName, confidence, version; var
appName, confidence, version,
detectedApps = document.querySelector('#detected-apps');
html = '';
var callback = function(tabs) { if ( response.tabCache && response.tabCache.count > 0 ) {
function sendGetApps(response) { for ( appName in response.tabCache.appsDetected ) {
var html; confidence = response.tabCache.appsDetected[appName].confidenceTotal;
version = response.tabCache.appsDetected[appName].version;
if ( response.tabCache && response.tabCache.count > 0 ) {
detectedApps.innerHTML = '';
for ( appName in response.tabCache.appsDetected ) { html +=
confidence = response.tabCache.appsDetected[appName].confidenceTotal; '<div class="detected-app">' +
version = response.tabCache.appsDetected[appName].version; '<a target="_blank" href="https://wappalyzer.com/applications/' + popup.slugify(appName) + '">' +
'<img src="images/icons/' + ( response.apps[appName].icon || 'default.svg' ) + '"/>' +
'<span class="label">' +
'<span class="name">' + appName + '</span>' +
( version ? ' ' + version : '' ) + ( confidence < 100 ? ' (' + confidence + '% sure)' : '' ) +
'</span>' +
'</a>';
html = response.apps[appName].cats.forEach(function(cat) {
'<div class="detected-app">' + html +=
'<a target="_blank" href="https://wappalyzer.com/applications/' + slugify(appName) + '">' + '<a target="_blank" href="https://wappalyzer.com/categories/' + popup.slugify(response.categories[cat].name) + '">' +
'<img src="images/icons/' + (response.apps[appName].icon || 'default.svg') + '"/>' + '<span class="category"><span class="name">' + browser.i18n.getMessage('categoryName' + cat) + '</span></span>' +
'<span class="label"><span class="name">' + appName + '</span>' + ( version ? ' ' + version : '' ) + ( confidence < 100 ? ' (' + confidence + '% sure)' : '' ) + '</span>' + '</a>';
'</a>'; });
response.apps[appName].cats.forEach(function(cat) { html +=
html += '</a>' +
'<a target="_blank" href="https://wappalyzer.com/categories/' + slugify(response.categories[cat].name) + '">' + '</div>';
'<span class="category"><span class="name">' + browser.i18n.getMessage('categoryName' + cat) + '</span></span>' + }
'</a>'; } else {
}); html = '<div class="empty">' + browser.i18n.getMessage('noAppsDetected') + '</div>';
}
html += detectedApps.innerHTML = html;
'</a>' +
'</div>';
detectedApps.innerHTML = detectedApps.innerHTML + html; // Force redraw after popup animation on Mac OS
} setTimeout(function() {
} document.body.innerHTML += '<span style="line-height: 1px;"> </span>';
} }, 600);
if (typeof chrome === "undefined") { },
browser.runtime.sendMessage({ id: 'get_apps', tab: tabs[0] }, sendGetApps);
} else {
chrome.runtime.sendMessage({ id: 'get_apps', tab: tabs[0] }, sendGetApps);
}
};
try { slugify: function(string) {
// Chrome, Firefox return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
browser.tabs.query({ active: true, currentWindow: true }).then(callback);
} catch ( e ) {
// Edge
browser.tabs.query({ active: true, currentWindow: true }, callback);
}
} }
}; };
popup.init(); popup.init();
}); }());

@ -4,7 +4,6 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="stylesheet" href="css/widgets.css">
<link rel="stylesheet" href="css/popup.css"> <link rel="stylesheet" href="css/popup.css">
<script src="js/browser-polyfill.js"></script> <script src="js/browser-polyfill.js"></script>

@ -109,7 +109,7 @@ var wappalyzer = (function() {
*/ */
var driver = function(func, args) { var driver = function(func, args) {
if ( typeof w.driver[func] !== 'function' ) { if ( typeof w.driver[func] !== 'function' ) {
w.log('not implemented: w.driver.' + func, 'warn'); w.log('not implemented: w.driver.' + func, 'core', 'warn');
return; return;
} }
@ -154,7 +154,7 @@ var wappalyzer = (function() {
} catch (e) { } catch (e) {
attrs.regex = new RegExp(); attrs.regex = new RegExp();
w.log(e + ': ' + attr, 'error'); w.log(e + ': ' + attr, 'error', 'core');
} }
} }
}); });
@ -197,8 +197,9 @@ var wappalyzer = (function() {
/** /**
* Log messages to console * Log messages to console
*/ */
log: function(message, type) { log: function(message, source, type) {
driver('log', { driver('log', {
source: source || '',
message: JSON.stringify(message), message: JSON.stringify(message),
type: type || 'debug' type: type || 'debug'
}); });
@ -208,13 +209,13 @@ var wappalyzer = (function() {
* Initialize * Initialize
*/ */
init: function() { init: function() {
w.log('w.init'); w.log('Function call: w.init()', 'core');
// Initialize driver // Initialize driver
if ( w.driver !== undefined ) { if ( w.driver !== undefined ) {
driver('init'); driver('init');
} else { } else {
w.log('No driver, exiting'); w.log('No driver, exiting', 'core');
} }
}, },
@ -226,10 +227,10 @@ var wappalyzer = (function() {
app, app,
apps = {}; apps = {};
w.log('w.analyze'); w.log('Function call: w.analyze()', 'core');
if ( w.apps === undefined || w.categories === undefined ) { if ( w.apps === undefined || w.categories === undefined ) {
w.log('apps.json not loaded, check for syntax errors'); w.log('apps.json not loaded, check for syntax errors', 'core');
return; return;
} }
@ -279,7 +280,9 @@ var wappalyzer = (function() {
w.cacheDetectedApps(apps, url); w.cacheDetectedApps(apps, url);
w.trackDetectedApps(apps, url, hostname, data.html); w.trackDetectedApps(apps, url, hostname, data.html);
w.log(Object.keys(apps).length + ' apps detected: ' + Object.keys(apps).join(', ') + ' on ' + url); if ( Object.keys(apps).length ) {
w.log(Object.keys(apps).length + ' apps detected: ' + Object.keys(apps).join(', ') + ' on ' + url, 'core');
}
driver('displayApps'); driver('displayApps');
}, },
@ -292,7 +295,7 @@ var wappalyzer = (function() {
// Exclude app in detected apps only // Exclude app in detected apps only
for ( app in apps ) { for ( app in apps ) {
if ( w.apps[app].excludes ) { if ( w.apps[app].excludes ) {
asArray(w.apps[app]).excludes.forEach(function(excluded) { asArray(w.apps[app].excludes).forEach(function(excluded) {
excludes.push(excluded); excludes.push(excluded);
}); });
} }
@ -325,7 +328,7 @@ var wappalyzer = (function() {
implied = parsePatterns(implied)[0]; implied = parsePatterns(implied)[0];
if ( !w.apps[implied.string] ) { if ( !w.apps[implied.string] ) {
w.log('Implied application ' + implied.string + ' does not exist', 'warn'); w.log('Implied application ' + implied.string + ' does not exist', 'core', 'warn');
return; return;
} }

Loading…
Cancel
Save