1 line
9.9 KiB
JavaScript
1 line
9.9 KiB
JavaScript
function fcn_unsetOauth(t){const n=prompt(t.dataset.warning);if(!n||n.toLowerCase()!=t.dataset.confirm.toLowerCase())return;const e=_$$$(`oauth-${t.dataset.channel}`);e.classList.add("ajax-in-progress"),fcn_ajaxPost(payload={action:"fictioneer_ajax_unset_my_oauth",nonce:t.dataset.nonce,channel:t.dataset.channel,id:t.dataset.id}).then((t=>{t.success?(e.classList.remove("_connected"),e.classList.add("_disconnected"),e.querySelector("button").remove(),fcn_showNotification(e.dataset.unset)):(e.style.background="var(--notice-warning-background)",fcn_showNotification(t.data.failure??t.data.error??fictioneer_tl.notification.error,5,"warning"),(t.data.error||t.data.failure)&&console.error("Error:",t.data.error??t.data.failure))})).catch((t=>{t.status&&t.statusText&&(e.style.background="var(--notice-warning-background)",fcn_showNotification(`${t.status}: ${t.statusText}`,5,"warning")),console.error(t)})).then((()=>{e.classList.remove("ajax-in-progress")}))}function fcn_deleteMyAccount(t){if(_$$$("button-delete-my-account").hasAttribute("disabled"))return;const n=prompt(t.dataset.warning);n&&n.toLowerCase()==t.dataset.confirm.toLowerCase()&&(_$$$("button-delete-my-account").setAttribute("disabled",!0),fcn_ajaxPost({action:"fictioneer_ajax_delete_my_account",nonce:t.dataset.nonce,id:t.dataset.id}).then((t=>{t.success?(localStorage.removeItem("fcnAuth"),localStorage.removeItem("fcnProfileAvatar"),location.reload()):(_$$$("button-delete-my-account").innerHTML=t.data.button,fcn_showNotification(t.data.failure??t.data.error??fictioneer_tl.notification.error,5,"warning"),(t.data.error||t.data.failure)&&console.error("Error:",t.data.error??t.data.failure))})).catch((t=>{t.status&&t.statusText&&(fcn_showNotification(`${t.status}: ${t.statusText}`,5,"warning"),_$$$("button-delete-my-account").innerHTML=response.data.button),console.error(t)})))}_$$(".button-unset-oauth").forEach((t=>{t.addEventListener("click",(t=>{fcn_unsetOauth(t.currentTarget)}))})),_$$$("button-delete-my-account")?.addEventListener("click",(t=>{fcn_deleteMyAccount(t.currentTarget)}));const fcn_profileDataTranslations=_$$$("profile-data-translations")?.dataset;function fcn_dataDeletionPrompt(t){const n=prompt(t.dataset.warning);return!(!n||n.toLowerCase()!=t.dataset.confirm.toLowerCase())}function fcn_clearData(t,n){const e=t.closest(".card");localStorage.removeItem("fcnBookshelfContent"),e.classList.add("ajax-in-progress"),t.remove(),fcn_ajaxPost({action:n,fcn_fast_ajax:1,nonce:t.dataset.nonce}).then((t=>{t.success?e.querySelector(".card__content").innerHTML=t.data.success:(fcn_showNotification(t.data.failure??t.data.error??fictioneer_tl.notification.error,10,"warning"),(t.data.error||t.data.failure)&&console.error("Error:",t.data.error??t.data.failure))})).catch((t=>{t.status&&t.statusText&&fcn_showNotification(`${t.status}: ${t.statusText}`,10,"warning"),console.error(t)})).then((()=>{e.classList.remove("ajax-in-progress")}))}function fcn_getSkins(){const t=fcn_getCookie("fcnLoggedIn");if(!t)return null;const n={data:{},active:null,fingerprint:t},e=fcn_parseJSON(localStorage.getItem("fcnSkins"))??n;return e?.fingerprint&&t===e.fingerprint?(("object"!=typeof e.data||Array.isArray(e.data))&&(e.data={}),e):n}function fcn_setSkins(t){"object"!=typeof t||null===t||"object"!=typeof t.data||Array.isArray(t.data)?fcn_showNotification(fcn_skinTranslations.invalidJson,3,"warning"):t?.fingerprint&&fcn_getCookie("fcnLoggedIn")===t.fingerprint?localStorage.setItem("fcnSkins",JSON.stringify(t)):fcn_showNotification(fcn_skinTranslations.wrongFingerprint,3,"warning")}function fcn_getSkinInfo(t){const n=t.match(/Name:\s*(.+)/),e=t.match(/Author:\s*(.+)/),a=t.match(/Version:\s*(.+)/);return{name:n?fcn_sanitizeHTML(n[1].trim()):null,author:e?fcn_sanitizeHTML(e[1].trim()):null,version:a?fcn_sanitizeHTML(a[1].trim()):null}}function fcn_toggleSkin(t){const n=t.closest(".custom-skin"),e=fcn_getSkins();n.classList.contains("active")?(_$$(".custom-skin").forEach((t=>t.classList.remove("active"))),e.active=null):(_$$(".custom-skin").forEach((t=>t.classList.remove("active"))),n.classList.add("active"),e.active=t.dataset.skinId),fcn_setSkins(e),fcn_applySkin()}function fcn_deleteSkin(t){const n=t.closest(".custom-skin"),e=fcn_getSkins();n.classList.contains("active")&&(e.active=null),delete e.data[t.dataset.skinId],_$("#css-upload-form > input").value="",fcn_setSkins(e),fcn_renderSkinList(),fcn_applySkin()}function fcn_applySkin(){const t=fcn_getCookie("fcnLoggedIn");if(!t)return;const n=fcn_getSkins();if(_$$$("fictioneer-active-custom-skin")?.remove(),n?.fingerprint===t&&n?.data?.[n.active]?.css){const t=document.createElement("style");t.textContent=n.data[n.active].css,t.id="fictioneer-active-custom-skin",_$("head").appendChild(t)}}function fcn_renderSkinList(){const t=_$(".custom-skin-list"),n=fcn_getCookie("fcnLoggedIn");if(!n||!t)return;const e=fcn_getSkins();t.innerHTML="",e?.fingerprint===n?(e?.data&&Object.keys(e.data).length>0&&(Object.entries(e.data).forEach((([n,a])=>{const r=_$$$("template-custom-skin").content.cloneNode(!0);e.active===n&&r.querySelector(".custom-skin").classList.add("active"),r.querySelector('[data-click-action="skin-toggle"]').dataset.skinId=n,r.querySelector('[data-click-action="skin-delete"]').dataset.skinId=n,r.querySelector('[data-finder="skin-name"]').innerText=a.name,r.querySelector('[data-finder="skin-version"]').innerText=a.version,r.querySelector('[data-finder="skin-author"]').innerText=a.author,t.appendChild(r)})),_$$('[data-click-action="skin-toggle"]').forEach((t=>{t.addEventListener("click",(t=>fcn_toggleSkin(t.currentTarget)))})),_$$('[data-click-action="skin-delete"]').forEach((t=>{t.addEventListener("click",(t=>fcn_deleteSkin(t.currentTarget)))}))),Object.keys(e.data).length>2?_$$$("css-upload-form").style.display="none":_$$$("css-upload-form").style.display=""):_$$$("css-upload-form").style.display=""}function fcn_uploadSkins(t){if(!fcn_isUserLoggedIn()||t.classList.contains("disabled"))return;const n=fcn_getSkins();fcn_toggleInProgress(t),fcn_ajaxPost({action:"fictioneer_ajax_save_skins",fcn_fast_ajax:1,skins:JSON.stringify(n)}).then((t=>{t.success?fcn_showNotification(t.data.message,3,"success"):(fcn_showNotification(t.data.failure??t.data.error??fictioneer_tl.notification.error,3,"warning"),(t.data.error||t.data.failure)&&console.error("Error:",t.data.error??t.data.failure))})).catch((t=>{t.status&&t.statusText&&fcn_showNotification(`${t.status}: ${t.statusText}`,3,"warning"),console.error(t)})).then((()=>{fcn_toggleInProgress(t)}))}function fcn_downloadSkins(t){fcn_isUserLoggedIn()&&!t.classList.contains("disabled")&&(fcn_toggleInProgress(t),fcn_ajaxPost({action:"fictioneer_ajax_get_skins",fcn_fast_ajax:1}).then((t=>{t.success?(fcn_showNotification(t.data.message,3,"success"),fcn_setSkins(JSON.parse(t.data.skins)),fcn_renderSkinList(),fcn_applySkin()):(fcn_showNotification(t.data.failure??t.data.error??fictioneer_tl.notification.error,3,"warning"),(t.data.error||t.data.failure)&&console.error("Error:",t.data.error??t.data.failure))})).catch((t=>{t.status&&t.statusText&&fcn_showNotification(`${t.status}: ${t.statusText}`,3,"warning"),console.error(t)})).then((()=>{_$("#css-upload-form > input").value="",fcn_toggleInProgress(t)})))}_$(".button-clear-comments")?.addEventListener("click",(t=>{fcn_dataDeletionPrompt(t.currentTarget)&&fcn_clearData(t.currentTarget,"fictioneer_ajax_clear_my_comments")})),_$(".button-clear-comment-subscriptions")?.addEventListener("click",(t=>{fcn_dataDeletionPrompt(t.currentTarget)&&fcn_clearData(t.currentTarget,"fictioneer_ajax_clear_my_comment_subscriptions")})),_$(".button-clear-checkmarks")?.addEventListener("click",(t=>{if(!fcn_dataDeletionPrompt(t.currentTarget))return;const n=fcn_getUserData();n.checkmarks={data:{},updated:Date.now()},fcn_setUserData(n),fcn_updateCheckmarksView(),fcn_clearData(t.currentTarget,"fictioneer_ajax_clear_my_checkmarks",!0)})),_$(".button-clear-reminders")?.addEventListener("click",(t=>{if(!fcn_dataDeletionPrompt(t.currentTarget))return;const n=fcn_getUserData();n.reminders={data:{}},fcn_setUserData(n),fcn_updateRemindersView(),fcn_clearData(t.currentTarget,"fictioneer_ajax_clear_my_reminders",!0)})),_$(".button-clear-follows")?.addEventListener("click",(t=>{if(!fcn_dataDeletionPrompt(t.currentTarget))return;const n=fcn_getUserData();n.follows={data:{}},fcn_setUserData(n),fcn_updateFollowsView(),fcn_clearData(t.currentTarget,"fictioneer_ajax_clear_my_follows",!0)})),_$(".button-clear-bookmarks")?.addEventListener("click",(t=>{if(!fcn_dataDeletionPrompt(t.currentTarget))return;const n=fcn_getUserData();n.bookmarks="{}",fcn_setUserData(n),fcn_bookmarks.data={},t.currentTarget.closest(".card").querySelector(".card__content").innerHTML=fcn_profileDataTranslations.clearedSuccess,fcn_setBookmarks(fcn_bookmarks)})),fcn_renderSkinList(),_$("#css-upload-form > input")?.addEventListener("input",(t=>{t.preventDefault();const n=_$$$("css-file").files[0],e=fcn_getSkins(),a=fcn_getCookie("fcnLoggedIn");if(Object.keys(e.data).length>2)return void fcn_showNotification(fcn_skinTranslations.tooManySkins,3,"warning");if(e?.fingerprint!==a)return void fcn_showNotification(fcn_skinTranslations.wrongFingerprint,3,"warning");if(!n)return;if(n.size>2e5)return void fcn_showNotification(fcn_skinTranslations.fileTooLarge,3,"warning");if("text/css"!==n.type)return void fcn_showNotification(fcn_skinTranslations.wrongFileType,3,"warning");const r=new FileReader;r.onload=t=>{const n=t.target.result,e=fcn_getSkinInfo(n),a=fcn_getSkins();if(!fcn_validateCss(n))return void fcn_showNotification(fcn_skinTranslations.invalidCss,5,"warning");if(!e.name)return void fcn_showNotification(fcn_skinTranslations.missingMetaData,3,"warning");const r=btoa(e.name);a.data[r]={name:e.name,version:e.version,author:e.author,css:n},fcn_setSkins(a),fcn_renderSkinList()},r.onerror=()=>{console.error(r.error),fcn_showNotification(r.error,3,"warning")},r.readAsText(n)})),_$('[data-click-action="skins-sync-up"]')?.addEventListener("click",(t=>{fcn_uploadSkins(t.currentTarget)})),_$('[data-click-action="skins-sync-down"]')?.addEventListener("click",(t=>{fcn_downloadSkins(t.currentTarget)})); |