2024-12-21 23:33:22 +01:00
|
|
|
const _$=document.querySelector.bind(document),_$$=document.querySelectorAll.bind(document),_$$$=document.getElementById.bind(document),FcnUtils={userDataDefaults:{lastLoaded:0,timestamp:0,loggedIn:"pending",follows:!1,reminders:!1,checkmarks:!1,bookmarks:null,likes:null,fingerprint:!1,nonceHtml:"",nonce:"",isAdmin:!1,isModerator:!1,isAuthor:!1,isEditor:!1},userData(){const e=FcnUtils.parseJSON(localStorage.getItem("fcnUserData"))||{};return{...FcnUtils.userDataDefaults,...e}},resetUserData(){const e=FcnUtils.parseJSON(localStorage.getItem("fcnUserData"))||{},t={lastLoaded:e.lastLoaded??0,loggedIn:(!FcnGlobals.ajaxAuth||!1!==e.loggedIn)&&"pending"};localStorage.setItem("fcnUserData",JSON.stringify({...e,...FcnUtils.userDataDefaults,...t}))},removeUserData(){localStorage.removeItem("fcnUserData")},setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))},loggedInCache:null,loggedInCacheTime:0,loggedIn(){const e=Date.now();return null!==FcnUtils.loggedInCache&&e-FcnUtils.loggedInCacheTime<20||(FcnUtils.loggedInCache=FcnUtils.hasLoginCookie(),FcnUtils.loggedInCacheTime=e),FcnUtils.loggedInCache},hasLoginCookie(){const e=document.cookie.split(";");for(let t=0;t<e.length;t++){if(-1!==e[t].trim().indexOf("fcnLoggedIn="))return!0}return!1},async aGet(e={},t=null,n={}){try{return fcn_ajaxGet(e,t,n)}catch(e){throw console.error("aGet Error:",e),e}},async aPost(e={},t=null,n={}){try{return await fcn_ajaxPost(e,t,n)}catch(e){throw console.error("aPost Error:",e),e}},remoteAction(e,{element:t=null,callback:n=null,errorCallback:o=null,finalCallback:a=null,nonce:r=null,fast:i=!0,payload:l={}}={}){t?.classList.add("ajax-in-progress"),FcnUtils.aPost({action:e,fcn_fast_ajax:!!i,nonce:r??FcnUtils.nonce(),...l}).then((e=>{n&&n(e,t),e.success||(fcn_showNotification(e.data.failure??e.data.error??fictioneer_tl.notification.error,10,"warning"),(e.data.error||e.data.failure)&&console.error("Error:",e.data.error??e.data.failure))})).catch((e=>{429===e.status?fcn_showNotification(fictioneer_tl.notification.slowDown,3,"warning"):e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning"),o&&o(e,t),console.error(e)})).then((()=>{t?.classList.remove("ajax-in-progress"),a&&a(t)}))},parseJSON(e){if(null==e||"string"!=typeof e)return null;try{return JSON.parse(e)}catch(e){return null}},copyToClipboard(e,t=!1){t=t||fictioneer_tl.notification.copiedToClipboard,navigator.clipboard&&(navigator.clipboard.writeText(e),t&&fcn_showNotification(t,2))},clamp:(e,t,n)=>Math.min(Math.max(n,e),t),offset(e){const t=e.getBoundingClientRect();return{top:t.top+window.scrollY,left:t.left+window.scrollX}},throttle(e,t,n){var o,a,r,i=null,l=0;n||(n={});var s=function(){l=!1===n.leading?0:Date.now(),i=null,r=e.apply(o,a),i||(o=a=null)};return function(){var c=Date.now();l||!1!==n.leading||(l=c);var u=t-(c-l);return o=this,a=arguments,u<=0||u>t?(i&&(clearTimeout(i),i=null),l=c,r=e.apply(o,a),i||(o=a=null)):i||!1===n.trailing||(i=setTimeout(s,u)),r}},deleteCookie(e){document.cookie=e+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"},deleteAllCookies(){localStorage.clear(),document.cookie.split(";").forEach((e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires="+(new Date).toUTCString()+";path=/")}))},setCookie(e,t,n=30){const o=new Date;o.setTime(o.getTime()+24*n*60*60*1e3);const a="expires="+o.toUTCString();document.cookie=e+"="+encodeURIComponent(t)+";"+a+";SameSite=Strict;path=/"},getCookie(e){const t=e+"=",n=document.cookie.split(";");for(var o=0;o<n.length;o++){const e=n[o].trim();if(0==e.indexOf(t))return decodeURIComponent(e.substring(t.length,e.length))}return null},nonce:()=>_$$$("fictioneer-ajax-nonce")?.value??_$$$("general-fictioneer-nonce")?.value??_$('[name="fictioneer_nonce"]')?.value??0,buildUrl:(e={},t=null)=>(t=t?new URL(t):new URL(window.location.protocol+"//"+window.location.host+window.location.pathname),e&&Object.keys(e).forEach((n=>{t.searchParams.append(n,e[n])})),t),buildErrorNotice(e,t=!1,n=!0){console.error("Error:",e);const o=document.createElement("div");let a=e;return t&&(o.id=t),o.classList
|