Completed
Push — master ( bf4769...c9b0ab )
by Elbert
33s
created

inject.js ➔ ???   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 61
rs 9.5147

2 Functions

Rating   Name   Duplication   Size   Complexity  
B inject.js ➔ ... ➔ detectJs 0 19 6
D inject.js ➔ ... ➔ onMessage 0 33 9

How to fix   Long Method   

Long Method

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:

1
(() => {
2
	try {
3
    addEventListener('message', onMessage);
4
5
    function onMessage(event) {
0 ignored issues
show
Bug introduced by
The function onMessage is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var onMessage = function() { /* ... */ }; instead.
Loading history...
6
      if ( event.data.id !== 'patterns' ) {
7
        return;
8
      }
9
10
      removeEventListener('message', onMessage);
11
12
      const patterns = event.data.patterns || {};
13
14
      const js = {};
15
16
      for ( let appName in patterns ) {
17
        if ( patterns.hasOwnProperty(appName) ) {
18
          js[appName] = {};
19
20
          for ( let chain in patterns[appName] ) {
21
            if ( patterns[appName].hasOwnProperty(chain) ) {
22
              js[appName][chain] = {};
23
24
              for ( let index in patterns[appName][chain] ) {
25
                const value = detectJs(chain);
26
27
                if ( value && patterns[appName][chain].hasOwnProperty(index) ) {
28
                  js[appName][chain][index] = value;
29
                }
30
              }
31
            }
32
          }
33
        }
34
      }
35
36
      postMessage({ id: 'js', js }, '*');
37
    }
38
39
    function detectJs(chain) {
0 ignored issues
show
Bug introduced by
The function detectJs is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var detectJs = function() { /* ... */ }; instead.
Loading history...
40
      const properties = chain.split('.');
41
42
      var value = properties.length ? window : null;
43
44
      for ( let i = 0; i < properties.length; i ++ ) {
45
        var property = properties[i];
46
47
        if ( value.hasOwnProperty(property) ) {
48
          value = value[property];
49
        } else {
50
          value = null;
51
52
          break;
53
        }
54
      }
55
56
      return typeof value === 'string' || typeof value === 'number' ? value : !!value;
57
    }
58
  } catch(e) {
59
    // Fail quietly
60
  }
61
})();
62