1 | /* global hexo, __dirname */ |
||
2 | |||
3 | 'use strict'; |
||
4 | |||
5 | const fs = require('hexo-fs'); |
||
6 | const path = require('path'); |
||
7 | const CryptoJS = require('crypto-js'); |
||
8 | const log = require('hexo-log')({ |
||
9 | 'debug': false, |
||
10 | 'silent': false, |
||
11 | }); |
||
12 | |||
13 | hexo.extend.filter.register('after_post_render', function encrypt (data) { |
||
14 | |||
15 | // Close the encrypt function |
||
16 | if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) { |
||
17 | |||
18 | return data; |
||
19 | |||
20 | } |
||
21 | if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template |
||
22 | |||
23 | hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html')); |
||
24 | |||
25 | } |
||
26 | if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info |
||
27 | |||
28 | hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>'; |
||
29 | |||
30 | } |
||
31 | if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message |
||
32 | |||
33 | hexo.config.encrypt.default_message = 'Please enter the password to read the blog.'; |
||
34 | |||
35 | } |
||
36 | if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // Wrong password |
||
37 | |||
38 | hexo.config.encrypt.default_decryption_error = 'Incorrect Password!'; |
||
39 | |||
40 | } |
||
41 | if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // No content |
||
42 | |||
43 | hexo.config.encrypt.default_no_content_error = 'No content to display!'; |
||
44 | |||
45 | } |
||
46 | |||
47 | var tag1,tag2; |
||
48 | if (!('password' in data && data.password) && ('tags' in hexo.config.encrypt)) { |
||
49 | outer: |
||
50 | for(var i in data.tags.data){ |
||
0 ignored issues
–
show
|
|||
51 | tag1 = data.tags.data[i].name |
||
52 | console.log(tag1) |
||
0 ignored issues
–
show
|
|||
53 | for(var j in hexo.config.encrypt.tags){ |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
![]() |
|||
54 | tag2 = hexo.config.encrypt.tags[j].name |
||
55 | if (tag1==tag2){ |
||
56 | data.password = hexo.config.encrypt.tags[j].password; |
||
57 | console.log(data.password) |
||
58 | break outer; |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if ('password' in data && data.password) { |
||
65 | |||
66 | // Use the blog's config first |
||
67 | log.info(`Encrypted the blog: ${ data.title.trim() }`); |
||
68 | |||
69 | // Store the origin data |
||
70 | data.origin = data.content; |
||
71 | data.encrypt = true; |
||
72 | |||
73 | if (!('abstract' in data && data.abstract)) { |
||
74 | |||
75 | data.abstract = hexo.config.encrypt.default_abstract; |
||
76 | |||
77 | } |
||
78 | if (!('template' in data && data.template)) { |
||
79 | |||
80 | data.template = hexo.config.encrypt.default_template; |
||
81 | |||
82 | } |
||
83 | if (!('message' in data && data.message)) { |
||
84 | |||
85 | data.message = hexo.config.encrypt.default_message; |
||
86 | |||
87 | } |
||
88 | if (!('decryptionError' in data && data.decryptionError)) { |
||
89 | |||
90 | data.decryptionError = hexo.config.encrypt.default_decryption_error; |
||
91 | |||
92 | } |
||
93 | if (!('noContentError' in data && data.noContentError)) { |
||
94 | |||
95 | data.noContentError = hexo.config.encrypt.default_no_content_error; |
||
96 | |||
97 | } |
||
98 | |||
99 | if (data.content.trim() === '') { |
||
100 | |||
101 | log.warn('Warning: Your blog has no content, it may cause error when decrypting.'); |
||
102 | |||
103 | } |
||
104 | |||
105 | data.content = escape(data.content); |
||
106 | data.content = CryptoJS.enc.Utf8.parse(data.content); |
||
107 | data.content = CryptoJS.enc.Base64.stringify(data.content); |
||
108 | data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString(); |
||
109 | |||
110 | data.template = data.template.replace('{{content}}', data.content); |
||
111 | data.template = data.template.replace('{{message}}', data.message); |
||
112 | data.template = data.template.replace('{{message}}', data.message); |
||
113 | data.template = data.template.replace('{{decryptionError}}', data.decryptionError); |
||
114 | data.template = data.template.replace('{{noContentError}}', data.noContentError); |
||
115 | |||
116 | data.content = data.template; |
||
117 | data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>`; |
||
118 | data.content += `<script src="${hexo.config.root}lib/blog-encrypt.js"></script>`; |
||
119 | data.content += `<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`; |
||
120 | |||
121 | data.more = data.abstract; |
||
122 | data.excerpt = data.more; |
||
123 | |||
124 | } |
||
125 | |||
126 | return data; |
||
127 | |||
128 | }); |
||
129 | |||
130 | hexo.extend.generator.register('blog-encrypt', () => [ |
||
131 | { |
||
132 | 'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')), |
||
133 | 'path': 'lib/crypto-js.js', |
||
134 | }, { |
||
135 | 'data': function () { |
||
136 | |||
137 | const Readable = require('stream').Readable; |
||
138 | const stream = new Readable(); |
||
139 | stream.push(fs.readFileSync(path.resolve(__dirname, 'lib/blog-encrypt.js')) |
||
140 | .replace('callBackReplaceHere', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : '')); |
||
141 | stream.push(null); // Indicates the end of the stream |
||
142 | return stream; |
||
143 | |||
144 | }, |
||
145 | 'path': 'lib/blog-encrypt.js', |
||
146 | }, { |
||
147 | 'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')), |
||
148 | 'path': 'css/blog-encrypt.css', |
||
149 | }, |
||
150 | ]); |
||
151 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: