|
1
|
|
-(function(){"use strict";const u="X-WP-Nonce";function L(e,t){const o=k();if(!o)return t;const n=E(e);if(!n||!_(n))return t;const s=t?.headers??(typeof Request<"u"&&e instanceof Request?e.headers:void 0),r=new Headers(s??{});return r.has(u)?t:(r.set(u,o),{...t??{},headers:r})}function k(){if(typeof window>"u")return;const t=window.openStationConfig?.restNonce;return typeof t=="string"&&t.length>0?t:void 0}function E(e){try{const t=typeof window<"u"&&window.location?window.location.href:void 0;return typeof e=="string"?new URL(e,t):e instanceof URL?e:typeof Request<"u"&&e instanceof Request?new URL(e.url,t):null}catch{return null}}function _(e){return typeof window>"u"||!window.location||e.origin!==window.location.origin?!1:!!(e.pathname.includes("/wp-json/")||e.searchParams.has("rest_route"))}function N(e,t,o={}){const n=window.wp?.os?.fetch;if(typeof n=="function")return n(e,t,o);const s=L(e,t);return fetch(e,s)}function R(e){const t=document.createElement("textarea");return t.innerHTML=e,t.value}const b="desktop-mode/starter",S=async(e,t)=>{let o=!1;const n=document.createElement("div");n.className="dm-starter";const s=document.createElement("div");s.className="dm-starter__header",s.textContent="Starter Widget";const r=document.createElement("div");r.className="dm-starter__body",r.textContent="Loading…",n.appendChild(s),n.appendChild(r),e.appendChild(n);const f=t.storage.get("clicks")??0,i=document.createElement("button");i.className="dm-starter__counter",i.textContent=`Clicked ${f} ${f===1?"time":"times"}`;const w=()=>{const c=(t.storage.get("clicks")??0)+1;t.storage.set("clicks",c),i.textContent=`Clicked ${c} ${c===1?"time":"times"}`};i.addEventListener("click",w),n.appendChild(i);const x=window.wpApiSettings?.root??"/wp-json/",m=async()=>{if(!o)try{const d=await N(x.replace(/\/$/,"")+"/wp/v2/posts?per_page=1&orderby=date&order=desc&_fields=id,title",{credentials:"same-origin"},{source:"desktop-mode/starter",silent:!0});if(o)return;if(!d.ok){r.textContent="Could not load posts ("+d.status+").";return}const c=await d.json();if(o)return;r.textContent=c.length>0?"Latest post: "+R(c[0].title.rendered):"No posts found."}catch{o||(r.textContent="Could not load data.")}};await m();const p=6e4;let a=null,g=Date.now();const h=()=>{g=Date.now(),m()},v=()=>{a===null&&(a=setInterval(h,p))},C=()=>{a!==null&&(clearInterval(a),a=null)},y=()=>{if(document.hidden){C();return}Date.now()-g>=p&&h(),v()};return document.addEventListener("visibilitychange",y),document.hidden||v(),()=>{o=!0,C(),document.removeEventListener("visibilitychange",y),i.removeEventListener("click",w)}},l=window;l.openStationWidgets=l.openStationWidgets??{},l.openStationWidgets[b]=S})(); |
|
1
|
+(function(){"use strict";const w="X-WP-Nonce";function M(e,t){const n=I();if(!n)return t;const r=$(e);if(!r||!x(r))return t;const o=t?.headers??(typeof Request<"u"&&e instanceof Request?e.headers:void 0),a=new Headers(o??{});return a.has(w)?t:(a.set(w,n),{...t??{},headers:a})}function I(){if(typeof window>"u")return;const t=window.openStationConfig?.restNonce;return typeof t=="string"&&t.length>0?t:void 0}function $(e){try{const t=typeof window<"u"&&window.location?window.location.href:void 0;return typeof e=="string"?new URL(e,t):e instanceof URL?e:typeof Request<"u"&&e instanceof Request?new URL(e.url,t):null}catch{return null}}function x(e){return typeof window>"u"||!window.location||e.origin!==window.location.origin?!1:!!(e.pathname.includes("/wp-json/")||e.searchParams.has("rest_route"))}function D(e,t,n={}){const r=window.wp?.os?.fetch;if(typeof r=="function")return r(e,t,n);const o=M(e,t);return fetch(e,o)}class m extends Error{constructor(t,n){super(t||n.serverMessage||`HTTP ${n.status}`),this.name="RestError",this.status=n.status,this.code=n.code,this.data=n.data,this.serverMessage=n.serverMessage??""}}function p(e){return e instanceof m}function S(e){const t=typeof e=="object"&&e!==null?e:void 0;return{code:typeof t?.code=="string"?t.code:void 0,data:t?.data,serverMessage:typeof t?.message=="string"?t.message:""}}function F(e,t,n=""){return new m(n,{status:e,...S(t)})}async function H(e,t=""){let n=null;try{n=await e.json()}catch{n=null}return F(e.status,n,t)}const O="desktop-mode";function v(){return window.wp?.i18n}function d(e,t=O){return v()?.__(e,t)??e}function k(e,...t){const n=v()?.sprintf;if(n)return n(e,...t);let r=0;return e.replace(/%(?:(\d+)\$)?[sd]/g,(o,a)=>{const s=a?Number.parseInt(a,10)-1:r++;return String(t[s]??"")})}const P="rest_cookie_invalid_nonce",y=()=>d("Could not reach the site. Check your connection and try again.","desktop-mode");function E(e){return typeof navigator<"u"&&navigator.onLine===!1?!0:e instanceof TypeError}function j(e){return p(e)?e.code===P||e.status===401?"session":e.status>=200&&e.status<300?"unreadable":e.status===404?"gone":e.status>=500?"server":e.status>=400?"refused":"unknown":E(e)?"offline":"unknown"}function U(e){return p(e)?e.serverMessage:E(e)?y():e instanceof Error?e.message:""}function W(e,t){const{fallback:n,lead:r}=t,o=j(e),a=o==="offline"||o==="unreadable"?"warning":"error";let s="",i=!0;switch(o){case"offline":s=y();break;case"session":s=d("Your session has expired. Reload the page and try again.","desktop-mode");break;case"unreadable":s=d("The site sent an unreadable reply. Reload the page to check whether the change went through.","desktop-mode");break;case"gone":case"refused":case"server":{const u=e;u.serverMessage?s=u.serverMessage:o==="gone"?(s=d("It no longer exists. Reload the page to catch up.","desktop-mode"),i=!1):o==="server"?(s=k(d("The server answered with error %d.","desktop-mode"),u.status),i=!1):(s=k(d("The server refused the request (HTTP %d).","desktop-mode"),u.status),i=!1);break}case"unknown":default:s=U(e);break}let c;return r?c=s?`${r}: ${s}`:n:s?i?c=s:c=`${n} ${s}`:c=n,{message:c,reason:s,kind:o,type:a}}function q(e){const t=document.createElement("textarea");return t.innerHTML=e,t.value}const A="desktop-mode/starter",V=async(e,t)=>{let n=!1;const r=document.createElement("div");r.className="dm-starter";const o=document.createElement("div");o.className="dm-starter__header",o.textContent="Starter Widget";const a=document.createElement("div");a.className="dm-starter__body",a.textContent="Loading…",r.appendChild(o),r.appendChild(a),e.appendChild(r);const s=t.storage.get("clicks")??0,i=document.createElement("button");i.className="dm-starter__counter",i.textContent=`Clicked ${s} ${s===1?"time":"times"}`;const c=()=>{const f=(t.storage.get("clicks")??0)+1;t.storage.set("clicks",f),i.textContent=`Clicked ${f} ${f===1?"time":"times"}`};i.addEventListener("click",c),r.appendChild(i);const u=window.wpApiSettings?.root??"/wp-json/",_=async()=>{if(!n)try{const l=await D(u.replace(/\/$/,"")+"/wp/v2/posts?per_page=1&orderby=date&order=desc&_fields=id,title",{credentials:"same-origin"},{source:"desktop-mode/starter",silent:!0});if(n)return;if(!l.ok)throw await H(l);const f=await l.json();if(n)return;a.textContent=f.length>0?"Latest post: "+q(f[0].title.rendered):"No posts found."}catch(l){n||(a.textContent=W(l,{fallback:"Could not load posts."}).message)}};await _();const b=6e4;let g=null,C=Date.now();const R=()=>{C=Date.now(),_()},L=()=>{g===null&&(g=setInterval(R,b))},N=()=>{g!==null&&(clearInterval(g),g=null)},T=()=>{if(document.hidden){N();return}Date.now()-C>=b&&R(),L()};return document.addEventListener("visibilitychange",T),document.hidden||L(),()=>{n=!0,N(),document.removeEventListener("visibilitychange",T),i.removeEventListener("click",c)}},h=window;h.openStationWidgets=h.openStationWidgets??{},h.openStationWidgets[A]=V})(); |