Completed
Push — master ( ccdb69...24c460 )
by Elbert
42s
created

inject.js ➔ ???   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
nc 8
nop 1
dl 0
loc 31
cc 9
rs 4.909
1
(function() {
2
	try {
3
    addEventListener('message', (event => {
4
      if ( event.data.id !== 'patterns' ) {
5
        return;
6
      }
7
8
      const patterns = event.data.patterns || {};
9
10
      const js = {};
11
12
      for ( let appName in patterns ) {
13
        if ( patterns.hasOwnProperty(appName) ) {
14
          js[appName] = {};
15
16
          for ( let chain in patterns[appName] ) {
17
            if ( patterns[appName].hasOwnProperty(chain) ) {
18
              js[appName][chain] = {};
19
20
              for ( let index in patterns[appName][chain] ) {
21
                const value = detectJs(chain);
22
23
                if ( value && patterns[appName][chain].hasOwnProperty(index) ) {
24
                  js[appName][chain][index] = value;
25
                }
26
              }
27
            }
28
          }
29
        }
30
      }
31
32
      postMessage({ id: 'js', js }, '*');
33
    }), false);
34
  } catch(e) {
35
    // Fail quietly
36
  }
37
38
  function detectJs(chain) {
39
    try {
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
    } catch(e) {
58
      // Fail quietly
59
    }
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
60
  }
61
}());
62