Refactor popup.js and popup.html, improved logging

main
Elbert Filius 8 years ago
parent f5705171bb
commit 70ecdf0899

@ -5,25 +5,25 @@
init: function() {
var html = document.documentElement.outerHTML;
c.log('init');
c.log('Function call: init()');
if ( html.length > 50000 ) {
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();
},
log: function(message) {
browser.runtime.sendMessage({ id: 'log', message: '[ content.js ] ' + message });
browser.runtime.sendMessage({ id: 'log', message: message, source: 'content.js' });
},
getEnvironmentVars: function() {
var container, script;
c.log('getEnvironmentVars');
c.log('Function call: getEnvironmentVars()');
if ( typeof document.documentElement.innerHTML === 'undefined' ) {
return;
@ -48,7 +48,7 @@
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);
document.documentElement.appendChild(container);

@ -26,7 +26,7 @@
* Log messages to console
*/
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
*/
init: function() {
w.log('init');
w.log('Function call: w.driver.init()', 'driver');
// Load apps.json
var xhr = new XMLHttpRequest();
@ -122,8 +122,6 @@
}
browser.tabs.onRemoved.addListener(function(tabId) {
w.log('remove tab');
tabCache[tabId] = null;
});
@ -153,8 +151,6 @@
}
}
}
w.log(JSON.stringify({ uri: uri, headers: responseHeaders }));
}
}, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]);
@ -178,11 +174,13 @@
a = document.createElement('a');
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 ) {
case 'log':
w.log(message.message);
w.log(message.message, message.source);
break;
case 'analyze':
@ -295,8 +293,6 @@
if ( Object.keys(w.ping.hostnames).length && tracking ) {
w.driver.post('http://ping.wappalyzer.com/v2/', w.ping);
w.log('w.driver.ping: ' + JSON.stringify(w.ping));
w.ping = { hostnames: {} };
w.driver.post('https://ad.wappalyzer.com/log/wp/', w.adCache);
@ -318,7 +314,7 @@
xhr.onreadystatechange = function() {
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: browser */
document.addEventListener('DOMContentLoaded', function() {
var
slugify, popup,
d = document,
detectedApps = d.getElementById('detected-apps');
slugify = function(string) {
return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
};
popup = {
(function() {
var popup = {
init: function() {
var callback = function(tabs) {
if ( tabs[0].url.match(/https?:\/\//) ) {
detectedApps.innerHTML = '<div class="empty">' + browser.i18n.getMessage('noAppsDetected') + '</div>';
} else {
detectedApps.innerHTML = '<div class="empty">' + browser.i18n.getMessage('nothingToDo') + '</div>';
}
( chrome || browser ).runtime.sendMessage({ id: 'get_apps', tab: tabs[0], source: 'popup.js' }, function(response) {
if ( /complete|interacrive|loaded/.test(document.readyState) ) {
popup.displayApps(response)
} else {
document.addEventListener('DOMContentLoaded', function() { popup.displayApps(response) });
}
});
};
try {
@ -28,62 +21,56 @@ document.addEventListener('DOMContentLoaded', function() {
// Edge
browser.tabs.query({ active: true, currentWindow: true }, callback);
}
popup.displayApps();
},
displayApps: function() {
var appName, confidence, version;
displayApps: function(response) {
var
appName, confidence, version,
detectedApps = document.querySelector('#detected-apps');
html = '';
var callback = function(tabs) {
function sendGetApps(response) {
var html;
if ( response.tabCache && response.tabCache.count > 0 ) {
detectedApps.innerHTML = '';
if ( response.tabCache && response.tabCache.count > 0 ) {
for ( appName in response.tabCache.appsDetected ) {
confidence = response.tabCache.appsDetected[appName].confidenceTotal;
version = response.tabCache.appsDetected[appName].version;
for ( appName in response.tabCache.appsDetected ) {
confidence = response.tabCache.appsDetected[appName].confidenceTotal;
version = response.tabCache.appsDetected[appName].version;
html +=
'<div class="detected-app">' +
'<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 =
'<div class="detected-app">' +
'<a target="_blank" href="https://wappalyzer.com/applications/' + 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>';
response.apps[appName].cats.forEach(function(cat) {
html +=
'<a target="_blank" href="https://wappalyzer.com/categories/' + popup.slugify(response.categories[cat].name) + '">' +
'<span class="category"><span class="name">' + browser.i18n.getMessage('categoryName' + cat) + '</span></span>' +
'</a>';
});
response.apps[appName].cats.forEach(function(cat) {
html +=
'<a target="_blank" href="https://wappalyzer.com/categories/' + slugify(response.categories[cat].name) + '">' +
'<span class="category"><span class="name">' + browser.i18n.getMessage('categoryName' + cat) + '</span></span>' +
'</a>';
});
html +=
'</a>' +
'</div>';
}
} else {
html = '<div class="empty">' + browser.i18n.getMessage('noAppsDetected') + '</div>';
}
html +=
'</a>' +
'</div>';
detectedApps.innerHTML = html;
detectedApps.innerHTML = detectedApps.innerHTML + html;
}
}
}
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);
}
};
// Force redraw after popup animation on Mac OS
setTimeout(function() {
document.body.innerHTML += '<span style="line-height: 1px;"> </span>';
}, 600);
},
try {
// Chrome, Firefox
browser.tabs.query({ active: true, currentWindow: true }).then(callback);
} catch ( e ) {
// Edge
browser.tabs.query({ active: true, currentWindow: true }, callback);
}
slugify: function(string) {
return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
}
};
popup.init();
});
}());

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

@ -109,7 +109,7 @@ var wappalyzer = (function() {
*/
var driver = function(func, args) {
if ( typeof w.driver[func] !== 'function' ) {
w.log('not implemented: w.driver.' + func, 'warn');
w.log('not implemented: w.driver.' + func, 'core', 'warn');
return;
}
@ -154,7 +154,7 @@ var wappalyzer = (function() {
} catch (e) {
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: function(message, type) {
log: function(message, source, type) {
driver('log', {
source: source || '',
message: JSON.stringify(message),
type: type || 'debug'
});
@ -208,13 +209,13 @@ var wappalyzer = (function() {
* Initialize
*/
init: function() {
w.log('w.init');
w.log('Function call: w.init()', 'core');
// Initialize driver
if ( w.driver !== undefined ) {
driver('init');
} else {
w.log('No driver, exiting');
w.log('No driver, exiting', 'core');
}
},
@ -226,10 +227,10 @@ var wappalyzer = (function() {
app,
apps = {};
w.log('w.analyze');
w.log('Function call: w.analyze()', 'core');
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;
}
@ -279,7 +280,9 @@ var wappalyzer = (function() {
w.cacheDetectedApps(apps, url);
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');
},
@ -292,7 +295,7 @@ var wappalyzer = (function() {
// Exclude app in detected apps only
for ( app in apps ) {
if ( w.apps[app].excludes ) {
asArray(w.apps[app]).excludes.forEach(function(excluded) {
asArray(w.apps[app].excludes).forEach(function(excluded) {
excludes.push(excluded);
});
}
@ -325,7 +328,7 @@ var wappalyzer = (function() {
implied = parsePatterns(implied)[0];
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;
}