Skip to content

Commit 1585614

Browse files
committed
some refactoring
1 parent a6e0446 commit 1585614

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

encryptcontent/decrypt-contents.tpl.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ function base64url_decode(input) {
362362
};
363363

364364
/* Decrypt content of a page */
365-
{% if webcrypto %}async {% endif %}function decrypt_action(password_input, encrypted_content, decrypted_content, key_from_storage, username_input) {
365+
{% if webcrypto %}async {% endif %}function decrypt_action(username_input, password_input, encrypted_content, decrypted_content, key_from_storage=false) {
366366
let key=false;
367367
let keys_from_keystore=false;
368368

@@ -399,8 +399,7 @@ function base64url_decode(input) {
399399
}
400400
};
401401

402-
{% if webcrypto %}async {% endif %}function decryptor_reaction(key_or_keys, password_input, fallback_used=false) {
403-
let decrypted_element = document.getElementById("mkdocs-decrypted-content");
402+
{% if webcrypto %}async {% endif %}function decryptor_reaction(key_or_keys, password_input, decrypted_content, fallback_used=false) {
404403
if (key_or_keys) {
405404
let key;
406405
if (typeof key_or_keys === "object") {
@@ -421,7 +420,7 @@ function base64url_decode(input) {
421420
decrypt_somethings(key, encrypted_something);
422421
{%- endif %}
423422
if (typeof inject_something !== 'undefined') {
424-
decrypted_element = {% if webcrypto %}await {% endif %}decrypt_somethings(key, inject_something);
423+
decrypted_content = {% if webcrypto %}await {% endif %}decrypt_somethings(key, inject_something);
425424
}
426425
if (typeof delete_something !== 'undefined') {
427426
let el = document.getElementById(delete_something)
@@ -438,7 +437,7 @@ function base64url_decode(input) {
438437
if (typeof mermaid === 'object') { mermaid.contentLoaded();};
439438
{%- endif %}
440439
{% if hljs -%}
441-
decrypted_element.querySelectorAll('pre code').forEach((block) => {
440+
decrypted_content.querySelectorAll('pre code').forEach((block) => {
442441
hljs.highlightElement(block);
443442
});
444443
{%- endif %}
@@ -473,43 +472,43 @@ function base64url_decode(input) {
473472

474473
/* Trigger decryption process */
475474
{% if webcrypto %}async {% endif %}function init_decryptor() {
476-
var username_input = document.getElementById('mkdocs-content-user');
477-
var password_input = document.getElementById('mkdocs-content-password');
475+
let username_input = document.getElementById('mkdocs-content-user');
476+
let password_input = document.getElementById('mkdocs-content-password');
478477
// adjust password field width to placeholder length
479478
//if (password_input.hasAttribute('placeholder')) {
480479
// password_input.setAttribute('size', password_input.getAttribute('placeholder').length);
481480
//}
482-
var encrypted_content = document.getElementById('mkdocs-encrypted-content');
483-
var decrypted_content = document.getElementById('mkdocs-decrypted-content');
481+
let encrypted_content = document.getElementById('mkdocs-encrypted-content');
482+
let decrypted_content = document.getElementById('mkdocs-decrypted-content');
484483
let content_decrypted;
485484
{% if remember_keys -%}
486485
/* If remember_keys is set, try to use sessionStorage item to decrypt content when page is loaded */
487486
let key_from_storage = {% if webcrypto %}await {% endif %}getItemName(encryptcontent_id);
488487
if (key_from_storage) {
489488
content_decrypted = {% if webcrypto %}await {% endif %}decrypt_action(
490-
password_input, encrypted_content, decrypted_content, key_from_storage, username_input
489+
username_input, password_input, encrypted_content, decrypted_content, key_from_storage
491490
);
492491
{% if remember_password -%}
493492
/* try to get username/password from sessionStorage */
494493
if (content_decrypted === false) {
495494
let got_credentials = {% if webcrypto %}await {% endif %}getCredentials(username_input, password_input);
496495
if (got_credentials) {
497496
content_decrypted = {% if webcrypto %}await {% endif %}decrypt_action(
498-
password_input, encrypted_content, decrypted_content, false, username_input
497+
username_input, password_input, encrypted_content, decrypted_content
499498
);
500499
}
501500
}
502501
{%- endif %}
503-
decryptor_reaction(content_decrypted, password_input, true);
502+
decryptor_reaction(content_decrypted, password_input, decrypted_content, true);
504503
}
505504
{% if remember_password -%}
506505
else {
507506
let got_credentials = {% if webcrypto %}await {% endif %}getCredentials(username_input, password_input);
508507
if (got_credentials) {
509508
content_decrypted = {% if webcrypto %}await {% endif %}decrypt_action(
510-
password_input, encrypted_content, decrypted_content, false, username_input
509+
username_input, password_input, encrypted_content, decrypted_content
511510
);
512-
decryptor_reaction(content_decrypted, password_input, true);
511+
decryptor_reaction(content_decrypted, password_input, decrypted_content, true);
513512
}
514513
}
515514
{%- endif %}
@@ -542,9 +541,9 @@ function base64url_decode(input) {
542541
}
543542
password_input.value = sharestring.substring(pass_sep+1);
544543
content_decrypted = {% if webcrypto %}await {% endif %}decrypt_action(
545-
password_input, encrypted_content, decrypted_content, false, username_input
544+
username_input, password_input, encrypted_content, decrypted_content
546545
);
547-
decryptor_reaction(content_decrypted, password_input, false);
546+
decryptor_reaction(content_decrypted, password_input, decrypted_content);
548547
}
549548
}
550549
}
@@ -556,20 +555,20 @@ function base64url_decode(input) {
556555
decrypt_button.onclick = {% if webcrypto %}async {% endif %}function(event) {
557556
event.preventDefault();
558557
content_decrypted = {% if webcrypto %}await {% endif %}decrypt_action(
559-
password_input, encrypted_content, decrypted_content, false, username_input
558+
username_input, password_input, encrypted_content, decrypted_content
560559
);
561-
decryptor_reaction(content_decrypted, password_input);
560+
decryptor_reaction(content_decrypted, password_input, decrypted_content);
562561
};
563562
}
564563
{%- endif %}
565-
/* Default, try decrypt content when key (ctrl) enter is press */
564+
/* Default, try decrypt content when key enter is press */
566565
password_input.addEventListener('keypress', {% if webcrypto %}async {% endif %}function(event) {
567566
if (event.key === "Enter") {
568567
event.preventDefault();
569568
content_decrypted = {% if webcrypto %}await {% endif %}decrypt_action(
570-
password_input, encrypted_content, decrypted_content, false, username_input
569+
username_input, password_input, encrypted_content, decrypted_content
571570
);
572-
decryptor_reaction(content_decrypted, password_input);
571+
decryptor_reaction(content_decrypted, password_input, decrypted_content);
573572
}
574573
});
575574
}

0 commit comments

Comments
 (0)