| Conditions | 1 |
| Paths | 1 |
| Total Lines | 110 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | /** global: chrome */ |
||
| 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 = [], |
||
|
|
|||
| 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 |