Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
17mie32
GitHub Repository: 17mie32/17mie32.github.io
Path: blob/main/js/gallery-encrypt.js
1317 views
1
'use strict';
2
3
function decryptAES(password) {
4
5
try {
6
7
var decryptionError = String(document.getElementById('decryptionError').innerHTML);
8
var noContentError = String(document.getElementById('noContentError').innerHTML);
9
10
} catch (e) {
11
12
decryptionError = 'Incorrect Password!';
13
noContentError = 'No content to display!';
14
15
}
16
17
try {
18
19
let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), password);
20
content = content.toString(CryptoJS.enc.Utf8);
21
content = decodeBase64(content);
22
content = unescape(content);
23
if (content === '') {
24
25
throw new Error(noContentError); // ???
26
27
} else {
28
29
document.getElementById('encrypt-blog').style.display = 'inline';
30
document.getElementById('encrypt-blog').innerHTML = '';
31
32
// Use jquery to load some js code
33
try {
34
35
$('#encrypt-blog').html(content);
36
37
// NO Style Change here
38
39
// NO Style Change here
40
41
} catch (e) {
42
43
const errorInfo = '<p>'
44
+ 'Some errors occurred, check the original file please.'
45
+ 'Detailed exceptions are shown in console.'
46
+ '</p>';
47
console.error(e);
48
$('#encrypt-blog').html(errorInfo);
49
50
}
51
52
document.getElementById('hbe-security').style.display = 'none';
53
if (document.getElementById('toc-div')) {
54
55
document.getElementById('toc-div').style.display = 'inline';
56
57
}
58
59
}
60
61
// Call MathJax to render
62
if (typeof MathJax !== 'undefined') {
63
64
try {
65
66
MathJax.Hub.Queue(
67
['resetEquationNumbers', MathJax.InputJax.TeX,],
68
['PreProcess', MathJax.Hub,],
69
['Reprocess', MathJax.Hub,]
70
);
71
72
} catch (e) {
73
74
console.log('Can\'t render with MathJax');
75
76
}
77
78
}
79
80
} catch (e) {
81
82
alert(decryptionError);
83
console.log(e);
84
return false;
85
86
}
87
88
return true;
89
90
}
91
92
function htmlDecode(str) {
93
94
let s = '';
95
if (str.length == 0) {
96
97
return '';
98
99
}
100
101
s = str.replace(/&gt;/g, '&');
102
s = s.replace(/&lt;/g, '<');
103
s = s.replace(/&gt;/g, '>');
104
s = s.replace(/&nbsp;/g, ' '); // ??? why not ' '
105
s = s.replace(/'/g, '\'');
106
s = s.replace(/&quot;/g, '"');
107
s = s.replace(/<br>/g, '\n');
108
return s;
109
110
}
111
112
function decodeBase64(content) {
113
114
content = CryptoJS.enc.Base64.parse(content);
115
content = CryptoJS.enc.Utf8.stringify(content);
116
return content;
117
118
}
119
120
function setCookie(cookieName, cookieValue, expireMinutes) {
121
122
const expireTime = new Date(new Date().getTime() + 1000 * 60 * expireMinutes);
123
document.cookie = `${cookieName}=${escape(cookieValue)}${expireMinutes == null ? '' : `;expires=${expireTime.toGMTString()}`}`;
124
125
}
126
127
function getCookie(cookieName) {
128
129
if (document.cookie.length > 0) {
130
131
let idx = document.cookie.indexOf(`${cookieName}=`);
132
if (idx != -1) {
133
134
idx = idx + cookieName.length + 1;
135
let idy = document.cookie.indexOf(';', idx);
136
if (idy == -1) {
137
138
idy = document.cookie.length;
139
140
}
141
return unescape(document.cookie.substring(idx, idy));
142
143
}
144
145
}
146
return '';
147
148
}
149
150
function GetUrlRelativePath() {
151
152
const url = document.location.toString();
153
const arrUrl = url.split('//');
154
155
const start = arrUrl[1].indexOf('/');
156
let relUrl = arrUrl[1].substring(start);
157
158
if (relUrl.indexOf('?') != -1) {
159
160
relUrl = relUrl.split('?')[0];
161
162
}
163
return relUrl;
164
165
}
166
167
function GenerateCookieName() {
168
169
const COOKIE_NAME = 'HBE-PASSWORD';
170
return COOKIE_NAME + GetUrlRelativePath();
171
172
}
173
174
// Since you decided to use jQuery.
175
$(document).ready(
176
function () {
177
let password = String(getCookie(GenerateCookieName()));
178
console.log(`Get password from Cookie:${password}`);
179
180
if (password != '') {
181
182
if (!decryptAES(password)) {
183
// Delete cookie
184
setCookie(COOKIE_NAME, password, -5);
185
} else {
186
187
document.getElementById('encrypt-blog').removeAttribute('style');
188
189
$("#encrypt-blog").justifiedGallery({margins: 5, rowHeight: 150});
190
}
191
}
192
document.getElementById('pass').onkeypress = function (keyPressEvent) {
193
194
password = String(document.getElementById('pass').value);
195
if (keyPressEvent.keyCode === 13) {
196
197
const result = decryptAES(password);
198
199
if (result) {
200
document.getElementById('encrypt-blog').removeAttribute('style');
201
202
$("#encrypt-blog").justifiedGallery({margins: 5, rowHeight: 150});
203
204
setCookie(GenerateCookieName(), password, 30);
205
}
206
}
207
};
208
$('#btn_decrypt').on('click', function () {
209
210
password = String(document.getElementById('pass').value);
211
212
const result = decryptAES(password);
213
214
if (result) {
215
216
document.getElementById('encrypt-blog').removeAttribute('style');
217
218
$("#encrypt-blog").justifiedGallery({margins: 5, rowHeight: 150});
219
220
setCookie(GenerateCookieName(), password, 30);
221
}
222
});
223
}
224
);
225