Code Duplication    Length = 97-106 lines in 2 locations

index.js 1 location

@@ 1-106 (lines=106) @@
1
'use strict';
2
3
function decryptAES () {
4
5
  const pass = String(document.getElementById('pass').value);
6
  try {
7
8
    var decryptionError = String(document.getElementById('decryptionError').innerHTML);
9
    var noContentError = String(document.getElementById('noContentError').innerHTML);
10
11
  } catch (e) {
12
13
    decryptionError = 'Incorrect Password!';
14
    noContentError = 'No content to display!';
15
16
  }
17
18
  try {
19
20
    let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), pass);
21
    content = content.toString(CryptoJS.enc.Utf8);
22
    content = decodeBase64(content);
23
    content = unescape(content);
24
    if (content === '') {
25
26
      throw new Error(noContentError); // ???
27
28
    } else {
29
30
      document.getElementById('encrypt-blog').style.display = 'inline';
31
      document.getElementById('encrypt-blog').innerHTML = '';
32
      // Use jquery to load some js code
33
      $('#encrypt-blog').html(content);
34
      document.getElementById('security').style.display = 'none';
35
      if (document.getElementById('toc-div')) {
36
37
        document.getElementById('toc-div').style.display = 'inline';
38
39
      }
40
41
    }
42
    // Call MathJax to render
43
    if(typeof MathJax !== 'undefined') {
44
45
      MathJax.Hub.Queue(
46
        ['resetEquationNumbers', MathJax.InputJax.TeX, ],
47
        ['PreProcess', MathJax.Hub, ],
48
        ['Reprocess', MathJax.Hub, ]
49
      );
50
51
    }
52
53
  } catch (e) {
54
55
    alert(decryptionError);
56
    console.log(e);
57
58
  }
59
60
}
61
62
function htmlDecode (str) {
63
64
  let s = '';
65
  if (str.length == 0) {
66
67
    return '';
68
69
  }
70
71
  s = str.replace(/>/g, '&');
72
  s = s.replace(/&lt;/g, '<');
73
  s = s.replace(/&gt;/g, '>');
74
  s = s.replace(/&nbsp;/g, '    '); // ??? why not ' '
75
  s = s.replace(/'/g, '\'');
76
  s = s.replace(/&quot;/g, '"');
77
  s = s.replace(/<br>/g, '\n');
78
  return s;
79
80
}
81
82
function decodeBase64 (content) {
83
84
  content = CryptoJS.enc.Base64.parse(content);
85
  content = CryptoJS.enc.Utf8.stringify(content);
86
  return content;
87
88
}
89
90
// Since you decided to use jQuery.
91
$(document).ready(
92
  function () {
93
94
    console.log('Registering Enter for decrypt.');
95
    document.getElementById('pass').onkeypress = function (keyPressEvent) {
96
97
      if (keyPressEvent.keyCode === 13) {
98
99
        decryptAES();
100
101
      }
102
103
    };
104
105
  }
106
);
107

lib/blog-encrypt.js 1 location

@@ 1-97 (lines=97) @@
1
'use strict';
2
3
function decryptAES () {
4
5
  const pass = String(document.getElementById('pass').value);
6
  const decryptionError = String(document.getElementById('decryptionError').innerHTML);
7
  const noContentError = String(document.getElementById('noContentError').innerHTML);
8
  try {
9
10
    let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), pass);
11
    content = content.toString(CryptoJS.enc.Utf8);
12
    content = decodeBase64(content);
13
    content = unescape(content);
14
    if (content === '') {
15
16
      throw new Error(noContentError); // No content.
17
18
    } else {
19
20
      document.getElementById('encrypt-blog').style.display = 'inline';
21
      document.getElementById('encrypt-blog').innerHTML = '';
22
      // Use jquery to load some js code
23
      $('#encrypt-blog').html(content);
24
      document.getElementById('security').style.display = 'none';
25
      if (document.getElementById('toc-div')) {
26
27
        document.getElementById('toc-div').style.display = 'inline';
28
29
      }
30
31
    }
32
33
    // Call MathJax to render
34
    if(typeof MathJax !== 'undefined') {
35
36
      MathJax.Hub.Queue(
37
        ['resetEquationNumbers', MathJax.InputJax.TeX, ],
38
        ['PreProcess', MathJax.Hub, ],
39
        ['Reprocess', MathJax.Hub, ]
40
      );
41
42
    }
43
44
  } catch (e) {
45
46
    alert(decryptionError);
47
    console.log(e);
48
49
  }
50
51
}
52
53
function htmlDecode (str) {
54
55
  let s = '';
56
  if (str.length == 0) {
57
58
    return '';
59
60
  }
61
62
  s = str.replace(/&gt;/g, '&');
63
  s = s.replace(/&lt;/g, '<');
64
  s = s.replace(/&gt;/g, '>');
65
  s = s.replace(/&nbsp;/g, '    '); // ??? why not ' '
66
  s = s.replace(/'/g, '\'');
67
  s = s.replace(/&quot;/g, '"');
68
  s = s.replace(/<br>/g, '\n');
69
  return s;
70
71
}
72
73
function decodeBase64 (content) {
74
75
  content = CryptoJS.enc.Base64.parse(content);
76
  content = CryptoJS.enc.Utf8.stringify(content);
77
  return content;
78
79
}
80
81
// Since you decided to use jQuery.
82
$(document).ready(
83
  function () {
84
85
    console.log('Registering Enter for decrypt.');
86
    document.getElementById('pass').onkeypress = function (keyPressEvent) {
87
88
      if (keyPressEvent.keyCode === 13) {
89
90
        decryptAES();
91
92
      }
93
94
    };
95
96
  }
97
);
98