Completed
Push — master ( 08e0a3...c4b707 )
by Elbert
34s
created

content.js ➔ sendMessage   A

Complexity

Conditions 1
Paths 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 7
cc 1
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A content.js ➔ ... ➔ ??? 0 1 1
1
/** global: browser */
2
/** global: XMLSerializer */
3
4
if ( typeof browser !== 'undefined' && typeof document.body !== 'undefined' ) {
5
  try {
6
    sendMessage('init', {});
7
8
    // HTML
9
    var html = new XMLSerializer().serializeToString(document).split('\n');
10
11
    html = html
12
      .slice(0, 1000).concat(html.slice(html.length - 1000))
13
      .map(line => line.substring(0, 1000))
14
      .join('\n');
15
16
    // Scripts
17
    const scripts = Array.prototype.slice
18
      .apply(document.scripts)
19
      .filter(script => script.src)
20
      .map(script => script.src)
21
      .filter(script => script.indexOf('data:text/javascript;') !== 0);
22
23
    sendMessage('analyze', { html, scripts });
24
25
    // JavaScript variables
26
    const script = document.createElement('script');
27
28
    script.onload = () => {
29
      addEventListener('message', event => {
30
        if ( event.data.id !== 'js' ) {
31
          return;
32
        }
33
34
        document.body.removeChild(script);
35
36
        sendMessage('analyze', { js: event.data.js });
37
      }, true);
38
39
      sendMessage('get_js_patterns', {}, response => {
40
        if ( response ) {
41
          postMessage({
42
            id: 'patterns',
43
            patterns: response.patterns
44
          }, '*');
45
        }
46
      });
47
    };
48
49
    script.setAttribute('src', browser.extension.getURL('js/inject.js'));
50
51
    document.body.appendChild(script);
52
  } catch (e) {
53
    sendMessage('log', e);
54
  }
55
}
56
57
function sendMessage(id, subject, callback) {
58
  ( chrome || browser ).runtime.sendMessage({
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable chrome is declared in the current environment, consider using typeof chrome === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
59
    id,
60
    subject,
61
    source: 'content.js'
62
  }, callback || ( () => {} ));
63
}
64