-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathblog-encrypt.js
167 lines (133 loc) · 4.36 KB
/
blog-encrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* global CryptoJS, MathJax */
'use strict';
const hbeCookieName = 'HBEPASSWORD';
function getExecutableScript(oldElem){
let out = document.createElement('script');
const attList = ['type', 'text', 'src', 'crossorigin', 'defer', 'referrerpolicy'];
attList.forEach((att) => {
if(oldElem[att])
out[att] = oldElem[att];
})
return out;
}
function convertHTMLToElement(content){
let out = document.createElement('div');
out.innerHTML = content;
out.querySelectorAll('script').forEach((elem) => {
elem.parentNode.replaceChild(getExecutableScript(elem), elem);
});
return out;
}
function decryptAES (password) {
try {
var decryptionError = String(document.getElementById('decryptionError').innerHTML);
var noContentError = String(document.getElementById('noContentError').innerHTML);
} catch (e) {
decryptionError = 'Incorrect Password!';
noContentError = 'No content to display!';
}
try {
let content = unescape(CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), password).toString(CryptoJS.enc.Utf8));
if (content === '') {
throw new Error(noContentError);
} else {
document.getElementById('encrypt-blog').style.display = 'inline';
document.getElementById('encrypt-blog').innerHTML = '';
try{
document.getElementById('encrypt-blog').appendChild(convertHTMLToElement(content));
callBackReplaceHere // eslint-disable-line no-undef
} catch (e){
const errorInfo = '<p>'
+ 'Some errors occurred, check the original file please.'
+ 'Detailed exceptions are shown in console.'
+ '</p>';
console.error(e);
document.getElementById('encrypt-blog').innerHTML = errorInfo;
}
document.getElementById('hbe-security').style.display = 'none';
var tocDiv = document.getElementById("toc-div");
if (tocDiv) {
tocDiv.style.display = 'inline';
}
var tocDivs = document.getElementsByClassName('toc-div-class');
if (tocDivs && tocDivs.length > 0) {
for (var idx in tocDivs) {
tocDivs[idx].style.display = 'inline';
}
}
}
// Call MathJax to render
if(typeof MathJax !== 'undefined') {
try {
MathJax.Hub.Queue(
['resetEquationNumbers', MathJax.InputJax.TeX, ],
['PreProcess', MathJax.Hub, ],
['Reprocess', MathJax.Hub, ]
);
} catch (e) {
console.log('Can\'t render with MathJax');
}
}
} catch (e) {
alert(decryptionError);
console.log(e);
return false;
}
return true;
}
function setCookie (cookieName, cookieValue, expireMinutes) {
const expireTime = new Date(new Date().getTime() + 1000 * 60 * expireMinutes);
document.cookie = `${ cookieName }=${ escape(cookieValue) }${ expireMinutes == null ? '' : `;expires=${ expireTime.toGMTString() }` }`;
}
function getCookie (cookieName) {
if (document.cookie.length > 0) {
let idx = document.cookie.indexOf(`${ cookieName }=`);
if (idx != -1) {
idx = idx + cookieName.length + 1;
let idy = document.cookie.indexOf(';', idx);
if (idy == -1) {
idy = document.cookie.length;
}
return unescape(document.cookie.substring(idx, idy));
}
}
return '';
}
function getUrlRelativePath () {
const url = document.location.toString();
const arrUrl = url.split('//');
const start = arrUrl[1].indexOf('/');
let relUrl = arrUrl[1].substring(start);
if(relUrl.indexOf('?') != -1) {
relUrl = relUrl.split('?')[0];
}
return relUrl;
}
function generateCookieName () {
return hbeCookieName + getUrlRelativePath();
}
function hbeLoader(){
let password = String(getCookie(generateCookieName()));
console.log(`Get password from Cookie:${password}`);
if (password != '') {
if (!decryptAES(password)) {
// Delete cookie
setCookie(hbeCookieName, password, -5);
}
}
console.log('Registering Enter for decrypt.');
document.getElementById('pass').onkeypress = function (keyPressEvent) {
password = String(document.getElementById('pass').value);
if (keyPressEvent.keyCode === 13) {
const result = decryptAES(password);
if (result) {
setCookie(generateCookieName(), password, 30);
}
}
};
}
if (document.readyState !== 'loading') {
hbeLoader();
} else {
document.addEventListener('DOMContentLoaded', hbeLoader);
}