// ==UserScript== // @name BlakhurdJsTest // @namespace blakhurd.people.amazon.dev // @version 0.1.0 // @author blakhurd@ // @description PoC for JS development issues // @license ISC // @downloadURL https://code.amazon.com/packages/PowerChat/releases/1.0/latest_artifact?version_set=live&path=jstest.user.js&download=true // @updateURL https://code.amazon.com/packages/PowerChat/releases/1.0/latest_artifact?version_set=live&path=jstest.meta.js&download=true // @match https://github.com/* // @connect www.amazon.com // ==/UserScript== (function () { 'use strict'; var R=(()=>typeof GM<"u"?GM:void 0)(),y;const g=(y=R.xmlHttpRequest)==null?void 0:y.RESPONSE_TYPE_STREAM;console.log("Streaming supported: ",g);function M(r){const o=new Headers;return r.replace(/\r?\n[\t ]+/g," ").split(/\r?\n/).forEach(i=>{let u;const c=i.split(":"),s=(u=c.shift())==null?void 0:u.trim();if(s){const t=c.join(":").trim();try{o.append(s,t);}catch(d){console.warn(`Response ${d.message}`);}}}),o}async function P(r,o){const e=new Request(r,o);if(e.signal.aborted)throw new DOMException("Network request aborted.","AbortError");const i=await e.arrayBuffer(),u=i.byteLength?new TextDecoder().decode(i):void 0,c=Object.fromEntries(e.headers);return new Headers(o==null?void 0:o.headers).forEach((s,t)=>{c[t]=s;}),new Promise((s,t)=>{let d=!1,m;m=new Promise(n=>{R.xmlHttpRequest({method:e.method.toUpperCase(),url:e.url||location.href,headers:c,data:u,redirect:e.redirect,nocache:e.cache==="no-store",revalidate:e.cache==="reload",timeout:3e5,responseType:g?"stream":"blob",overrideMimeType:e.headers.get("Content-Type")??void 0,anonymous:e.credentials==="omit",onload:({response:a})=>{if(d){n(null);return}n(a);},async onreadystatechange({readyState:a,responseHeaders:p,status:H,statusText:q,finalUrl:f,response:w}){if(a!==XMLHttpRequest.DONE&&a!==XMLHttpRequest.HEADERS_RECEIVED)return;if(d){n(null);return}const l=M(p),E=e.url!==f,h=new Response(w instanceof ReadableStream?w:await m,{headers:l,status:H,statusText:q});Object.defineProperties(h,{url:{value:f},type:{value:"basic"},...h.redirected!==E?{redirected:{value:E}}:{},...l.has("set-cookie")||l.has("set-cookie2")?{headers:{value:l}}:{}}),s(h),d=!0;},onerror:({statusText:a,error:p})=>{t(new TypeError(a||p||"Network request failed.")),n(null);},ontimeout(){t(new TypeError("Network request timeout.")),n(null);},onabort(){t(new DOMException("Network request aborted.","AbortError")),n(null);}});});})}P("https://www.amazon.com").then(r=>r.text()).then(r=>{console.log("Successfully made a call using gmFetch");}); })();