From 4e3742ec4247a842e4f519a1be0cc07177ecdf59 Mon Sep 17 00:00:00 2001 From: RTa-technology Date: Tue, 31 Oct 2023 04:13:45 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20PW=E3=81=AE=E4=B8=8A=E6=9B=B8?= =?UTF-8?q?=E3=81=8D=E3=81=8C=E5=8F=AF=E8=83=BD=E3=81=AA=E7=8A=B6=E6=85=8B?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/css/wikidot.css | 8 ++++ src/index.html | 6 ++- src/script/api.ts | 2 +- src/script/eventHandlers.ts | 91 ++++++++++++++++++++++++++++++------- src/script/helper.ts | 2 +- src/script/utils.ts | 27 ++++++++++- 6 files changed, 116 insertions(+), 20 deletions(-) diff --git a/src/css/wikidot.css b/src/css/wikidot.css index 51f3b5e..4acbb13 100644 --- a/src/css/wikidot.css +++ b/src/css/wikidot.css @@ -141,3 +141,11 @@ code, .code { .page-source { white-space: pre-wrap; } + + +.errors { + color: #cc0f35; + background: #feecf0; + margin: 0.5em; + font-size: 1.2rem; +} \ No newline at end of file diff --git a/src/index.html b/src/index.html index 0df3410..3ce0916 100644 --- a/src/index.html +++ b/src/index.html @@ -146,7 +146,11 @@

作成履歴

- + +
+
+
+
Advanced Settings

Edit Sidebar

diff --git a/src/script/api.ts b/src/script/api.ts index c753c13..ff9833b 100644 --- a/src/script/api.ts +++ b/src/script/api.ts @@ -1,5 +1,5 @@ // api.ts -import { postDataToGAS, getDataFromGAS, getHistoryFromGAS } from './helper'; +import { getDataFromGAS, getHistoryFromGAS } from './helper'; export const fetchSharedData = async (shortid: string) => { try { diff --git a/src/script/eventHandlers.ts b/src/script/eventHandlers.ts index dc1acea..675db71 100644 --- a/src/script/eventHandlers.ts +++ b/src/script/eventHandlers.ts @@ -4,7 +4,7 @@ import { debounce } from 'ts-debounce'; import { - generateShortId, getOrCreateUserShortId, getCurrentPageShortId, encryptSha256, encryptAES + generateShortId, getOrCreateUserShortId, getCurrentPageShortId, encryptSha256, setCookie, getCookie } from './utils'; @@ -74,25 +74,45 @@ export const handleDOMContentLoaded = async () => { const shortid = pathParts[2]; // shortId を取得 // console.log(shortid); + // const enchash = localStorage.getItem(`FtmlPWHash[${shortid}]`); + const enchash = getCookie(shortid); - document.body.style.display = "none"; let password, hash; - password = prompt("パスワードを入力してください"); - document.body.style.display = ""; - - hash = encryptSha256(password); + if (!enchash) { + document.body.style.display = "none"; + password = prompt("パスワードを入力してください"); + document.body.style.display = ""; + + hash = encryptSha256(password); + } else { + hash = enchash; + // document.getElementById("password")をdisabledにする + const Elementpassword = document.getElementById("password"); + const ElementpasswordEncripted = document.getElementById("password-encripted"); + if (Elementpassword) { + Elementpassword.setAttribute("disabled", "disabled"); + Elementpassword.setAttribute("placeholder", "パスワード設定済"); + } + // ElementpasswordEncriptedにhashを入れる + if (ElementpasswordEncripted) { + ElementpasswordEncripted.setAttribute("value", hash); + } + } try { const data = await getDataPWFromGAS(shortid, hash); // 適切な関数名に修正 if (data.error) { - displayLocalStorageData(`FtmlStorage[${shortid}]`) + // displayLocalStorageData(`FtmlStorage[${shortid}]`) + displayData({ title: "PASSWORD ERROR", source: "パスワードが間違っています" }); } else { - displayDataPW(data.data, password); + displayData(data.data); + // localStorage.setItem(`FtmlPWHash[${shortid}]`, hash); + setCookie(shortid, hash); } } catch (error) { console.error('Error fetching data:', error); - displayLocalStorageData(`FtmlStorage[${shortid}]`) + // displayLocalStorageData(`FtmlStorage[${shortid}]`) } @@ -211,7 +231,22 @@ const handleEditsaveButtonClick = async () => { // 共有ボタンを押したときの処理 const handleShareButtonClick = async () => { - const shortId = getCurrentPageShortId() || generateShortId(); + let shortId = getCurrentPageShortId() || generateShortId(); + + const url = new URL(window.location.href); + const pathname = url.pathname; + const pathParts = pathname.split('/').filter(part => part); + + const Elpassword = document.getElementById("password"); + const ElementpasswordEncripted = document.getElementById("password-encripted"); + + let hash = encryptSha256(Elpassword.value); + + // Elpasswordがdisabledになっている場合は、ElementpasswordEncriptedの値を使う + if (Elpassword && Elpassword.hasAttribute("disabled") && ElementpasswordEncripted) { + hash = ElementpasswordEncripted.getAttribute("value"); + } + const dataToSend = { shortid: shortId, title: edittitleField.value, @@ -219,22 +254,46 @@ const handleShareButtonClick = async () => { createdby: getOrCreateUserShortId(), }; let isPassword = false; - // #password がある場合はパスワードを送信 - const password = document.getElementById("password"); - if (password.value) { + + // share/pw/ がある場合はパスワードを必ず送信 + if (pathParts.length === 3 && pathParts[0] === 'share' && pathParts[1] === "pw") { isPassword = true; - dataToSend["password"] = encryptSha256(password.value); + dataToSend["password"] = hash; dataToSend["pw"] = "true"; - dataToSend["title"] = encryptAES(dataToSend["title"], password.value); - dataToSend["source"] = encryptAES(dataToSend["source"], password.value); } + // share/ の場合は、パスワードがあればshortIdを変更して送信 + else if (pathParts.length === 2 && pathParts[0] === 'share') { + if (Elpassword.value) { + isPassword = true; + shortId = generateShortId(); + dataToSend["password"] = hash; + dataToSend["pw"] = "true"; + } + } + else { + // #password がある場合はパスワードを送信 + if (Elpassword.value) { + isPassword = true; + dataToSend["password"] = hash; + dataToSend["pw"] = "true"; + } + } + + + + console.debug('Sending data to GAS:', dataToSend); try { const response = await postDataToGAS(dataToSend); if (response.error) { console.error('Error sending data to GAS:', response.error); + const errorElement = document.querySelector("#messages"); + if (errorElement) { + errorElement.innerHTML = response.error; + errorElement.style.padding = "1em"; + } } else if (isPassword) { window.location.href = `/share/pw/${shortId}`; diff --git a/src/script/helper.ts b/src/script/helper.ts index 67355e3..d0d8e6d 100644 --- a/src/script/helper.ts +++ b/src/script/helper.ts @@ -1,7 +1,7 @@ import { ftml } from './worker'; import { setTextContentForElement } from './utils'; -const GAS_API_URL = "https://script.google.com/macros/s/AKfycbyUuBsrBvyy_QdstSaOYK6RuPl3LonBpayL_c-KsnUza5TFG5kuBdQ_J91Y7GBJyNmEMQ/exec" +const GAS_API_URL = "https://script.google.com/macros/s/AKfycbxFUwsKJtnPNIwfUqg4BGwaT8N1E9l7REkEza1VFmAssXGlvWyhWKs4-GR-Mp0UWzMygg/exec" diff --git a/src/script/utils.ts b/src/script/utils.ts index 6be2047..57f16de 100644 --- a/src/script/utils.ts +++ b/src/script/utils.ts @@ -61,4 +61,29 @@ export function encryptAES(data: string, key: string) { export function decryptAES(data: string, key: string) { return crypto.AES.decrypt(data, key).toString(crypto.enc.Utf8); -} \ No newline at end of file +} + + +export function setCookie(shortid: string, hash: string, daysToExpire: number = 7) { + var date = new Date(); + date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000)); + var expires = "expires=" + date.toUTCString(); + document.cookie = "FtmlPWHash_" + shortid + "=" + hash + ";" + expires + ";path=/;Secure;"; +} + +export function getCookie(shortid: string): string | null { + const name = "FtmlPWHash_" + shortid + "="; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === ' ') { + c = c.substring(1); + } + if (c.indexOf(name) === 0) { + return c.substring(name.length, c.length); + } + } + return null; +} + \ No newline at end of file