jquery.postmessage-transport.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * jQuery postMessage Transport Plugin
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2011, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, require, window, document */
  12. ;(function (factory) {
  13. 'use strict';
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['jquery'], factory);
  17. } else if (typeof exports === 'object') {
  18. // Node/CommonJS:
  19. factory(require('jquery'));
  20. } else {
  21. // Browser globals:
  22. factory(window.jQuery);
  23. }
  24. }(function ($) {
  25. 'use strict';
  26. var counter = 0,
  27. names = [
  28. 'accepts',
  29. 'cache',
  30. 'contents',
  31. 'contentType',
  32. 'crossDomain',
  33. 'data',
  34. 'dataType',
  35. 'headers',
  36. 'ifModified',
  37. 'mimeType',
  38. 'password',
  39. 'processData',
  40. 'timeout',
  41. 'traditional',
  42. 'type',
  43. 'url',
  44. 'username'
  45. ],
  46. convert = function (p) {
  47. return p;
  48. };
  49. $.ajaxSetup({
  50. converters: {
  51. 'postmessage text': convert,
  52. 'postmessage json': convert,
  53. 'postmessage html': convert
  54. }
  55. });
  56. $.ajaxTransport('postmessage', function (options) {
  57. if (options.postMessage && window.postMessage) {
  58. var iframe,
  59. loc = $('<a>').prop('href', options.postMessage)[0],
  60. target = loc.protocol + '//' + loc.host,
  61. xhrUpload = options.xhr().upload;
  62. // IE always includes the port for the host property of a link
  63. // element, but not in the location.host or origin property for the
  64. // default http port 80 and https port 443, so we strip it:
  65. if (/^(http:\/\/.+:80)|(https:\/\/.+:443)$/.test(target)) {
  66. target = target.replace(/:(80|443)$/, '');
  67. }
  68. return {
  69. send: function (_, completeCallback) {
  70. counter += 1;
  71. var message = {
  72. id: 'postmessage-transport-' + counter
  73. },
  74. eventName = 'message.' + message.id;
  75. iframe = $(
  76. '<iframe style="display:none;" src="' +
  77. options.postMessage + '" name="' +
  78. message.id + '"></iframe>'
  79. ).bind('load', function () {
  80. $.each(names, function (i, name) {
  81. message[name] = options[name];
  82. });
  83. message.dataType = message.dataType.replace('postmessage ', '');
  84. $(window).bind(eventName, function (e) {
  85. e = e.originalEvent;
  86. var data = e.data,
  87. ev;
  88. if (e.origin === target && data.id === message.id) {
  89. if (data.type === 'progress') {
  90. ev = document.createEvent('Event');
  91. ev.initEvent(data.type, false, true);
  92. $.extend(ev, data);
  93. xhrUpload.dispatchEvent(ev);
  94. } else {
  95. completeCallback(
  96. data.status,
  97. data.statusText,
  98. {postmessage: data.result},
  99. data.headers
  100. );
  101. iframe.remove();
  102. $(window).unbind(eventName);
  103. }
  104. }
  105. });
  106. iframe[0].contentWindow.postMessage(
  107. message,
  108. target
  109. );
  110. }).appendTo(document.body);
  111. },
  112. abort: function () {
  113. if (iframe) {
  114. iframe.remove();
  115. }
  116. }
  117. };
  118. }
  119. });
  120. }));