Completed
Push — master ( 3d8ed9...b1b851 )
by Elbert
01:22 queued 26s
created

driver.js ➔ getPageContent   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 27
rs 8.8571

1 Function

Rating   Name   Duplication   Size   Complexity  
A driver.js ➔ ... ➔ ??? 0 1 1
1
/**
2
 * Bookmarklet driver
3
 */
4
5
/** global: wappalyzer */
6
/** global: XMLHttpRequest */
7
8
(function() {
9
  wappalyzer.driver.document = document;
10
11
	const container = document.getElementById('wappalyzer-container');
12
	const url = wappalyzer.parseUrl(top.location.href);
0 ignored issues
show
Bug introduced by
The variable top seems to be never declared. If this is a global, consider adding a /** global: top */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
13
	const hasOwn = Object.prototype.hasOwnProperty;
14
15
  /**
16
   * Log messages to console
17
   */
18
  wappalyzer.driver.log = (message, source, type) => {
19
    console.log('[wappalyzer ' + type + ']', '[' + source + ']', message);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
20
  };
21
22
  function getPageContent() {
23
    wappalyzer.log('func: getPageContent', 'driver');
24
25
    var env = [];
26
27
    for ( let i in window ) {
28
      env.push(i);
29
    }
30
31
    var scripts = Array.prototype.slice
32
      .apply(document.scripts)
33
      .filter(s => s.src)
34
      .map(s => s.src);
35
36
    var html = new XMLSerializer().serializeToString(document).split('\n');
0 ignored issues
show
Bug introduced by
The variable XMLSerializer seems to be never declared. If this is a global, consider adding a /** global: XMLSerializer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
37
38
    html = html
39
      .slice(0, 1000).concat(html.slice(html.length - 1000))
40
      .map(line => line.substring(0, 1000))
41
      .join('\n');
42
43
    wappalyzer.analyze(url, {
44
      html: html,
45
      env: env,
46
      scripts: scripts
47
    });
48
  }
49
50
	function getResponseHeaders() {
51
    wappalyzer.log('func: getResponseHeaders', 'driver');
52
53
    var xhr = new XMLHttpRequest();
54
55
    xhr.open('GET', url, true);
56
57
    xhr.onreadystatechange = () => {
58
      if ( xhr.readyState === 4 && xhr.status ) {
59
        var headers = xhr.getAllResponseHeaders().split("\n");
60
61
        if ( headers.length > 0 && headers[0] != '' ) {
62
          wappalyzer.log('responseHeaders: ' + xhr.getAllResponseHeaders(), 'driver');
63
64
          var responseHeaders = {};
65
66
          headers.forEach(line => {
67
            var name, value;
68
69
            if ( line ) {
70
              name  = line.substring(0, line.indexOf(': '));
71
              value = line.substring(line.indexOf(': ') + 2, line.length - 1);
72
73
              if ( !responseHeaders[name.toLowerCase()] ){
74
                responseHeaders[name.toLowerCase()] = []
75
              }
76
              responseHeaders[name.toLowerCase()].push(value);
77
            }
78
          });
79
80
          wappalyzer.analyze(url, {
81
            headers: responseHeaders
82
          });
83
        }
84
      }
85
    }
86
87
    xhr.send();
88
  }
89
90
  /**
91
   * Display apps
92
   */
93
  wappalyzer.driver.displayApps = detected => {
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
94
    wappalyzer.log('func: diplayApps', 'driver');
95
96
    var first = true;
97
    var app;
98
    var category;
99
    var html;
100
101
    html =
102
      '<a id="wappalyzer-close" href="javascript: document.body.removeChild(document.getElementById(\'wappalyzer-container\')); void(0);">' +
103
        'Close' +
104
      '</a>' +
105
      '<div id="wappalyzer-apps">';
106
107
    if ( detected != null && Object.keys(detected).length ) {
108
      for ( app in detected ) {
109
        if ( !hasOwn.call(detected, app) ) {
110
          continue;
111
        }
112
113
        var version = detected[app].version,
114
          confidence = detected[app].confidence;
115
116
        html +=
117
          '<div class="wappalyzer-app' + ( first ? ' wappalyzer-first' : '' ) + '">' +
118
            '<a target="_blank" class="wappalyzer-application" href="' + wappalyzer.config.websiteURL + 'applications/' + app.toLowerCase().replace(/ /g, '-').replace(/[^a-z0-9-]/g, '') + '">' +
119
              '<strong>' +
120
                '<img src="' + wappalyzer.config.websiteURL + 'images/icons/' + (wappalyzer.apps[app].icon || 'default.svg') + '" width="16" height="16"/> ' + app +
121
              '</strong>' +
122
              ( version ? ' ' + version : '' ) + ( confidence < 100 ? ' (' + confidence + '% sure)' : '' ) +
123
            '</a>';
124
125
        for ( let i in wappalyzer.apps[app].cats ) {
126
          if ( !hasOwn.call(wappalyzer.apps[app].cats, i) ) {
127
            continue;
128
          }
129
130
          category = wappalyzer.categories[wappalyzer.apps[app].cats[i]].name;
131
132
          html += '<a target="_blank" class="wappalyzer-category" href="' + wappalyzer.config.websiteURL + 'categories/' + slugify(category) + '">' + category + '</a>';
133
        }
134
135
        html += '</div>';
136
137
        first = false;
138
      }
139
    } else {
140
      html += '<div id="wappalyzer-empty">No applications detected</div>';
141
    }
142
143
    html += '</div>';
144
145
    container.innerHTML = html;
146
  },
147
148
  /**
149
   * Open a tab
150
   */
151
  function openTab(args) {
152
    open(args.url);
153
  }
154
155
  function slugify(string) {
156
    return string.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/--+/g, '-').replace(/(?:^-|-$)/, '');
157
  }
158
159
  getPageContent();
160
  getResponseHeaders();
161
})();
162