title: How to control the XMLHttpRequest object on an HTML5 Web Worker?
body: <p>I have a page which will normally overrides window.XMLHttpRequest with a wrapper that does a few extra things like inserting in headers on certain requests.</p>
  
  <p>I have some functionality in a 3rd party library that uses HTML5 Worker, and we are seeing that this request does not use the XMLHttpRequest wrapper object. So any request that this library makes is missing the required headers, and so the request will fail.</p>
  
  <p>Is there a way to control the XMLHttpRequest that any Worker the current thread creates?</p>
  
  <p>This 3rd party library code looks like this:</p>
  
  <pre><code>        function createWorker(url) {
              var worker = new Worker(url);
              worker.onmessage = function (e) {
                  if (e.data.status) {
                      onprogress(e.data.status);
                  } else if (e.data.error) {
                      onerror(e.data.error);
                  } else {
                      exportUtils.saveFile(new Blob([e.data]), params.fileName);
                      onfinish();
                  }
              };
              worker.postMessage(params); // window.location.origin +
              return worker;
          }
  </code></pre>
  
  <p>The Javascript that is returned by the URL variable above contains code like this:</p>
  
  <pre><code>        return new Promise(function(t, r) {
              var n = new XMLHttpRequest
                , a = "batch_" + o()
                , u = e.dataUrl.split(e.serviceUrl)[1]
                , c = [];
              n.onload = function() {
                  for (var e = this.responseText, n = this.responseText.split("\r\n"), o = 0, a = n.length, i = a - 1; o &lt; a &amp;&amp; "{" !== n[o].slice(0, 1); )
                      o++;
                  for (; i &gt; 0 &amp;&amp; "}" !== n[i].slice(-1); )
                      i--;
                  n = n.slice(o, i + 1),
                  e = n.join("\r\n");
                  try {
                      var u = JSON.parse(e);
                      t(u)
                  } catch (t) {
                      r(s + e)
                  }
              }
              ,
              n.onerror = function() {
                  r(i)
              }
              ,
              n.onabort = function() {
                  r(i)
              }
              ,
              n.open("POST", e.serviceUrl + "$batch", !0),
              n.setRequestHeader("Accept", "multipart/mixed"),
              n.setRequestHeader("Content-Type", "multipart/mixed;boundary=" + a);
              for (var p in e.headers)
                  "accept" != p.toLowerCase() &amp;&amp; n.setRequestHeader(p, e.headers[p]);
              c.push("--" + a),
              c.push("Content-Type: application/http"),
              c.push("Content-Transfer-Encoding: binary"),
              c.push(""),
              c.push("GET " + u + " HTTP/1.1");
              for (var p in e.headers)
                  c.push(p + ":" + e.headers[p]);
              c.push(""),
              c.push(""),
              c.push("--" + a + "--"),
              c.push(""),
              c = c.join("\r\n"),
              n.send(c)
          }
          )
  </code></pre>