Completed
Push — master ( 0083cf...5b9113 )
by Elbert
41s
created

src/drivers/webextension/js/popup.js (2 issues)

1
/** global: chrome */
2
/** global: browser */
3
4
var pinnedCategory = null;
5
6
var func = tabs => {
7
  ( chrome || browser ).runtime.sendMessage({
8
    id: 'get_apps',
9
    tab: tabs[0],
10
    source: 'popup.js'
11
  }, response => {
12
    pinnedCategory = response.pinnedCategory;
13
14
    replaceDomWhenReady(appsToDomTemplate(response));
15
  });
16
};
17
18
browser.tabs.query({ active: true, currentWindow: true })
19
  .then(func)
20
  .catch(console.error);
21
22
function replaceDomWhenReady(dom) {
23
  if ( /complete|interactive|loaded/.test(document.readyState) ) {
24
    replaceDom(dom);
25
  } else {
26
    document.addEventListener('DOMContentLoaded', () => {
27
      replaceDom(dom);
28
    });
29
  }
30
}
31
32
function replaceDom(domTemplate) {
33
  var container = document.getElementsByClassName('container')[0];
34
35
  while ( container.firstChild ) {
36
    container.removeChild(container.firstChild);
37
  }
38
39
  container.appendChild(jsonToDOM(domTemplate, document, {}));
40
41
  var nodes = document.querySelectorAll('[data-i18n]');
42
43
  Array.prototype.forEach.call(nodes, node => {
44
    node.childNodes[0].nodeValue = browser.i18n.getMessage(node.dataset.i18n);
45
  });
46
47
  Array.from(document.querySelectorAll('.detected__category-pin-wrapper')).forEach(pin => {
48
    pin.addEventListener('click', event => {
0 ignored issues
show
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
49
      const categoryId = parseInt(pin.dataset.categoryId, 10);
50
51
      if ( categoryId === pinnedCategory ) {
52
        pin.className = 'detected__category-pin-wrapper';
53
54
        pinnedCategory = null;
55
      } else {
56
        const active = document.querySelector('.detected__category-pin-wrapper--active');
57
58
        if ( active ) {
59
          active.className = 'detected__category-pin-wrapper';
60
        }
61
62
        pin.className = 'detected__category-pin-wrapper detected__category-pin-wrapper--active';
63
64
        pinnedCategory = categoryId;
65
      }
66
67
      ( chrome || browser ).runtime.sendMessage({
68
        id: 'set_option',
69
        key: 'pinnedCategory',
70
        value: pinnedCategory,
71
      });
72
    });
73
  });
74
}
75
76
function appsToDomTemplate(response) {
77
  var
78
    appName, confidence, version, categories,
79
    template = [];
80
81
  if ( response.tabCache && Object.keys(response.tabCache.detected).length > 0 ) {
82
    const categories = {};
83
84
    // Group apps by category
85
    for ( appName in response.tabCache.detected ) {
86
      response.apps[appName].cats.forEach(cat => {
87
        categories[cat] = categories[cat] || { apps: [] };
88
89
        categories[cat].apps[appName] = appName;
90
      });
91
    }
92
93
    for ( cat in categories ) {
0 ignored issues
show
The variable cat seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.cat.
Loading history...
94
      const apps = [];
95
96
      for ( appName in categories[cat].apps ) {
97
        confidence = response.tabCache.detected[appName].confidenceTotal;
98
        version    = response.tabCache.detected[appName].version;
99
100
        apps.push(
101
          [
102
            'a', {
103
              class: 'detected__app',
104
              target: '_blank',
105
              href: 'https://www.wappalyzer.com/technologies/' + slugify(appName)
106
            }, [
107
              'img', {
108
                class: 'detected__app-icon',
109
                src: '../images/icons/' + ( response.apps[appName].icon || 'default.svg' )
110
              },
111
            ], [
112
              'span', {
113
                class: 'detected__app-name'
114
              },
115
              appName,
116
            ], version ? [
117
              'span', {
118
                class: 'detected__app-version'
119
              },
120
              version
121
            ] : null, confidence < 100 ? [
122
              'span', {
123
                class: 'detected__app-confidence'
124
              },
125
              confidence + '% sure'
126
            ] : null
127
          ]
128
        );
129
      }
130
131
      template.push(
132
        [
133
          'div', {
134
            class: 'detected__category'
135
          }, [
136
            'div', {
137
              class: 'detected__category-name'
138
            }, [
139
              'a', {
140
                class: 'detected__category-link',
141
                target: '_blank',
142
                href: 'https://www.wappalyzer.com/categories/' + slugify(response.categories[cat].name)
143
              },
144
              browser.i18n.getMessage('categoryName' + cat),
145
            ], [
146
              'span', {
147
                class: 'detected__category-pin-wrapper' + ( pinnedCategory == cat ? ' detected__category-pin-wrapper--active' : '' ),
148
                'data-category-id': cat,
149
                'title': browser.i18n.getMessage('categoryPin'),
150
              }, [
151
                'img', {
152
                  class: 'detected__category-pin detected__category-pin--active',
153
                  src: '../images/pin-active.svg'
154
                },
155
              ], [
156
                'img', {
157
                  class: 'detected__category-pin detected__category-pin--inactive',
158
                  src: '../images/pin.svg'
159
                }
160
              ]
161
            ]
162
          ], [
163
            'div', {
164
              class: 'detected__apps'
165
            },
166
            apps
167
          ]
168
        ]
169
      );
170
    }
171
172
    template = [
173
      'div', {
174
        class: 'detected'
175
      },
176
      template
177
    ];
178
  } else {
179
    template = [
180
      'div', {
181
        class: 'empty'
182
      },
183
      [
184
        'span', {
185
          class: 'empty__text'
186
        },
187
        browser.i18n.getMessage('noAppsDetected')
188
      ],
189
    ];
190
  }
191
192
  return template;
193
}
194
195
function slugify(string) {
196
  return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
197
}
198