@waline/client
Version:
client for waline comment system
2 lines • 1.65 kB
JavaScript
const e=`3.15.2`,t={"Content-Type":`application/json`},n=e=>`${e.replace(/\/?$/u,`/`)}api/`,r=(e,t=``)=>{if(typeof e==`object`&&e.errno)throw TypeError(`${t} failed with ${e.errno}: ${e.errmsg}`);return e},i=({serverURL:e,lang:t,paths:i,type:a,signal:o})=>fetch(`${n(e)}article?path=${encodeURIComponent(i.join(`,`))}&type=${encodeURIComponent(a.join(`,`))}&lang=${t}`,{signal:o}).then(e=>e.json()).then(e=>r(e,`Get counter`).data),a=({serverURL:e,lang:i,path:a,type:o,action:s})=>fetch(`${n(e)}article?lang=${i}`,{method:`POST`,headers:t,body:JSON.stringify({path:a,type:o,action:s})}).then(e=>e.json()).then(e=>r(e,`Update counter`).data),o=({serverURL:e,lang:t,paths:n,signal:r})=>i({serverURL:e,lang:t,paths:n,type:[`time`],signal:r}),s=e=>a({...e,type:`time`,action:`inc`}),c=(e=``)=>e.replace(/\/$/u,``),l=e=>/^(https?:)?\/\//u.test(e),u=e=>{let t=c(e);return l(t)?t:`https://${t}`},d=e=>{e.name!==`AbortError`&&console.error(e.message)},f=e=>{let{path:t}=e.dataset;return t?.length?t:null},p=(e,t)=>{t.forEach((t,n)=>{let r=e[n].time;typeof r==`number`&&(t.textContent=r.toString())})},m=({serverURL:e,path:t=window.location.pathname,selector:n=`.waline-pageview-count`,update:r=!0,lang:i=navigator.language})=>{let a=new AbortController,c=[...document.querySelectorAll(n)],l=e=>{let n=f(e);return n!=null&&t!==n},m=n=>o({serverURL:u(e),paths:n.map(e=>f(e)??t),lang:i,signal:a.signal}).then(e=>{p(e,n)}).catch(d);if(r){let n=c.filter(e=>!l(e)),r=c.filter(e=>l(e));s({serverURL:u(e),path:t,lang:i}).then(e=>{p(e,n)}),r.length>0&&m(r)}else m(c);return a.abort.bind(a)};export{m as pageviewCount,e as version};
//# sourceMappingURL=pageview.js.map