← All changes
|
libraries/freemius/assets/js/nojquery.ba-postmessage.js
+1
-113
2.2
→
3.4
View file →
| @@ -24,117 +24,5 @@ | ||
| 24 | 24 | * deserialization to the client. |
| 25 | 25 | * 5. The use of `$.isFunction` is replaced by a functionally-identical check. |
| 26 | 26 | * 6. The `$:nomunge` YUI option is no longer necessary. |
| 27 | 27 | */ |
| 28 | - | |
| 29 | -function NoJQueryPostMessageMixin(postBinding, receiveBinding) { | |
| 30 | - | |
| 31 | - var setMessageCallback, unsetMessageCallback, currentMsgCallback, | |
| 32 | - intervalId, lastHash, cacheBust = 1; | |
| 33 | - | |
| 34 | - if (window.postMessage) { | |
| 35 | - | |
| 36 | - if (window.addEventListener) { | |
| 37 | - setMessageCallback = function(callback) { | |
| 38 | - window.addEventListener('message', callback, false); | |
| 39 | - } | |
| 40 | - | |
| 41 | - unsetMessageCallback = function(callback) { | |
| 42 | - window.removeEventListener('message', callback, false); | |
| 43 | - } | |
| 44 | - } else { | |
| 45 | - setMessageCallback = function(callback) { | |
| 46 | - window.attachEvent('onmessage', callback); | |
| 47 | - } | |
| 48 | - | |
| 49 | - unsetMessageCallback = function(callback) { | |
| 50 | - window.detachEvent('onmessage', callback); | |
| 51 | - } | |
| 52 | - } | |
| 53 | - | |
| 54 | - this[postBinding] = function(message, targetUrl, target) { | |
| 55 | - if (!targetUrl) { | |
| 56 | - return; | |
| 57 | - } | |
| 58 | - | |
| 59 | - // The browser supports window.postMessage, so call it with a targetOrigin | |
| 60 | - // set appropriately, based on the targetUrl parameter. | |
| 61 | - target.postMessage( message, targetUrl.replace( /([^:]+:\/\/[^\/]+).*/, '$1' ) ); | |
| 62 | - } | |
| 63 | - | |
| 64 | - // Since the browser supports window.postMessage, the callback will be | |
| 65 | - // bound to the actual event associated with window.postMessage. | |
| 66 | - this[receiveBinding] = function(callback, sourceOrigin, delay) { | |
| 67 | - // Unbind an existing callback if it exists. | |
| 68 | - if (currentMsgCallback) { | |
| 69 | - unsetMessageCallback(currentMsgCallback); | |
| 70 | - currentMsgCallback = null; | |
| 71 | - } | |
| 72 | - | |
| 73 | - if (!callback) { | |
| 74 | - return false; | |
| 75 | - } | |
| 76 | - | |
| 77 | - // Bind the callback. A reference to the callback is stored for ease of | |
| 78 | - // unbinding. | |
| 79 | - currentMsgCallback = setMessageCallback(function(e) { | |
| 80 | - switch(Object.prototype.toString.call(sourceOrigin)) { | |
| 81 | - case '[object String]': | |
| 82 | - if (sourceOrigin !== e.origin) { | |
| 83 | - return false; | |
| 84 | - } | |
| 85 | - break; | |
| 86 | - case '[object Function]': | |
| 87 | - if (sourceOrigin(e.origin)) { | |
| 88 | - return false; | |
| 89 | - } | |
| 90 | - break; | |
| 91 | - } | |
| 92 | - | |
| 93 | - callback(e); | |
| 94 | - }); | |
| 95 | - }; | |
| 96 | - | |
| 97 | - } else { | |
| 98 | - | |
| 99 | - this[postBinding] = function(message, targetUrl, target) { | |
| 100 | - if (!targetUrl) { | |
| 101 | - return; | |
| 102 | - } | |
| 103 | - | |
| 104 | - // The browser does not support window.postMessage, so set the location | |
| 105 | - // of the target to targetUrl#message. A bit ugly, but it works! A cache | |
| 106 | - // bust parameter is added to ensure that repeat messages trigger the | |
| 107 | - // callback. | |
| 108 | - target.location = targetUrl.replace( /#.*$/, '' ) + '#' + (+new Date) + (cacheBust++) + '&' + message; | |
| 109 | - } | |
| 110 | - | |
| 111 | - // Since the browser sucks, a polling loop will be started, and the | |
| 112 | - // callback will be called whenever the location.hash changes. | |
| 113 | - this[receiveBinding] = function(callback, sourceOrigin, delay) { | |
| 114 | - if (intervalId) { | |
| 115 | - clearInterval(intervalId); | |
| 116 | - intervalId = null; | |
| 117 | - } | |
| 118 | - | |
| 119 | - if (callback) { | |
| 120 | - delay = typeof sourceOrigin === 'number' | |
| 121 | - ? sourceOrigin | |
| 122 | - : typeof delay === 'number' | |
| 123 | - ? delay | |
| 124 | - : 100; | |
| 125 | - | |
| 126 | - intervalId = setInterval(function(){ | |
| 127 | - var hash = document.location.hash, | |
| 128 | - re = /^#?\d+&/; | |
| 129 | - if ( hash !== lastHash && re.test( hash ) ) { | |
| 130 | - lastHash = hash; | |
| 131 | - callback({ data: hash.replace( re, '' ) }); | |
| 132 | - } | |
| 133 | - }, delay ); | |
| 134 | - } | |
| 135 | - }; | |
| 136 | - | |
| 137 | - } | |
| 138 | - | |
| 139 | - return this; | |
| 140 | -} | |
| 28 | +function NoJQueryPostMessageMixin(n,e){var t,i,o,s,a,r=1;return window.postMessage?(window.addEventListener?(t=function(n){window.addEventListener("message",n,!1)},i=function(n){window.removeEventListener("message",n,!1)}):(t=function(n){window.attachEvent("onmessage",n)},i=function(n){window.detachEvent("onmessage",n)}),this[n]=function(n,e,t){e&&t.postMessage(n,e.replace(/([^:]+:\/\/[^\/]+).*/,"$1"))},this[e]=function(n,e,s){if(o&&(i(o),o=null),!n)return!1;o=t((function(t){switch(Object.prototype.toString.call(e)){case"[object String]":if(e!==t.origin)return!1;break;case"[object Function]":if(e(t.origin))return!1}n(t)}))}):(this[n]=function(n,e,t){e&&(t.location=e.replace(/#.*$/,"")+"#"+ +new Date+r+++"&"+n)},this[e]=function(n,e,t){s&&(clearInterval(s),s=null),n&&(t="number"==typeof e?e:"number"==typeof t?t:100,s=setInterval((function(){var e=document.location.hash,t=/^#?\d+&/;e!==a&&t.test(e)&&(a=e,n({data:e.replace(t,"")}))}),t))}),this} | |