From c8558411302cbefb2b9280c02e43f7240d75e480 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Fri, 27 May 2022 15:25:54 +0800 Subject: [PATCH] :sparkles: Feat(fixit-decryptor): add encrypt again feature (#123) --- assets/css/_page/_single.scss | 1 + .../_partial/_single/_fixit-decryptor.scss | 47 +++++++++++++++++++ assets/js/fixit-decryptor.js | 27 ++++++++--- exampleSite/content/tests/encryption.md | 2 +- i18n/de.toml | 9 ++++ i18n/en.toml | 11 ++++- i18n/es.toml | 9 ++++ i18n/fr.toml | 9 ++++ i18n/it.toml | 9 ++++ i18n/pl.toml | 9 ++++ i18n/pt-BR.toml | 9 ++++ i18n/ro.toml | 9 ++++ i18n/ru.toml | 9 ++++ i18n/sr.toml | 9 ++++ i18n/vi.toml | 9 ++++ i18n/zh-CN.toml | 11 ++++- i18n/zh-TW.toml | 11 ++++- layouts/_default/single.md | 2 +- layouts/partials/single/fixit-decryptor.html | 8 ++-- 19 files changed, 195 insertions(+), 15 deletions(-) create mode 100644 assets/css/_partial/_single/_fixit-decryptor.scss diff --git a/assets/css/_page/_single.scss b/assets/css/_page/_single.scss index e98cf785..0ca9c11c 100644 --- a/assets/css/_page/_single.scss +++ b/assets/css/_page/_single.scss @@ -1,4 +1,5 @@ @import '../_partial/_single/toc'; +@import '../_partial/_single/fixit-decryptor'; .single { .single-title { diff --git a/assets/css/_partial/_single/_fixit-decryptor.scss b/assets/css/_partial/_single/_fixit-decryptor.scss new file mode 100644 index 00000000..790e3e28 --- /dev/null +++ b/assets/css/_partial/_single/_fixit-decryptor.scss @@ -0,0 +1,47 @@ +.fixit-decryptor-container { + font-family: $global-font-family; + text-align: center; + margin-top: 3rem; + + #fixit-decryptor-input, + .fixit-encryptor-btn { + display: inline-block; + box-sizing: border-box; + outline: none; + color: $global-font-color; + background-color: $global-background-color; + border: 1px solid $global-border-color; + border-radius: $global-border-radius; + transition: 0.1s; + + &:active, + &:hover { + color: $global-link-hover-color; + border-color: $global-link-hover-color; + + [theme='dark'] & { + color: $global-link-hover-color-dark; + border-color: $global-link-hover-color-dark; + } + } + + [theme='dark'] & { + color: $global-font-color-dark; + border-color: $global-border-color-dark; + background-color: $global-background-color-dark; + } + } + + #fixit-decryptor-input { + width: clamp(50%, 400px, 100%); + height: 3rem; + padding-left: 1rem; + padding-right: 1rem; + } + + .fixit-encryptor-btn { + cursor: pointer; + transition: 0.1s; + padding: 0.6rem 1rem; + } +} diff --git a/assets/js/fixit-decryptor.js b/assets/js/fixit-decryptor.js index e4226468..e9ac0a76 100644 --- a/assets/js/fixit-decryptor.js +++ b/assets/js/fixit-decryptor.js @@ -19,7 +19,8 @@ FixItDecryptor = function (options = {}) { */ var _decryptContent = (base64EncodeContent) => { try { - this.$el.classList.add('d-none'); + this.$el.querySelector('#fixit-decryptor-input').classList.add('d-none'); + this.$el.querySelector('.fixit-encryptor-btn').classList.remove('d-none'); document.querySelector('#content').insertAdjacentHTML( 'afterbegin', CryptoJS.enc.Base64.parse(base64EncodeContent).toString(CryptoJS.enc.Utf8) @@ -43,7 +44,7 @@ FixItDecryptor = function (options = {}) { this.validateCache(); - document.querySelector('#fixit-decryptor')?.addEventListener('keydown', function (e) { + this.$el.querySelector('#fixit-decryptor-input')?.addEventListener('keydown', function (e) { if (e.key === 'Enter') { e.preventDefault(); const $content = document.querySelector('#content'); @@ -53,13 +54,13 @@ FixItDecryptor = function (options = {}) { const inputMd5 = CryptoJS.MD5(input).toString(); const inputSha256 = CryptoJS.SHA256(input).toString(); + this.value = ''; if (!input) { - alert('Please input the correct password!'); - return console.warn('Please input the correct password!'); + alert('Please enter the correct password!'); + return console.warn('Please enter the correct password!'); } if (inputMd5 !== password) { alert(`Password error: ${input} not the correct password!`); - this.value = ''; return console.warn(`Password error: ${input} not the correct password!`); } // cache decryption statistics @@ -74,6 +75,18 @@ FixItDecryptor = function (options = {}) { _decryptContent($content.getAttribute('data-content').replace(inputSha256.slice(saltLen), '')); } }); + + this.$el.querySelector('.fixit-encryptor-btn')?.addEventListener('click', function (e) { + e.preventDefault(); + this.classList.add('d-none') + _decryptor.$el.querySelector('#fixit-decryptor-input').classList.remove('d-none'); + document.querySelector('#content').innerHTML = ''; + document.querySelector('#content').insertAdjacentElement( + 'afterbegin', + _decryptor.$el + ); + window.localStorage.removeItem(`fixit-decryptor/#${location.pathname}`); + }); }; /** @@ -85,10 +98,10 @@ FixItDecryptor = function (options = {}) { const cachedStat = JSON.parse(window.localStorage.getItem(`fixit-decryptor/#${location.pathname}`)); if (!cachedStat) { - return this.$el.classList.remove('d-none'); + return this.$el.querySelector('#fixit-decryptor-input').classList.remove('d-none'); } if (cachedStat?.md5 !== password || Number(cachedStat?.expiration) < Math.ceil(Date.now() / 1000)) { - this.$el.classList.remove('d-none'); + this.$el.querySelector('#fixit-decryptor-input').classList.remove('d-none'); window.localStorage.removeItem(`fixit-decryptor/#${location.pathname}`); return console.warn('The password has expired, please re-enter!'); } diff --git a/exampleSite/content/tests/encryption.md b/exampleSite/content/tests/encryption.md index 30af600c..8e5542ab 100644 --- a/exampleSite/content/tests/encryption.md +++ b/exampleSite/content/tests/encryption.md @@ -5,7 +5,7 @@ description: "Test for encrypting content" type: 'posts' draft: true password: 1212 -message: password is 1212 +message: Password is 1212 lightgallery: true diff --git a/i18n/de.toml b/i18n/de.toml index a1438a07..20dcc243 100644 --- a/i18n/de.toml +++ b/i18n/de.toml @@ -190,8 +190,17 @@ other = "Mehr lesen" [expirationReminder] other = "Dieser Artikel wurde zuletzt auf {{ .Date }} aktualisiert, der Inhalt kann veraltet sein." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Passwort" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/en.toml b/i18n/en.toml index 05f9a4cd..cc229de4 100644 --- a/i18n/en.toml +++ b/i18n/en.toml @@ -186,8 +186,17 @@ other = "Read More" [expirationReminder] other = "This article was last updated on {{ .Date }}, the content may be out of date." +[encryptedAbstract] +other = "This article has been encrypted, so its raw content is invisible!" + [encryptedMessage] -other = "This article was encrypted, please enter the correct password to view!" +other = "Please enter the password" + +[password] +other = "Password" + +[encryptyAgain] +other = "Encrypt again" # === posts/single.html === # === 404.html === diff --git a/i18n/es.toml b/i18n/es.toml index 8ced058a..6c47b503 100644 --- a/i18n/es.toml +++ b/i18n/es.toml @@ -190,8 +190,17 @@ other = "Leer más" [expirationReminder] other = "Este artículo se actualizó por última vez el {{ .Date }}, es posible que el contenido no esté actualizado." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Contraseña" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/fr.toml b/i18n/fr.toml index becda35a..fd66d542 100644 --- a/i18n/fr.toml +++ b/i18n/fr.toml @@ -187,8 +187,17 @@ other = "Accueil" [readMore] other = "En savoir plus" +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Mot de passe" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/it.toml b/i18n/it.toml index c9dc262d..bf721d6a 100644 --- a/i18n/it.toml +++ b/i18n/it.toml @@ -190,8 +190,17 @@ other = "Leggi di più" [expirationReminder] other = "Questo articolo è stato aggiornato l'ultima volta il {{ .Date }}, il contenuto potrebbe non essere aggiornato." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Password" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/pl.toml b/i18n/pl.toml index 97131c5b..9fe664c5 100644 --- a/i18n/pl.toml +++ b/i18n/pl.toml @@ -187,8 +187,17 @@ other = "Czytaj więcej" [expirationReminder] other = "Ten artykuł był ostatnio aktualizowany {{ .Date }}, jego treść może być nieaktualna." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Hasło" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/pt-BR.toml b/i18n/pt-BR.toml index c64b520c..a7f47f1e 100644 --- a/i18n/pt-BR.toml +++ b/i18n/pt-BR.toml @@ -190,8 +190,17 @@ other = "Leia mais" [expirationReminder] other = "Este artigo foi atualizado pela última vez em {{ .Date }}, o conteúdo pode estar desatualizado." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Contrasinha" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/ro.toml b/i18n/ro.toml index 545438f7..0d8b5d11 100644 --- a/i18n/ro.toml +++ b/i18n/ro.toml @@ -190,8 +190,17 @@ other = "Citește mai mult" [expirationReminder] other = "Acest articol a fost actualizat ultima dată pe {{ .Date }}, conținutul poate fi depășit." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Parolă" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/ru.toml b/i18n/ru.toml index 4dd33935..d5dcc491 100644 --- a/i18n/ru.toml +++ b/i18n/ru.toml @@ -190,8 +190,17 @@ other = "Читать больше" [expirationReminder] other = "Эта статья последний раз обновлялась {{ .Date }}, содержание может быть устаревшим." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "арготизм" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/sr.toml b/i18n/sr.toml index c9e3738e..0851fdf9 100644 --- a/i18n/sr.toml +++ b/i18n/sr.toml @@ -187,8 +187,17 @@ other = "Прочитај више" [expirationReminder] other = "Овај чланак је последњи пут ажуриран {{ .Date }}, садржај је можда застарео." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Паролица" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/vi.toml b/i18n/vi.toml index 78c7ce45..ef56356e 100644 --- a/i18n/vi.toml +++ b/i18n/vi.toml @@ -189,8 +189,17 @@ other = "Đọc thêm" [expirationReminder] other = "Bài viết này được cập nhật lần cuối vào {{ .Date }}, nội dung có thể đã lỗi thời." +[encryptedAbstract] +other = "" # waitting for translation + [encryptedMessage] other = "" # waitting for translation + +[password] +other = "Mật khẩu" + +[encryptyAgain] +other = "" # waitting for translation # === posts/single.html === # === 404.html === diff --git a/i18n/zh-CN.toml b/i18n/zh-CN.toml index d9a1a8db..5a66af6d 100644 --- a/i18n/zh-CN.toml +++ b/i18n/zh-CN.toml @@ -190,8 +190,17 @@ other = "阅读全文" [expirationReminder] other = "本文最后更新于 {{ .Date }},文中内容可能已过时。" +[encryptedAbstract] +other = "本文已加密,因此其原始内容不可见!" + [encryptedMessage] -other = "本文已加密,请输入正确密码后查看!" +other = "请输入密码" + +[password] +other = "密码" + +[encryptyAgain] +other = "重新加密" # === posts/single.html === # === 404.html === diff --git a/i18n/zh-TW.toml b/i18n/zh-TW.toml index ac98dbfb..8e9c24d7 100644 --- a/i18n/zh-TW.toml +++ b/i18n/zh-TW.toml @@ -190,8 +190,17 @@ other = "閱讀全文" [expirationReminder] other = "本文最後更新於 {{ .Date }},文中內容可能已過時。" +[encryptedAbstract] +other = "本文已加密,囙此其原始內容不可見!" + [encryptedMessage] -other = "本文已加密,請輸入正確密碼後查看!" +other = "請輸入密碼" + +[password] +other = "密碼" + +[encryptyAgain] +other = "重新加密" # === posts/single.html === # === 404.html === diff --git a/layouts/_default/single.md b/layouts/_default/single.md index 5311de02..cfd1a13a 100644 --- a/layouts/_default/single.md +++ b/layouts/_default/single.md @@ -2,7 +2,7 @@ # {{ .Title }} {{ if $params.password -}} - ***{{ T "encryptedMessage" }}*** + ***{{ T "encryptedAbstract" }}*** {{- else -}} {{ .RawContent }} {{- end }} diff --git a/layouts/partials/single/fixit-decryptor.html b/layouts/partials/single/fixit-decryptor.html index 1adca94f..fc04d660 100644 --- a/layouts/partials/single/fixit-decryptor.html +++ b/layouts/partials/single/fixit-decryptor.html @@ -1,8 +1,10 @@ {{- $params := .Scratch.Get "params" -}} -