UNPKG

@s4e/jsentinel

Version:

Script integrity and clipboard tamper detection for browsers.

2 lines (1 loc) 3.21 kB
import{_ as t}from"./index-hwEiSUPx.js";import{emit as e}from"./events-ffa6G8za.js";const n=()=>Math.random().toString(36).slice(2);function o(){return"undefined"!=typeof performance&&performance.now?performance.now():Date.now()}function r(t){try{const e=new URL(t,location.origin);return e.search=e.search?"?***":"",e.toString()}catch(e){return t}}function s(t){try{const e=performance.getEntriesByType("resource");for(let n=e.length-1;n>=0;n--)if(e[n].name===t)return{transferSize:e[n].transferSize||void 0}}catch(t){}return{}}function i(){!function(){const i=window.fetch;i&&(window.fetch=function(a,d){return t(this,void 0,void 0,function*(){const t="string"==typeof a?a:(null==a?void 0:a.url)||String(a),u=n(),c=((null==d?void 0:d.method)||(null==a?void 0:a.method)||"GET").toUpperCase(),l=o(),h={id:u,kind:"fetch",url:r(t),method:c,ts:Date.now()};e({type:"network.request",data:h});try{const n=yield i(a,d),c=o(),h={};try{n.headers.forEach((t,e)=>h[e.toLowerCase()]=t)}catch(t){}const p={id:u,kind:"fetch",url:r(t),status:"opaque"===n.type?"opaque":n.status,ok:"opaque"===n.type?null:n.ok,duration_ms:Math.round(c-l),headers:h,size:Object.assign({contentLength:Number(h["content-length"])||void 0},s(t)),ts:Date.now()};return e({type:"network.response",data:p}),n}catch(n){const s=o(),i={id:u,kind:"fetch",url:r(t),status:null,ok:null,duration_ms:Math.round(s-l),error:String((null==n?void 0:n.message)||n),ts:Date.now()};throw e({type:"network.response",data:i}),n}})})}(),function(){const t=window.XMLHttpRequest;if(!t)return;const i=t.prototype.open,a=t.prototype.send;t.prototype.open=function(t,e,...o){return this.__js_req={id:n(),method:(t||"GET").toUpperCase(),url:e,t0:0},i.call(this,t,e,...o)},t.prototype.send=function(t){const n=this.__js_req;if(n){n.t0=o();const t={id:n.id,kind:"xhr",url:r(n.url),method:n.method,ts:Date.now()};e({type:"network.request",data:t}),this.addEventListener("loadend",()=>{const t=o();let i;try{const t=this.getAllResponseHeaders()||"";i={},t.trim().split(/[\r\n]+/).forEach(t=>{const e=t.indexOf(":");e>-1&&(i[t.slice(0,e).toLowerCase()]=t.slice(e+1).trim())})}catch(t){}const a={id:n.id,kind:"xhr",url:r(n.url),status:this.status||null,ok:this.status?this.status>=200&&this.status<400:null,duration_ms:Math.round(t-n.t0),headers:i,size:Object.assign({contentLength:Number(null==i?void 0:i["content-length"])||void 0},s(n.url)),error:0===this.status&&4===this.readyState?"network-error":void 0,ts:Date.now()};e({type:"network.response",data:a})},{once:!0}),this.addEventListener("error",()=>{const t=o(),s={id:n.id,kind:"xhr",url:r(n.url),status:null,ok:null,duration_ms:Math.round(t-n.t0),error:"xhr-error",ts:Date.now()};e({type:"network.response",data:s})},{once:!0})}return a.call(this,t)}}(),function(){var t;const s=null===(t=navigator.sendBeacon)||void 0===t?void 0:t.bind(navigator);s&&(navigator.sendBeacon=(t,i)=>{const a=String(t),d=n(),u=o(),c={id:d,kind:"beacon",url:r(a),method:"POST",ts:Date.now()};e({type:"network.request",data:c});const l=s(t,i),h=o(),p={id:d,kind:"beacon",url:r(a),status:l?204:null,ok:!!l||null,duration_ms:Math.round(h-u),ts:Date.now()};return e({type:"network.response",data:p}),l})}()}export{i as installRequestGuard};