Completed
Push — master ( f16461...f45b8a )
by Elbert
01:14
created

src/drivers/webextension/js/popup.js   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 110

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 110
rs 8.2857

3 Functions

Rating   Name   Duplication   Size   Complexity  
B popup.displayApps 0 78 6
A popup.slugify 0 3 1
A popup.init 0 21 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/** global: chrome */
2
/** global: browser */
3
4
(function() {
5
	var popup = {
6
		init: function() {
7
			var callback = function(tabs) {
8
				( chrome || browser ).runtime.sendMessage({ id: 'get_apps', tab: tabs[0], source: 'popup.js' }, function(response) {
9
					if ( /complete|interacrive|loaded/.test(document.readyState) ) {
10
						popup.displayApps(response)
11
					} else {
12
						document.addEventListener('DOMContentLoaded', function() {
13
							popup.displayApps(response)
14
						});
15
					}
16
				});
17
			};
18
19
			try {
20
				// Chrome, Firefox
21
				browser.tabs.query({ active: true, currentWindow: true }).then(callback);
22
			} catch ( e ) {
23
				// Edge
24
				browser.tabs.query({ active: true, currentWindow: true }, callback);
25
			}
26
		},
27
28
		displayApps: function(response) {
29
			var
30
				appName, confidence, version,
31
				detectedApps = document.querySelector('#detected-apps'),
32
				categories = [],
0 ignored issues
show
Unused Code introduced by
The assignment to variable categories seems to be never used. Consider removing it.
Loading history...
33
				json = [];
34
35
			if ( response.tabCache && response.tabCache.count > 0 ) {
36
				for ( appName in response.tabCache.appsDetected ) {
37
					confidence = response.tabCache.appsDetected[appName].confidenceTotal;
38
					version    = response.tabCache.appsDetected[appName].version;
39
40
					categories = [];
41
42
					response.apps[appName].cats.forEach(function(cat) {
43
						categories.push(
44
							[
45
								'a', {
46
									target: '_blank',
47
									href: 'https://wappalyzer.com/categories/' + popup.slugify(response.categories[cat].name)
48
								}, [
49
									'span', {
50
										class: 'category'
51
									}, [
52
										'span', {
53
											class: 'name'
54
										},
55
										browser.i18n.getMessage('categoryName' + cat)
56
									]
57
								]
58
							]
59
						);
60
					});
61
62
					json.push(
63
						[
64
							'div', {
65
								class: 'detected-app'
66
							}, [
67
								'a', {
68
									target: '_blank',
69
									href: 'https://wappalyzer.com/applications/' + popup.slugify(appName)
70
								}, [
71
									'img', {
72
										src: 'images/icons/' + ( response.apps[appName].icon || 'default.svg' )
73
									}
74
								], [
75
									'span', {
76
										class: 'label'
77
									}, [
78
										'span', {
79
											class: 'name'
80
										},
81
									 	appName
82
									],
83
									( version ? ' ' + version : '' ) + ( confidence < 100 ? ' (' + confidence + '% sure)' : '' )
84
								]
85
							],
86
							categories
87
						]
88
					);
89
				}
90
			} else {
91
				json = [
92
					'div', {
93
						class: 'empty'
94
					},
95
					browser.i18n.getMessage('noAppsDetected')
96
				];
97
			}
98
99
			detectedApps.appendChild(jsonToDOM(json, document, {}));
100
101
			// Force redraw after popup animation on Mac OS
102
			setTimeout(function() {
103
				document.body.appendChild(jsonToDOM([ 'span', { style: 'line-height: 1px;' }], document, {}));
104
			}, 800);
105
		},
106
107
		slugify: function(string) {
108
			return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
109
		}
110
	};
111
112
	popup.init();
113
}());
114