geekssl /

wstats

公开
main
wstats/public/sdk/wstat.js
wstat.js9 KB
(function(a,s){"use strict";var C="1.5",D="wstat_uid",x="wstat_sid",M="wstat_user",P="wstat_ui",H="wstat_attr",A="wstat_ref",p=18e5,w=730*24*3600,q={gclid:"Google Ads",msclkid:"Microsoft Ads",fbclid:"Meta Ads",ttclid:"TikTok Ads",ytclid:"YouTube Ads",sccid:"Snapchat Ads",dclid:"DoubleClick",wbraid:"Google Ads",gbraid:"Google Ads",qqclid:"Tencent Ads",lgclickid:"LG U+",ogclid:"Omnicom"},c={host:"",siteKey:"",heartbeat:25,spaHash:!1,heatmap:!0,auto:!0},j="",g=null,y="",S=null,u={ttfb:0,fcp:0,lcp:0,cls:0,inp:0},I=!1,z=!1,_=null,b=null;function B(){for(var t=s.getElementsByTagName("script"),e={},r=t.length-1;r>=0;r--){var n=t[r].src||"";if(n.indexOf("wstat.js")!==-1){e.siteKey=t[r].getAttribute("data-site-key")||"",e.host=t[r].getAttribute("data-host")||"";break}}return e}function T(t){var e=s.cookie.match(new RegExp("(?:^|; )"+t+"=([^;]*)"));return e?decodeURIComponent(e[1]):""}function d(t,e,r){var n=[t+"="+encodeURIComponent(e),"path=/","SameSite=Lax"];r&&n.push("max-age="+r),a.location.protocol==="https:"&&n.push("Secure"),s.cookie=n.join("; ")}function N(t){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",r=[],n;try{n=a.crypto.getRandomValues(new Uint8Array(t))}catch(o){n=null}for(var i=0;i<t;i++)r.push(n?e[n[i]%e.length]:e[Math.floor(Math.random()*e.length)]);return r.join("")}function k(t){var e=new RegExp("[?&]"+t+"=([^&#]*)","i"),r=a.location.search.match(e);return r?decodeURIComponent(r[1].replace(/\+/g," ")):""}function E(t){return t=String(t||""),/^https?:\/\//i.test(t)?t.slice(0,600):""}function Q(t){var e=k(t);if(e)return e;var r=(a.location.hash||"").match(new RegExp("[?&]"+t+"=([^&#]*)","i"));return r?decodeURIComponent(r[1].replace(/\+/g," ")):""}function tt(){if(S!==null)return S;var t=E(y);return t||(t=E(Q(A))),t||(t=String(s.referrer||"").slice(0,600)),S=t,t}function Y(){var t=a.location.search||"";return t.indexOf(A)!==-1&&(t=t.replace(new RegExp("[?&]"+A+"=[^&]*","gi"),""),t!==""&&(t="?"+t.replace(/^[?&]+/,""))),a.location.pathname+t}function et(){try{var t=a.performance&&a.performance.getEntriesByType?a.performance.getEntriesByType("navigation")[0]:null;if(t&&typeof t.redirectCount=="number")return t.redirectCount}catch(e){}try{if(a.performance&&a.performance.navigation)return a.performance.navigation.redirectCount||0}catch(e){}return 0}function W(t){return y=E(t),S=null,y!==""}function rt(t){try{return a.sessionStorage.getItem(t)}catch(e){return null}}function at(t,e){try{a.sessionStorage.setItem(t,e)}catch(r){}}var F=/^[A-Za-z0-9_.\-@+:]{1,128}$/;function G(){if(g!==null)return g;var t="";try{t=a.localStorage.getItem(M)||""}catch(e){}return t||(t=T(P)),g=t&&F.test(t)?t:"",g}function J(t){var e=String(t==null?"":t).trim();if(!F.test(e))return!1;g=e;try{a.localStorage.setItem(M,e)}catch(r){}return d(P,e,w),!0}function nt(){var t=rt(H);if(t)try{return JSON.parse(t)}catch(m){}for(var e={source:"",medium:"",campaign:"",content:"",term:"",click:"",click_source:""},r=["source","medium","campaign","content","term"],n=0;n<r.length;n++)e[r[n]]=k("utm_"+r[n]);for(var i=Object.keys(q),o=0;o<i.length;o++){var f=k(i[o]);if(f){e.click=f,e.click_source=q[i[o]];break}}return(e.source||e.medium||e.campaign||e.click)&&at(H,JSON.stringify(e)),e}function it(t){return{utm_source:t.source,utm_medium:t.medium,utm_campaign:t.campaign,utm_content:t.content,utm_term:t.term}}function V(){var t=T(D);return(!t||t.length<8)&&(t="U"+N(24),d(D,t,w)),t}function L(){var t=T(x),e=!t||t.length<8,r=T("wstat_sid_exp");return t&&r&&(+r||0)<Date.now()&&(e=!0),e&&(t="S"+N(24),d(x,t,p/1e3),d("wstat_sid_exp",String(Date.now()+p),p/1e3)),t}function R(){d(x,L(),p/1e3),d("wstat_sid_exp",String(Date.now()+p),p/1e3)}function l(t,e){var r=nt(),n=it(r),i={v:1,ak:c.siteKey,t,uid:V(),sid:L(),tz:-new Date().getTimezoneOffset(),url:Y(),title:s.title?s.title.slice(0,255):"",ref:tt(),lang:(a.navigator.language||"").slice(0,16),scr:a.screen&&a.screen.width?a.screen.width+"x"+a.screen.height:"",utm_source:n.utm_source,utm_medium:n.utm_medium,utm_campaign:n.utm_campaign,utm_content:n.utm_content,utm_term:n.utm_term,click_id:r.click||"",click_name:r.click_source||""};return G()&&(i.ui=G()),e&&typeof e=="object"&&(i.d=e),i}function ct(){var t=c.host.replace(/\/+$/,"");return t+"/collect.php"}function v(t){if(c.siteKey){var e=ct(),r;try{r=JSON.stringify(t)}catch(o){return}try{var n=new Blob([r],{type:"text/plain;charset=UTF-8"});if(a.navigator.sendBeacon&&a.navigator.sendBeacon(e,n)){R();return}}catch(o){}try{if(a.fetch){a.fetch(e,{method:"POST",keepalive:!0,credentials:"omit",headers:{"Content-Type":"text/plain;charset=UTF-8"},body:r}).then(R).catch(function(){});return}}catch(o){}try{var i=new XMLHttpRequest;i.open("POST",e,!0),i.setRequestHeader("Content-Type","text/plain;charset=UTF-8"),i.onloadend=R,i.send(r)}catch(o){}}}function h(){var t=Y();if(t!==j){j=t,R();var e=et();v(l("pageview",e>0?{rd:e}:null)),c.auto&&ft(),ht()}}function $(t,e){var r={n:String(t).slice(0,64)};if(e&&typeof e=="object")for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&typeof e[n]!="object"&&(r[n]=String(e[n]).slice(0,255));v(l("event",r))}function X(t){if(!I){I=!0,clearTimeout(b);var e={};for(var r in u)Object.prototype.hasOwnProperty.call(u,r)&&u[r]&&(e[r]=r==="cls"?Math.round(u[r]*1e4)/1e4:Math.round(u[r]));for(var n in t||{})e[n]=t[n];v(l("perf",e))}}var Z=0;function ot(t){if(c.heatmap){var e=a.Date&&Date.now?Date.now():+new Date;if(!(e-Z<300)){Z=e;var r=t.target;if(!(!r||r.nodeType!==1)&&!(r.closest&&r.closest(".wstat-ignore"))){var n=s.documentElement,i=a.innerWidth||n.clientWidth||0,o=Math.max(n.scrollHeight,s.body?s.body.scrollHeight:0)||0;if(!(i<=0||o<=0)){var f=typeof t.pageY=="number"?t.pageY:t.clientY+(a.pageYOffset||0);f>o&&(f=o);var m=(r.tagName||"").toLowerCase(),St=(r.innerText||r.textContent||"").replace(/\s+/g," ").slice(0,64);v(l("click",{x:Math.round(t.clientX/i*1e4)/100,y:Math.round(f/o*1e4)/100,vw:i,vh:a.innerHeight||0,dh:o,tag:m.slice(0,16),txt:St}))}}}}}function st(){s.addEventListener("click",ot,!0)}var U=["q","s","search","keyword","wd","query","k","word","searchword"],ut=/\.(zip|rar|7z|tar|gz|tgz|pdf|docx?|xlsx?|pptx?|csv|exe|dmg|apk|mp3|mp4|avi|mov|wmv|flv|epub)(\?|#|$)/i;function ft(){for(var t=0;t<U.length;t++){var e=k(U[t]);if(e){v(l("search",{term:e.slice(0,100),param:U[t]}));return}}}function lt(t){if(c.siteKey){var e=t.target;if(!(!e||e.nodeType!==1)){var r=e.closest?e.closest("a[href]"):null;if(r&&!(r.closest&&r.closest(".wstat-ignore"))){var n=r.getAttribute("href")||"";if(!(!n||n.charAt(0)==="#"||/^(javascript|mailto|tel|sms):/i.test(n))){var i;try{i=new URL(n,a.location.href)}catch(f){return}if(!(i.protocol!=="http:"&&i.protocol!=="https:")){var o=(r.innerText||r.textContent||"").replace(/\s+/g," ").slice(0,100);i.host!==a.location.host?v(l("outlink",{to:i.href.slice(0,500),txt:o})):ut.test(i.pathname+i.search)&&v(l("download",{file:(i.pathname+i.search).slice(0,500),txt:o}))}}}}}}function vt(){s.addEventListener("click",lt,!0)}function ht(){b&&clearTimeout(b),b=setTimeout(function(){I||X()},15e3)}function pt(){function t(r,n){if(!a.PerformanceObserver)return null;try{var i=new a.PerformanceObserver(function(o){for(var f=o.getEntries(),m=0;m<f.length;m++)n(f[m])});return i.observe({type:r,buffered:!0}),i}catch(o){return null}}var e=null;try{e=a.performance.getEntriesByType("navigation")[0]}catch(r){}e&&e.responseStart>0&&(u.ttfb=e.responseStart),t("paint",function(r){r.name==="first-contentful-paint"&&(u.fcp=r.startTime)}),t("largest-contentful-paint",function(r){r.startTime>(u.lcp||0)&&(u.lcp=r.startTime)}),t("layout-shift",function(r){r.hadRecentInput||(u.cls+=r.value)}),t("event",function(r){r.duration>(u.inp||0)&&r.interactionId&&(u.inp=r.duration)})}function dt(){s.visibilityState==="hidden"||!c.siteKey||v(l("hb"))}function mt(){_&&(clearInterval(_),_=null)}function gt(){mt(),X()}function yt(){if(a.history){var t=function(e){var r=a.history[e];return function(){var n=r.apply(this,arguments);return setTimeout(h,0),n}};try{a.history.pushState=t("pushState"),a.history.replaceState=t("replaceState")}catch(e){}a.addEventListener("popstate",function(){setTimeout(h,0)}),c.spaHash&&a.addEventListener("hashchange",function(){setTimeout(h,0)})}}function O(t){if(!z){var e=B();if(t=t||{},c.host=(t.host||e.host||"").replace(/\/+$/,""),c.siteKey=t.siteKey||e.siteKey||"",c.heartbeat=t.heartbeat>5?t.heartbeat:c.heartbeat,c.spaHash=!!t.spaHash,t.heatmap===!1&&(c.heatmap=!1),t.auto===!1&&(c.auto=!1),y=E(t.referrer||a.WSTAT_REF||""),!c.siteKey){a.console&&console.warn&&console.warn("[wstat] \u7F3A\u5C11 siteKey\uFF0C\u91C7\u96C6\u672A\u542F\u52A8");return}z=!0,V(),L(),pt(),yt(),c.heatmap&&st(),c.auto&&vt(),s.readyState==="loading"?s.addEventListener("DOMContentLoaded",function(){setTimeout(h,0)}):setTimeout(h,0),_=setInterval(dt,c.heartbeat*1e3),a.addEventListener("pagehide",gt),a.console&&console.info&&console.info("[wstat] v"+C+" ok site="+c.siteKey),a.WStat={version:C,init:O,page:h,track:$,identify:J,setRef:W,config:c}}}var K=B();K.siteKey&&(s.readyState==="loading"?s.addEventListener("DOMContentLoaded",function(){O(K)}):O(K)),a.WStat=a.WStat||{version:C,init:O,page:h,track:$,identify:J,setRef:W,config:c}})(window,document);