| Conditions | 1 |
| Paths | 1 |
| Total Lines | 119 |
| 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 | popup.update([ 'p', {}, ' ' ], document, {}); |
||
| 8 | |||
| 9 | var callback = function(tabs) { |
||
| 10 | ( chrome || browser ).runtime.sendMessage({ id: 'get_apps', tab: tabs[0], source: 'popup.js' }, function(response) { |
||
| 11 | popup.update(popup.appsToDomTemplate(response)); |
||
| 12 | }); |
||
| 13 | }; |
||
| 14 | |||
| 15 | try { |
||
| 16 | // Chrome, Firefox |
||
| 17 | browser.tabs.query({ active: true, currentWindow: true }).then(callback); |
||
| 18 | } catch ( e ) { |
||
| 19 | // Edge |
||
| 20 | browser.tabs.query({ active: true, currentWindow: true }, callback); |
||
| 21 | } |
||
| 22 | }, |
||
| 23 | |||
| 24 | update: function(dom) { |
||
| 25 | if ( /complete|interacrive|loaded/.test(document.readyState) ) { |
||
| 26 | popup.replaceDom(dom); |
||
| 27 | } else { |
||
| 28 | document.addEventListener('DOMContentLoaded', function() { |
||
| 29 | popup.replaceDom(dom); |
||
| 30 | }); |
||
| 31 | } |
||
| 32 | }, |
||
| 33 | |||
| 34 | replaceDom: function(domTemplate) { |
||
| 35 | var body = document.body; |
||
| 36 | |||
| 37 | while ( body.firstChild ) { |
||
| 38 | body.removeChild(body.firstChild); |
||
| 39 | } |
||
| 40 | |||
| 41 | body.append(jsonToDOM(domTemplate, document, {})); |
||
| 42 | }, |
||
| 43 | |||
| 44 | appsToDomTemplate: function(response) { |
||
| 45 | var |
||
| 46 | appName, confidence, version, |
||
| 47 | categories = [], |
||
|
|
|||
| 48 | template = []; |
||
| 49 | |||
| 50 | if ( response.tabCache && response.tabCache.count > 0 ) { |
||
| 51 | for ( appName in response.tabCache.appsDetected ) { |
||
| 52 | confidence = response.tabCache.appsDetected[appName].confidenceTotal; |
||
| 53 | version = response.tabCache.appsDetected[appName].version; |
||
| 54 | categories = []; |
||
| 55 | |||
| 56 | response.apps[appName].cats.forEach(function(cat) { |
||
| 57 | categories.push( |
||
| 58 | [ |
||
| 59 | 'a', { |
||
| 60 | target: '_blank', |
||
| 61 | href: 'https://wappalyzer.com/categories/' + popup.slugify(response.categories[cat].name) |
||
| 62 | }, [ |
||
| 63 | 'span', { |
||
| 64 | class: 'category' |
||
| 65 | }, [ |
||
| 66 | 'span', { |
||
| 67 | class: 'name' |
||
| 68 | }, |
||
| 69 | browser.i18n.getMessage('categoryName' + cat) |
||
| 70 | ] |
||
| 71 | ] |
||
| 72 | ] |
||
| 73 | ); |
||
| 74 | }); |
||
| 75 | |||
| 76 | template.push( |
||
| 77 | [ |
||
| 78 | 'div', { |
||
| 79 | class: 'detected-app' |
||
| 80 | }, [ |
||
| 81 | 'a', { |
||
| 82 | target: '_blank', |
||
| 83 | href: 'https://wappalyzer.com/applications/' + popup.slugify(appName) |
||
| 84 | }, [ |
||
| 85 | 'img', { |
||
| 86 | src: 'images/icons/' + ( response.apps[appName].icon || 'default.svg' ) |
||
| 87 | } |
||
| 88 | ], [ |
||
| 89 | 'span', { |
||
| 90 | class: 'label' |
||
| 91 | }, [ |
||
| 92 | 'span', { |
||
| 93 | class: 'name' |
||
| 94 | }, |
||
| 95 | appName |
||
| 96 | ], |
||
| 97 | ( version ? ' ' + version : '' ) + ( confidence < 100 ? ' (' + confidence + '% sure)' : '' ) |
||
| 98 | ] |
||
| 99 | ], |
||
| 100 | categories |
||
| 101 | ] |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | template = [ |
||
| 106 | 'div', { |
||
| 107 | class: 'empty' |
||
| 108 | }, |
||
| 109 | browser.i18n.getMessage('noAppsDetected') |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | |||
| 113 | return template; |
||
| 114 | }, |
||
| 115 | |||
| 116 | slugify: function(string) { |
||
| 117 | return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, ''); |
||
| 118 | } |
||
| 119 | }; |
||
| 120 | |||
| 121 | popup.init(); |
||
| 122 | }()); |
||
| 123 |