1 line
7.1 KiB
JavaScript
1 line
7.1 KiB
JavaScript
const _$=document.querySelector.bind(document),_$$=document.querySelectorAll.bind(document),_$$$=document.getElementById.bind(document);async function fcn_ajaxPost(e={},t=null,n={}){t&&!t.startsWith("http")&&(t=fictioneer_ajax.rest_url+t),t=t||fictioneer_ajax.ajax_url;let o={"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"};o={...o,...n},e={nonce:fcn_getNonce(),...e};const c=await fetch(t,{method:"POST",credentials:"same-origin",headers:o,mode:"same-origin",body:new URLSearchParams(e)});return c.ok?c.json():Promise.reject(c)}async function fcn_ajaxGet(e={},t=null,n={}){t&&!t.startsWith("http")&&(t=fictioneer_ajax.rest_url+t),t=t||fictioneer_ajax.ajax_url,t=fcn_buildUrl(e={nonce:fcn_getNonce(),...e},t);let o={"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"};o={...o,...n};const c=await fetch(t,{method:"GET",credentials:"same-origin",headers:o,mode:"same-origin"});return c.ok?c.json():Promise.reject(c)}function fcn_evaluateAsBoolean(e,t=!1){if(void 0===e)return t;if("boolean"==typeof e)return e;if(e instanceof HTMLInputElement&&"checkbox"===e.getAttribute("type"))return e.checked;e instanceof HTMLElement&&(e.hasAttribute("value")?e=e.value:e.hasAttribute("data-value")&&(e=e.dataset.value));const n=String(e),o=parseInt(e);return"true"===n||"1"===n||1===o||"false"!==n&&"0"!==n&&0!==o&&t}function fcn_copyToClipboard(e,t=!1){t=t||fictioneer_tl.notification.copiedToClipboard,navigator.clipboard&&(navigator.clipboard.writeText(e),t&&fcn_showNotification(t,2))}function fcn_parseJSON(e){if(null==e||"string"!=typeof e)return null;try{return JSON.parse(e)}catch(e){return null}}function fcn_removeItemOnce(e,t){var n=e.indexOf(t);return n>-1&&e.splice(n,1),e}function fcn_clamp(e,t,n){return Math.min(Math.max(n,e),t)}function fcn_updateThemeColor(e=!1){const t=fcn_siteSettings.darken?fcn_siteSettings.darken:0,n=fcn_siteSettings.saturation?fcn_siteSettings.saturation:0,o=fcn_siteSettings["hue-rotate"]?fcn_siteSettings["hue-rotate"]:0,c=t>=0?1+t**2:1-t**2,i=n>=0?1+n**2:1-n**2;let r=getComputedStyle(document.documentElement).getPropertyValue("--theme-color-base").trim().split(" ");r=`hsl(${(parseInt(r[0])+o)%360}deg ${(parseInt(r[1])*i).toFixed(2)}% ${(parseInt(r[2])*c).toFixed(2)}%)`,_$("meta[name=theme-color]").setAttribute("content",e||r)}function fcn_offset(e){const t=e.getBoundingClientRect();return{top:t.top+window.scrollY,left:t.left+window.scrollX}}function fcn_throttle(e,t,n){var o,c,i,r=null,a=0;n||(n={});var l=function(){a=!1===n.leading?0:Date.now(),r=null,i=e.apply(o,c),r||(o=c=null)};return function(){var s=Date.now();a||!1!==n.leading||(a=s);var u=t-(s-a);return o=this,c=arguments,u<=0||u>t?(r&&(clearTimeout(r),r=null),a=s,i=e.apply(o,c),r||(o=c=null)):r||!1===n.trailing||(r=setTimeout(l,u)),i}}var fcn_lastClicked,fcn_animFrameEvents=new Map;function fcn_bindEventToAnimationFrame(e,t,n=window){n.addEventListener(e,(function(){fcn_animFrameEvents.get(t)||(fcn_animFrameEvents.set(t,!0),requestAnimationFrame((()=>{n.dispatchEvent(new CustomEvent(t)),fcn_animFrameEvents.set(t,!1)})))}))}function fcn_toggleLastClicked(e){const t=!e.classList.contains("last-clicked");e.classList.toggle("last-clicked",t),e.closest(".watch-last-clicked")?.classList.toggle("has-last-clicked",t),fcn_lastClicked&&fcn_lastClicked!=e&&fcn_removeLastClick(fcn_lastClicked),fcn_lastClicked=e}function fcn_removeLastClick(e){e.closest(".watch-last-clicked")?.classList.remove("has-last-clicked"),e.classList.remove("last-clicked"),fcn_lastClicked=null}function fcn_cleanTextSelectionFromButtons(e){return e=(e=(e=(e=(e=(e=e.replace(/[\r\n]{2,}/g,"__$__")).replace(new RegExp("(__Bookmark|__Quote|__Link)","g"),"")).replace(new RegExp("(__Bookmark|__Quote|__TTS|__Link)","g"),"")).replace(new RegExp("(__Bookmark|__Quote|__Suggestion|__TTS|__Link)","g"),"")).replace(new RegExp("(__Bookmark|__Quote|__Suggestion|__Link)","g"),"")).replace(/[__$]{1,}/g,"\n\n").replace(/^[\r\n]+|[\r\n]+$/g,"")}function fcn_deleteCookie(e){document.cookie=e+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"}function fcn_deleteAllCookies(){localStorage.clear(),document.cookie.split(";").forEach((e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires="+(new Date).toUTCString()+";path=/")}))}function fcn_setCookie(e,t,n=30){const o=new Date;o.setTime(o.getTime()+24*n*60*60*1e3);const c="expires="+o.toUTCString();document.cookie=e+"="+encodeURIComponent(t)+";"+c+";SameSite=Strict;path=/"}function fcn_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}function fcn_isValidUrl(e){return!!e&&null!=e.match(/^(https?:\/\/)/)}function fcn_getNonce(){return _$$$("fictioneer-ajax-nonce")?.value??_$$$("general-fictioneer-nonce")?.value??_$('[name="fictioneer_nonce"]')?.value??0}function fcn_matchFingerprint(e){const t=fcn_getUserData().fingerprint;return!!t&&t===e}function fcn_buildUrl(e={},t=null){return 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}function fcn_buildErrorNotice(e,t=!1,n=!0){console.error("Error Message",e);const o=document.createElement("div");let c=e;return t&&(o.id=t),o.classList="notice _warning","object"==typeof e&&(c="",e.status&&(c=`${e.status}: `),e.statusText&&(c+=e.statusText),c||(c="Unknown error.")),o.innerHTML=`<i class="fa-solid fa-triangle-exclamation"></i><div>${n?fcn_sanitizeHTML(c):c}</div>`,o}function fcn_sanitizeHTML(e){const t=document.createElement("div");return t.innerText=e instanceof HTMLElement?e.innerHTML:e,t.innerHTML}function fcn_detectScreenCollision(e){const t=e.getBoundingClientRect(),n=window.innerHeight??document.documentElement.clientHeight,o=window.innerWidth??document.documentElement.clientWidth,c=(e.closest(".popup-menu-toggle")?.clientHeight??32)+16,i=n-t.bottom,r=[];return t.top<=50&&i>50+c&&r.push("top"),t.bottom>=n-50&&t.top>50+c&&r.push("bottom"),t.left<=10&&r.push("left"),t.right>=o-10&&r.push("right"),r}function fcn_scrollTo(e,t=64){window.scrollTo({top:e.getBoundingClientRect().top+window.scrollY-t,behavior:"smooth"})}function fcn_html(...e){const t=document.createElement("template");return t.innerHTML=String.raw(...e).trim(),t.content.firstChild}function fcn_splitList(e,t=","){if(!e||""===e.trim())return[];let n=e.replace(/\r?\n|\r/g,"").split(t);return n=n.map((e=>e.trim())).filter((e=>e.length>0)),n}function fcn_isSearchEngineCrawler(){const e=navigator.userAgent.toLowerCase();return["googlebot","bingbot","slurp","duckduckbot","baiduspider","yandexbot","sogou","exabot","facebot","ia_archiver"].some((t=>e.includes(t)))}_$("body").addEventListener("click",(e=>{!["BUTTON","A"].includes(e.target.tagName)&&e.target.closest(".toggle-last-clicked")||null!==e.target.closest(".escape-last-click")||fcn_lastClicked&&e.currentTarget!=fcn_lastClicked&&fcn_removeLastClick(fcn_lastClicked)})),_$("body").addEventListener("keydown",(e=>{27==e.keyCode&&fcn_lastClicked&&(fcn_removeLastClick(fcn_lastClicked),document.activeElement?.blur())})),_$$$("full-navigation")?.addEventListener("mouseover",(()=>{fcn_lastClicked&&(fcn_removeLastClick(fcn_lastClicked),document.activeElement?.blur())})); |