TBH this probably did not interfere due to there only being two such custom events registered that never occur simultaneously, but I rather have this properly just in case. Also, "running" was a bit generic for a global variable name.
1 line
6.7 KiB
JavaScript
1 line
6.7 KiB
JavaScript
const _$=document.querySelector.bind(document),_$$=document.querySelectorAll.bind(document),_$$$=document.getElementById.bind(document);async function fcn_ajaxPost(e={},n=null,t={}){n||(n=fictioneer_ajax.ajax_url),final_headers={"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"},final_headers={...final_headers,...t},e={nonce:fcn_getNonce(),...e};const c=await fetch(n,{method:"POST",credentials:"same-origin",headers:final_headers,mode:"same-origin",body:new URLSearchParams(e)});return c.ok?c.json():Promise.reject(c)}async function fcn_ajaxGet(e={},n=null,t={}){n=n||fictioneer_ajax.ajax_url,n=fcn_buildUrl(e={nonce:fcn_getNonce(),...e},n),final_headers={"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"},final_headers={...final_headers,...t};const c=await fetch(n,{method:"GET",credentials:"same-origin",headers:final_headers,mode:"same-origin"});return c.ok?c.json():Promise.reject(c)}function fcn_evaluateAsBoolean(e,n=!1){if("boolean"==typeof e)return e;if(void 0===e)return n;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 t=String(e),c=parseInt(e);return"true"===t||"1"===t||1===c||"false"!==t&&"0"!==t&&0!==c&&n}function fcn_copyToClipboard(e,n=!1){n=n||__("Copied to clipboard!","fictioneer"),navigator.clipboard&&(navigator.clipboard.writeText(e),n&&fcn_showNotification(n,2))}function fcn_isValidJSONString(e){try{JSON.parse(e)}catch(e){return!1}return!0}function fcn_removeItemOnce(e,n){var t=e.indexOf(n);return t>-1&&e.splice(t,1),e}function fcn_clamp(e,n,t){return Math.min(Math.max(t,e),n)}function fcn_updateThemeColor(e=!1){const n=fcn_siteSettings.darken?fcn_siteSettings.darken:0,t=fcn_siteSettings.saturation?fcn_siteSettings.saturation:0,c=fcn_siteSettings["hue-rotate"]?fcn_siteSettings["hue-rotate"]:0,o=n>=0?1+Math.pow(n,2):1-Math.pow(n,2),i=t>=0?1+Math.pow(t,2):1-Math.pow(t,2);let a=fcn_cssVars.getPropertyValue("--theme-color-base").trim().split(" ");a=`hsl(${(parseInt(a[0])+c)%360}deg ${(parseInt(a[1])*i).toFixed(2)}% ${(parseInt(a[2])*o).toFixed(2)}%)`,_$("meta[name=theme-color]").setAttribute("content",e||a)}function fcn_offset(e){const n=e.getBoundingClientRect();return{top:n.top+window.scrollY,left:n.left+window.scrollX}}function fcn_throttle(e,n,t){var c,o,i,a=null,r=0;t||(t={});var l=function(){r=!1===t.leading?0:Date.now(),a=null,i=e.apply(c,o),a||(c=o=null)};return function(){var s=Date.now();r||!1!==t.leading||(r=s);var u=n-(s-r);return c=this,o=arguments,u<=0||u>n?(a&&(clearTimeout(a),a=null),r=s,i=e.apply(c,o),a||(c=o=null)):a||!1===t.trailing||(a=setTimeout(l,u)),i}}var fcn_lastClicked,fcn_animFrameEvents=new Map;function fcn_bindEventToAnimationFrame(e,n,t=window){t.addEventListener(e,(function(){fcn_animFrameEvents.get(n)||(fcn_animFrameEvents.set(n,!0),requestAnimationFrame((()=>{t.dispatchEvent(new CustomEvent(n)),fcn_animFrameEvents.set(n,!1)})))}))}function fcn_toggleLastClicked(e){const n=!e.classList.contains("last-clicked");e.classList.toggle("last-clicked",n),e.closest(".watch-last-clicked")?.classList.toggle("has-last-clicked",n),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,n,t=30){const c=new Date;c.setTime(c.getTime()+24*t*60*60*1e3);const o="expires="+c.toUTCString();document.cookie=e+"="+encodeURIComponent(n)+";"+o+";SameSite=Strict;path=/"}function fcn_getCookie(e){const n=e+"=",t=document.cookie.split(";");for(var c=0;c<t.length;c++){const e=t[c].trim();if(0==e.indexOf(n))return decodeURIComponent(e.substring(n.length,e.length))}return null}function fcn_isValidUrl(e){return!!e&&null!=e.match(/^(https?:\/\/)/)}_$("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())}));var fcn_defaultNonce=_$$$("fictioneer-nonce")?.value??0;function fcn_getNonce(){return _$$$("fictioneer-ajax-nonce")?.value??fcn_defaultNonce}function fcn_matchFingerprint(e){return"undefined"!=typeof fcn_fingerprint&&fcn_fingerprint.fingerprint===e}function fcn_buildUrl(e={},n=null){return n=n?new URL(n):new URL(window.location.protocol+"//"+window.location.host+window.location.pathname),e&&Object.keys(e).forEach((t=>{n.searchParams.append(t,e[t])})),n}function fcn_buildErrorNotice(e,n=!1){const t=document.createElement("div");let c=e;return n&&(t.id=n),t.classList="notice _warning","object"==typeof e&&(c="",e.status&&(c=`${e.status}: `),e.statusText&&(c+=e.statusText),c||(c="Unknown error.")),t.innerHTML=`<i class="fa-solid fa-triangle-exclamation"></i><div>${fcn_sanitizeHTML(c)}</div>`,t}function fcn_sanitizeHTML(e){const n=document.createElement("div");return n.innerText=e instanceof HTMLElement?e.innerHTML:e,n.innerHTML}function fcn_resizeInput(e,n=null){n=n||e.value.length,e.style.width=.88*n+2+"ch"}function fcn_ariaCheckedUpdate(e){const n=e.closest('[role="checkbox"][aria-checked]');if(n){const t=fcn_evaluateAsBoolean(e);n.ariaChecked=t}}function fcn_scrollToAnchor(e){return _$(`[name="${e.getAttribute("href").replace("#","")}"]`).scrollIntoView({behavior:"smooth",block:"center"}),!1}function fcn_detectScreenCollision(e){const n=e.getBoundingClientRect(),t=window.innerHeight??document.documentElement.clientHeight,c=(e.closest(".popup-menu-toggle")?.clientHeight??32)+16,o=t-n.bottom-e.clientHeight,i=n.top-e.clientHeight,a=[];return n.top<=50&&o>50+c&&a.push("top"),n.bottom>=t-50&&i>50+c&&a.push("bottom"),a}function fcn_scrollTo(e,n=64){window.scrollTo({top:e.getBoundingClientRect().top+window.scrollY-n,behavior:"smooth"})} |