tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

file_blob_upload_frame.html (2160B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <!DOCTYPE HTML>
      6 <html>
      7 <head>
      8  <title>test file blob upload with SW interception</title>
      9 </head>
     10 <body>
     11 <p id="display"></p>
     12 <div id="content" style="display: none"></div>
     13 <pre id="test"></pre>
     14 <script class="testbody" type="text/javascript">
     15 
     16 if (!parent) {
     17  dump("sw_clients/file_blob_upload_frame.html shouldn't be launched directly!");
     18 }
     19 
     20 function makeFileBlob(obj) {
     21  return new Promise(function(resolve, reject) {
     22 
     23    var request = indexedDB.open(window.location.pathname, 1);
     24    request.onerror = reject;
     25    request.onupgradeneeded = function(evt) {
     26      var db = evt.target.result;
     27      db.onerror = reject;
     28 
     29      var objectStore = db.createObjectStore('test', { autoIncrement: true });
     30      var index = objectStore.createIndex('test', 'index');
     31    };
     32 
     33    request.onsuccess = function(evt) {
     34      var db = evt.target.result;
     35      db.onerror = reject;
     36 
     37      var blob = new Blob([JSON.stringify(obj)],
     38                          { type: 'application/json' });
     39      var data = { blob, index: 5 };
     40 
     41      objectStore = db.transaction('test', 'readwrite').objectStore('test');
     42      objectStore.add(data).onsuccess = function(evt1) {
     43        var key = evt1.target.result;
     44        objectStore = db.transaction('test').objectStore('test');
     45        objectStore.get(key).onsuccess = function(evt2) {
     46          resolve(evt2.target.result.blob);
     47        };
     48      };
     49    };
     50  });
     51 }
     52 
     53 navigator.serviceWorker.ready.then(function() {
     54  parent.postMessage({ status: 'READY' }, '*');
     55 });
     56 
     57 var URL = '/tests/dom/serviceworkers/test/redirect_post.sjs';
     58 
     59 addEventListener('message', function(evt) {
     60  if (evt.data.type == 'TEST') {
     61    makeFileBlob(evt.data.body).then(function(blob) {
     62      return fetch(URL, { method: 'POST', body: blob });
     63    }).then(function(response) {
     64      return response.json();
     65    }).then(function(result) {
     66      parent.postMessage({ status: 'OK', result }, '*');
     67    }).catch(function(e) {
     68      parent.postMessage({ status: 'ERROR', result: e.toString() }, '*');
     69    });
     70  }
     71 });
     72 
     73 </script>
     74 </pre>
     75 </body>
     76 </html>