nojquery.ba-postmessage.js
9 years ago
nojquery.ba-postmessage.min.js
9 years ago
postmessage.js
9 years ago
nojquery.ba-postmessage.js
140 lines
| 1 | /*! |
| 2 | * jQuery postMessage - v0.5 - 9/11/2009 |
| 3 | * http://benalman.com/projects/jquery-postmessage-plugin/ |
| 4 | * |
| 5 | * Copyright (c) 2009 "Cowboy" Ben Alman |
| 6 | * Dual licensed under the MIT and GPL licenses. |
| 7 | * http://benalman.com/about/license/ |
| 8 | * |
| 9 | * Non-jQuery fork by Jeff Lee |
| 10 | * |
| 11 | * This fork consists of the following changes: |
| 12 | * 1. Basic code cleanup and restructuring, for legibility. |
| 13 | * 2. The `postMessage` and `receiveMessage` functions can be bound arbitrarily, |
| 14 | * in terms of both function names and object scope. Scope is specified by |
| 15 | * the the "this" context of NoJQueryPostMessageMixin(); |
| 16 | * 3. I've removed the check for Opera 9.64, which used `$.browser`. There were |
| 17 | * at least three different GitHub users requesting the removal of this |
| 18 | * "Opera sniff" on the original project's Issues page, so I figured this |
| 19 | * would be a relatively safe change. |
| 20 | * 4. `postMessage` no longer uses `$.param` to serialize messages that are not |
| 21 | * strings. I actually prefer this structure anyway. `receiveMessage` does |
| 22 | * not implement a corresponding deserialization step, and as such it seems |
| 23 | * cleaner and more symmetric to leave both data serialization and |
| 24 | * deserialization to the client. |
| 25 | * 5. The use of `$.isFunction` is replaced by a functionally-identical check. |
| 26 | * 6. The `$:nomunge` YUI option is no longer necessary. |
| 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 | } |