Passed
Branch master (45a946)
by Dongxin
01:25
created

scripts/render.js   A

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 82
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 0
c 5
b 0
f 1
nc 1
dl 0
loc 82
rs 10
wmc 7
mnd 3
bc 9
fnc 2
bpm 4.5
cpm 3.5
noi 12
1
// Copyright © 2017 TangDongxin
2
3
// Permission is hereby granted, free of charge, to any person obtaining
4
// A copy of this software and associated documentation files (the "Software"),
5
// To deal in the Software without restriction, including without limitation
6
// The rights to use, copy, modify, merge, publish, distribute, sublicense,
7
// And/or sell copies of the Software, and to permit persons to whom the
8
// Software is furnished to do so, subject to the following conditions:
9
10
// The above copyright notice and this permission notice shall be included
11
// In all copies or substantial portions of the Software.
12
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21
// ===========================================
22
// JSON PARSER
23
// ===========================================
24
function onParse (result) {
25
26
  const chrome = this.chrome || this.browser;
27
  const body = document.body;
28
  let str; let jsonpMatch; let tag;
29
30
  let content = body.textContent;
31
32
  dlog(`is relaxed json support: ${isRelaxedJsonSupport}, ${content}`);
33
  if (isRelaxedJsonSupport) {
34
35
    content = transform(content);
36
37
    dlog(`after transform ${content}`);
38
39
  }
40
41
42
  dlog(`strict mode:${strictOnly}`);
43
  if (strictOnly) {
44
45
    // Only render when the contentType is json
46
    if (/[+\/]json$/i.test(document.contentType)) {
47
48
      init();
49
      draw(content, body);
50
51
    }
52
53
  } else {
54
55
    // Check whether the content is json or like json
56
    try {
57
58
      if (isJSON(content)) {
59
60
        init();
61
        draw(content, body);
62
63
      }
64
65
    } catch (e) {
66
67
      dlog(e);
68
69
    }
70
71
  }
72
73
  function init () {
74
75
    if (tag) {
76
77
      return;
78
79
    }
80
81
    tag = document.createElement('style');
82
    tag.textContent = [
83
      '.R', ',.D', `{font:${ fontSize } ${ fontStyle }}` +
84
            '.D', '{margin-left:0.5em; padding-left:2em; margin-top: 1px; border-left:1px dashed; border-color: #93A1A1;}' +
85
            '.X', '{border:1px solid #ccc; padding:1em}' +
86
            'a.L', '{text-decoration:none}' +
87
            'a.L', ':hover,a.L', ':focus{text-decoration:underline}' +
88
            'i.I', '{cursor:pointer;color:#ccc}' +
89
            'i.H', ',i.I', ':hover{text-shadow: 1px 1px 3px #999; color:#333}' +
90
            'i.I', ':before{content:" ▼ "}' +
91
            'i.C', ':before{content:" ▶ "}' +
92
            'i.C', '+.D', '{width:1px; height:1px; margin:0; padding:0; border:0; display:inline-block; overflow:hidden}' +
93
            '.S', `{color:${ strColor }}` + // String
94
            '.K', `{color:${ keyColor }}` + // Key
95
            '.E', '{color:#BCADAD}' + // Error
96
            '.B', `{color:${ intColor }}` + // Number and bool
97
            '.E', ',.B', '{font-style: italic}' + // Number bold
98
            'h3.E', '{margin:0 0 1em}',
99
    ].join(RAND);
100
101
    tag.textContent += `i.I${ RAND }:after{content:attr(data-content)}`;
102
    tag.textContent += `body {background: ${ bgColor }; color:${ defaultColor };}`;
103
    tag.textContent += '.HIDE {width:200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }';
104
    tag.textContent += `* {font:${ fontSize } ${ fontStyle } !important;}`;
105
    tag.textContent += '.black_overlay{ display: none; position: fixed; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.6; opacity:.60; filter: alpha(opacity=60); }';
106
    tag.textContent += '.white_content { display: none; position: fixed; top: 10%; left: 13%; width: 70%; min-width: 500px; min-height: 300px; height: 60%; padding: 1%; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; }';
107
    tag.textContent += 'str:hover{text-shadow: 1px 1px 3px #999; color:#333}';
108
    tag.textContent += 'json:hover{text-shadow: 1px 1px 3px #999; color:#333}';
109
    tag.textContent += 'url:hover{text-shadow: 1px 1px 3px #999; color:#333}';
110
    tag.textContent += 'base64:hover{text-shadow: 1px 1px 3px #999; color:#333}';
111
112
    document.head.appendChild(tag);
113
114
  }
115
116
}
117