aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/blueimp/jquery-file-upload/cors/postmessage.html
blob: 536e8b3c6c9bbb1f3fa2b694068a9454dea4688b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<!--
/*
 * jQuery File Upload Plugin postMessage API
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2011, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * https://opensource.org/licenses/MIT
 */
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>jQuery File Upload Plugin postMessage API</title>
    <script
      src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"
      integrity="sha384-xBuQ/xzmlsLoJpyjoggmTEz8OWUFM0/RC5BsqQBDX2v5cMvDHcMakNTNrHIW2I5f"
      crossorigin="anonymous"
    ></script>
  </head>
  <body>
    <script>
      'use strict';
      var origin = /^https:\/\/example.org/,
        target = new RegExp('^(http(s)?:)?\\/\\/' + location.host + '\\/');
      $(window).on('message', function (e) {
        e = e.originalEvent;
        var s = e.data,
          xhr = $.ajaxSettings.xhr(),
          f;
        if (!origin.test(e.origin)) {
          throw new Error('Origin "' + e.origin + '" does not match ' + origin);
        }
        if (!target.test(e.data.url)) {
          throw new Error(
            'Target "' + e.data.url + '" does not match ' + target
          );
        }
        $(xhr.upload).on('progress', function (ev) {
          ev = ev.originalEvent;
          e.source.postMessage(
            {
              id: s.id,
              type: ev.type,
              timeStamp: ev.timeStamp,
              lengthComputable: ev.lengthComputable,
              loaded: ev.loaded,
              total: ev.total
            },
            e.origin
          );
        });
        s.xhr = function () {
          return xhr;
        };
        if (!(s.data instanceof Blob)) {
          f = new FormData();
          $.each(s.data, function (i, v) {
            f.append(v.name, v.value);
          });
          s.data = f;
        }
        $.ajax(s).always(function (result, statusText, jqXHR) {
          if (!jqXHR.done) {
            jqXHR = result;
            result = null;
          }
          e.source.postMessage(
            {
              id: s.id,
              status: jqXHR.status,
              statusText: statusText,
              result: result,
              headers: jqXHR.getAllResponseHeaders()
            },
            e.origin
          );
        });
      });
    </script>
  </body>
</html>