1 line
2.6 KiB
JavaScript
1 line
2.6 KiB
JavaScript
function fcn_replaceProfileImage(e,t){const a=e.querySelector(".user-icon");if(a){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=t,a.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,t=!0){e&&fcn_isValidUrl(e)&&(t&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((t=>{fcn_replaceProfileImage(t,e)})),!1===fcn_getUserData().loggedIn&&fcn_prepareLogin())}function fcn_getProfileImage(){let e=localStorage.getItem("fcnProfileAvatar");fcn_isLoggedIn?(fcn_isValidUrl(e)||(e=!1),e?fcn_setProfileImage(e):fcn_getUserAvatar()):localStorage.removeItem("fcnProfileAvatar")}function fcn_getUserAvatar(){fcn_ajaxGet({action:"fictioneer_ajax_get_avatar",fcn_fast_ajax:1}).then((e=>{e.success&&fcn_setProfileImage(e.data.url)})).catch((()=>{fcn_theRoot.dataset.defaultAvatar&&fcn_setProfileImage(fcn_theRoot.dataset.defaultAvatar,!1)}))}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,loggedIn:"pending",follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){let e=fcn_getUserData();if(fcn_isLoggedIn&&!1===e.loggedIn&&(fcn_prepareLogin(),e=fcn_getUserData()),fcn_ajaxLimitThreshold<e.lastLoaded||!1===e.loggedIn)if(e.loggedIn){const t=new CustomEvent("fcnUserDataReady",{detail:{data:e,time:new Date},bubbles:!1,cancelable:!0});document.dispatchEvent(t)}else{const t=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}else fcn_ajaxGet({action:"fictioneer_ajax_get_user_data",fcn_fast_ajax:1}).then((e=>{if(e.success){let t=fcn_getUserData();t=e.data,t.lastLoaded=Date.now(),fcn_setUserData(t);const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}else{const t=fcn_getUserData();t.lastLoaded=Date.now(),t.loggedIn=!1,fcn_setUserData(t);const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData");const t=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}))}document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_getProfileImage()})),fcn_theRoot.dataset.ajaxAuth&&document.addEventListener("fcnAuthReady",(()=>{fcn_getProfileImage()})),document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_fetchUserData()})); |