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