Loading
function(e,t,n){"use strict";n.d(t,{qM:()=>M,fs:()=>U,wn:()=>v,WI:()=>w}),n(393431),n(532706),n(42231),n(232424),n(949626),n(767709),n(65162);var i=n(141931),r=n(611599),a=n(429582),s=n(475735),l=n(754712),o=n(420928),d=n(192888),c=n(965528);n(321073);var u=n(508382),_=n(365912),E=n(192308),A=n(186111),f=n(539197);function h(e,t){let n=e.getBoundingClientRect();return 2>=Math.abs(n.left-t.left)&&2>=Math.abs(n.top-t.top)&&4>=Math.abs(n.width-t.width)&&4>=Math.abs(n.height-t.height)}function I(e,t){if((0,E.ue)()||A.A.hasLayers()||function(e,t){let n=[...document.getElementsByClassName(_.Pb),...document.querySelectorAll(`[${u.IP}]`)],i=[],r=0;for(let s of n)for(let n of s.querySelectorAll("*")){if(++r>500)return!0;if(!(n.contains(e)||null!=n.closest(`[${f.t$}]`)||i.some(e=>e.contains(n)))){var a;if(h(n,t)){i.push(n);continue}if(a=n.getBoundingClientRect(),a.width>0&&a.height>0&&a.left<t.right&&a.right>t.left&&a.top<t.bottom&&a.bottom>t.top)return!0}}return!1}(e,t))return!0;let n=Math.min(4,t.width/4),i=Math.min(4,t.height/4);for(let r=0;r<7;r++)for(let a=0;a<7;a++){let s=t.left+n+(t.width-2*n)*r/6,l=t.top+i+(t.height-2*i)*a/6,o=document.elementsFromPoint(s,l),d=o.findIndex(t=>t.contains(e));if(-1===d||!o.slice(0,d).every(e=>h(e,t)))return!0}return!1}var p=n(731854);let m=new r.Vy("ConjureNativeCapture");function T(e,t){return m.verbose(`native capture not used: ${e}`,t??{}),null}function g(){return window.DiscordNative?.activityCapture??null}function S(e){let t=e.getBoundingClientRect();return t.width<40||t.height<40||t.left<-1||t.top<-1||t.right>window.innerWidth+1||t.bottom>window.innerHeight+1?null:t}function N(e){return e.resolveUploadUrl().catch(()=>null)}function C(e,t,n,i){return(0,o.e)(e,n,{captureId:t.captureId,meta:i,build:t.build,uploadToken:t.uploadToken})}function O(e,t,n){return(0,d.W)(e,"capture-prepare",null==n?{}:{spec:n},{id:t,timeoutMs:1e3})}function R(e,t){(0,d.W)(e,"capture-release",{prepareId:t},{timeoutMs:1e3}).catch(()=>{})}function L(e){let{bounds:t,scale:n}=e;return{width:Math.max(1,Math.round(t.width*n)),height:Math.max(1,Math.round(t.height*n))}}function y(e,t,n){let i=g();return null==i?Promise.reject(Error("activityCapture unavailable")):i.captureImage({frameId:e,rect:t.bounds,viewportSize:t.viewport,outputSize:L(t),format:n?.format??"webp",quality:n?.quality??92,timeoutMs:1500})}async function D(e,t,n){let i=performance.now(),r=i,a={};function s(e){let t=performance.now();a[`${e}Ms`]=Math.round(t-r),r=t}function l(e,t){return T(e,{...t,...a,totalMs:Math.round(performance.now()-i)})}try{let o,d,c=e.getBoundingClientRect();if(c.width<40||c.height<40)return l("frame too small");let u=N(t);r=performance.now();let _=`capture-prepare-${t.captureId}`;try{o=await O(e,_,t.spec)}catch(t){return s("prepare"),R(e,_),l("frame did not prepare",{err:t})}if(s("prepare"),"ready"!==o.phase)return l("frame refused",{code:o.code,error:o.error});let E=o;try{d=await n(E)}catch(e){return s("image"),l("host capture failed",{err:e})}finally{R(e,_)}if(s("image"),d.data.byteLength>5242880)return l("encoded capture too large",{bytes:d.data.byteLength});let A=await u;if(null==A)return l("no upload url");let f=new Blob([d.data],{type:d.mimeType}),h=await C(A,t,f,function(e){try{let t=JSON.parse(decodeURIComponent(e));if(null==t||"object"!=typeof t)return e;return encodeURIComponent(JSON.stringify({...t,source:"native"}))}catch{return e}}(E.meta));if(s("upload"),!h.ok)return l("upload refused",{status:h.status});let I=Math.round(performance.now()-i);return m.verbose(`native capture uploaded in ${I}ms`,{id:t.captureId,bytes:f.size,width:d.width,height:d.height,...a,totalMs:I}),{status:"accepted"}}catch(e){return l("threw",{err:e})}}let b=0;async function v(e){if(null==g())return T("frame surface capture unavailable");let t=(0,c.B0)(e);if(null==t)return T("frame identity unknown");let n=`capture-prepare-sketch-${++b}`;try{let i=await O(e,n,void 0);if("ready"!==i.phase)return T("frame refused",{code:i.code,error:i.error});let r=await y(t,i,void 0);return new Blob([r.data],{type:r.mimeType})}catch(e){return T("frame surface capture failed",{err:e})}finally{R(e,n)}}async function P(e,t){let n,{skipWindowPreview:r=!1,onStill:a}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(null==window.DiscordNative)return T("not the desktop app");if("visible"!==document.visibilityState)return T("window not visible");let o=S(e);if(null==o)return T("frame not fully on screen");if(I(e,o))return T("frame is covered by Discord UI");let d={x:Math.max(0,window.outerWidth-window.innerWidth),y:Math.max(0,window.outerHeight-window.innerHeight)},c=d.x>2||d.y>2?await async function(){let e=document.createElement("div");return e.style.cssText="position:fixed;left:40px;top:40px;width:6px;height:6px;background:rgb(255,0,255);z-index:2147483647;pointer-events:none",document.body.appendChild(e),await new Promise(e=>requestAnimationFrame(()=>requestAnimationFrame(()=>e()))),()=>e.remove()}():null;try{n=await async function(e,t){let n=window.DiscordNative,r=await n?.window?.getMediaSourceId?.();if(null==r)return T("no media source id for our own window");let a=r.split(":")[1];if(null==a||""===a)return T("unrecognized media source id",{sourceId:r});let o=Math.min(window.devicePixelRatio,1,1568/Math.max(e.width,e.height),Math.sqrt(115e4/(e.width*e.height))),d=Math.ceil(window.outerWidth*o),c=Math.ceil(window.outerHeight*o),u=l.Ay.getMediaEngine();if(!t&&u.supports(p.O5.WINDOW_PREVIEWS))try{let e=s.O.getConfig({location:"conjureNativeCapture"}).enabled,t=await u.getSingleWindowPreview(a,d,c,e);if(null!=t&&""!==t.url)return t.url}catch{}let _=await n?.desktopCapture?.getDesktopCaptureSources({types:[i.fS.WINDOW],thumbnailSize:{width:d,height:c}}),E=_?.find(e=>e.id.split(":")[1]===a);return null==E||""===E.url?T("own window missing from capture sources"):E.url}(o,r)}finally{c?.(),a?.()}if(null==n)return null;let u=S(e);if(null==u||Math.abs(u.left-o.left)>1||Math.abs(u.top-o.top)>1||Math.abs(u.width-o.width)>1||Math.abs(u.height-o.height)>1)return T("frame moved or resized during capture");if(I(e,u))return T("Discord UI covered the frame during capture");let _=await async function(e,t,n,i){let r=new Image;if(r.decoding="async",r.src=e,await r.decode(),0===r.naturalWidth||0===r.naturalHeight)return T("window still decoded empty");let a=r.naturalWidth/window.outerWidth,s=r.naturalHeight/window.outerHeight;if(a<=0||s<=0||Math.abs(a-s)>.03*a)return T("window still does not match the window geometry",{image:{width:r.naturalWidth,height:r.naturalHeight},window:{width:window.outerWidth,height:window.outerHeight}});let l=function(e,t,n,i){let r=t.x>2?[0,t.x]:[0],a=t.y>2?[0,t.y]:[0];if(1===r.length&&1===a.length)return{x:0,y:0};for(let t of r)for(let r of a)if(function(e,t,n){let i=document.createElement("canvas");i.width=1,i.height=1;let r=i.getContext("2d");if(null==r)return!1;r.drawImage(e,t,n,1,1,0,0,1,1);let[a,s,l]=r.getImageData(0,0,1,1).data;return a>150&&l>150&&s<Math.min(a,l)-80}(e,Math.round((t+43)*n),Math.round((r+43)*i)))return{x:t,y:r};return null}(r,n,a,s);if(null==l)return T("document not found inside the window still",{inset:n});let o=Math.max(0,Math.floor((l.x+t.left)*a)),d=Math.max(0,Math.floor((l.y+t.top)*s)),c=Math.min(r.naturalWidth-o,Math.round(t.width*a)),u=Math.min(r.naturalHeight-d,Math.round(t.height*s));if(c<1||u<1)return T("crop resolved empty");let _=Math.min(1,1568/Math.max(c,u),Math.sqrt(115e4/(c*u))),E=Math.max(1,Math.round(c*_)),A=Math.max(1,Math.round(u*_)),f=document.createElement("canvas");f.width=E,f.height=A;let h=f.getContext("2d");if(null==h)return T("no 2d context");h.drawImage(r,o,d,c,u,0,0,E,A);let I=`image/${i?.format??"webp"}`,p=Math.min(1,Math.max(0,(i?.quality??92)/100)),m=await new Promise(e=>f.toBlob(e,I,p));return null==m||m.type!==I?T("encode failed",{type:I}):m.size>5242880?T("encoded capture too large",{bytes:m.size}):{blob:m,scale:(E/t.width+A/t.height)/2}}(n,o,d,t);return null==_?null:{..._,rect:o}}function M(){let e=window.DiscordNative;return null!=e&&"function"==typeof e.window?.getMediaSourceId&&("function"==typeof e.desktopCapture?.getDesktopCaptureSources||l.Ay.getMediaEngine().supports(p.O5.WINDOW_PREVIEWS))}async function U(e,t){try{let n=await P(e,void 0,{skipWindowPreview:!0,onStill:t});return n?.blob??null}catch(e){return T("threw",{err:e})}}async function w(e,t){let n=(0,c.B0)(e);if(null==n&&T("frame identity unknown"),null!=g()&&null!=n){let i=await D(e,t,e=>y(n,e,t.spec));if(null!=i)return i}if((0,a.t8)(e)){let n=await D(e,t,n=>(async function(e,t,n){let i=n?.format??"webp",r=n?.quality??92,s=(0,a.oe)(e,{rect:t.bounds,outputSize:L(t),format:"jpeg"===i?"jpeg":"png",quality:r});if(null==s)throw Error("frame is not hosted in a view");let l=await new Promise((e,t)=>{let n=setTimeout(()=>t(Error("timed out after 1500ms")),1500);s.then(e,t).finally(()=>clearTimeout(n))});if("webp"!==i)return l;let o=await createImageBitmap(new Blob([l.data],{type:l.mimeType}));try{let e=document.createElement("canvas");e.width=o.width,e.height=o.height;let t=e.getContext("2d");if(null==t)throw Error("no 2d context");t.drawImage(o,0,0);let n=await new Promise(t=>e.toBlob(t,"image/webp",r/100));if(null==n||"image/webp"!==n.type)throw Error("webp encode failed");return{data:new Uint8Array(await n.arrayBuffer()),mimeType:n.type,width:o.width,height:o.height}}finally{o.close()}})(e,n,t.spec));if(null!=n)return n}return e.ownerDocument!==document?T("frame in another window"):async function(e,t){try{var n,i,r;if(null==window.DiscordNative)return T("not the desktop app");if(n=t.spec,null!=n&&"viewport"!==(n.mode??(null!=(i=n.target)&&(null!=i.ref&&""!==i.ref||null!=i.selector&&""!==i.selector)?"element":null!=(r=n.rect)&&r.width>0&&r.height>0?"rect":"viewport")))return T("targeted capture needs the frame DOM",{spec:t.spec});let a=N(t),s=await P(e,t.spec);if(null==s)return null;let{rect:l}=s,o=await a;if(null==o)return T("no upload url");let d={mode:"viewport",bounds:{x:0,y:0,width:Math.round(l.width),height:Math.round(l.height)},viewport:{width:Math.round(l.width),height:Math.round(l.height)},scale:Math.round(1e3*s.scale)/1e3,devicePixelRatio:window.devicePixelRatio,formFactor:l.width<=768?"narrow":"wide",...null==t.build?{}:{build:t.build},source:"native"},c=await C(o,t,s.blob,encodeURIComponent(JSON.stringify(d)));if(!c.ok)return T("upload refused",{status:c.status});return m.verbose("native capture uploaded",{id:t.captureId,bytes:s.blob.size,scale:d.scale}),{status:"accepted"}}catch(e){return T("threw",{err:e})}}(e,t)}}