Permalink
Browse files

getting things ready for ender

  • Loading branch information...
thomassturm committed May 24, 2011
1 parent ed0aa64 commit 02dc4c5e9bd75ed7bd517ac54aea236d216f2cfa
Showing with 181 additions and 168 deletions.
  1. +168 −168 ender-postmessage.js
  2. +13 −0 package.json
View
@@ -20,171 +20,171 @@
// 0.5 - (9/11/2009) Improved cache-busting
// 0.4 - (8/25/2009) Initial release
-(function($){
-
- // A few vars used in non-awesome browsers.
- var interval_id,
- last_hash,
- cache_bust = 1,
-
- // A var used in awesome browsers.
- rm_callback,
-
- // A few convenient shortcuts.
- window = this,
- FALSE = !1,
-
- // Reused internal strings.
- postMessage = 'postMessage',
- addEventListener = 'addEventListener',
-
- p_receiveMessage,
-
- // Method: ender.postMessage
- //
- // This method will call window.postMessage if available, setting the
- // targetOrigin parameter to the base of the target_url parameter for maximum
- // security in browsers that support it. If window.postMessage is not available,
- // the target window's location.hash will be used to pass the message.
- //
- // Please Note: The ender version does not support the object serialization
- // for postMessage
- //
- // Usage:
- //
- // > ender.postMessage( message, target_url [, target ] );
- //
- // Arguments:
- //
- // message - (String) A message to be passed to the other frame.
- // target_url - (String) The URL of the other frame this window is
- // attempting to communicate with. This must be the exact URL (including
- // any query string) of the other window for this script to work in
- // browsers that don't support window.postMessage.
- // target - (Object) A reference to the other frame this window is
- // attempting to communicate with. If omitted, defaults to `parent`.
- //
- // Returns:
- //
- // Nothing.
-
- $[postMessage] = function( message, target_url, target ) {
- if ( !target_url ) { return; }
-
- // Default to parent if unspecified.
- target = target || parent;
-
- if ( has_postMessage ) {
- // The browser supports window.postMessage, so call it with a targetOrigin
- // set appropriately, based on the target_url parameter.
- target[postMessage]( message, target_url.replace( /([^:]+:\/\/[^\/]+).*/, '$1' ) );
-
- } else if ( target_url ) {
- // The browser does not support window.postMessage, so set the location
- // of the target to target_url#message. A bit ugly, but it works! A cache
- // bust parameter is added to ensure that repeat messages trigger the
- // callback.
- target.location = target_url.replace( /#.*$/, '' ) + '#' + (+new Date) + (cache_bust++) + '&' + message;
- }
- };
-
- // Method: ender.receiveMessage
- //
- // Register a single callback for either a window.postMessage call, if
- // supported, or if unsupported, for any change in the current window
- // location.hash. If window.postMessage is supported and source_origin is
- // specified, the source window will be checked against this for maximum
- // security. If window.postMessage is unsupported, a polling loop will be
- // started to watch for changes to the location.hash.
- //
- // Note that for simplicity's sake, only a single callback can be registered
- // at one time. Passing no params will unbind this event (or stop the polling
- // loop), and calling this method a second time with another callback will
- // unbind the event (or stop the polling loop) first, before binding the new
- // callback.
- //
- // Also note that if window.postMessage is available, the optional
- // source_origin param will be used to test the event.origin property. From
- // the MDC window.postMessage docs: This string is the concatenation of the
- // protocol and "://", the host name if one exists, and ":" followed by a port
- // number if a port is present and differs from the default port for the given
- // protocol. Examples of typical origins are https://example.org (implying
- // port 443), http://example.net (implying port 80), and http://example.com:8080.
- //
- // Usage:
- //
- // > ender.receiveMessage( callback [, source_origin ] [, delay ] );
- //
- // Arguments:
- //
- // callback - (Function) This callback will execute whenever a <ender.postMessage>
- // message is received, provided the source_origin matches. If callback is
- // omitted, any existing receiveMessage event bind or polling loop will be
- // canceled.
- // source_origin - (String) If window.postMessage is available and this value
- // is not equal to the event.origin property, the callback will not be
- // called.
- // source_origin - (Function) If window.postMessage is available and this
- // function returns false when passed the event.origin property, the
- // callback will not be called.
- // delay - (Number) An optional zero-or-greater delay in milliseconds at
- // which the polling loop will execute (for browser that don't support
- // window.postMessage). If omitted, defaults to 100.
- //
- // Returns:
- //
- // Nothing!
-
- $.receiveMessage = p_receiveMessage = function( callback, source_origin, delay ) {
- if ( has_postMessage ) {
- // Since the browser supports window.postMessage, the callback will be
- // bound to the actual event associated with window.postMessage.
-
- if ( callback ) {
- // Unbind an existing callback if it exists.
- rm_callback && p_receiveMessage();
-
- // Bind the callback. A reference to the callback is stored for ease of
- // unbinding.
- rm_callback = function(e) {
- if ( ( typeof source_origin === 'string' && e.origin !== source_origin )
- || ( $.isFunction( source_origin ) && source_origin( e.origin ) === FALSE ) ) {
- return FALSE;
- }
- callback( e );
- };
- }
-
- if ( window[addEventListener] ) {
- window[ callback ? addEventListener : 'removeEventListener' ]( 'message', rm_callback, FALSE );
- } else {
- window[ callback ? 'attachEvent' : 'detachEvent' ]( 'onmessage', rm_callback );
- }
-
- } else {
- // Since the browser sucks, a polling loop will be started, and the
- // callback will be called whenever the location.hash changes.
-
- interval_id && clearInterval( interval_id );
- interval_id = null;
-
- if ( callback ) {
- delay = typeof source_origin === 'number'
- ? source_origin
- : typeof delay === 'number'
- ? delay
- : 100;
-
- interval_id = setInterval(function(){
- var hash = document.location.hash,
- re = /^#?\d+&/;
- if ( hash !== last_hash && re.test( hash ) ) {
- last_hash = hash;
- callback({ data: hash.replace( re, '' ) });
- }
- }, delay );
- }
- }
- };
-
-})(ender);
+!function ($) {
+ $.ender({
+ // A few vars used in non-awesome browsers.
+ var interval_id,
+ last_hash,
+ cache_bust = 1,
+
+ // A var used in awesome browsers.
+ rm_callback,
+
+ // A few convenient shortcuts.
+ window = this,
+ FALSE = !1,
+
+ // Reused internal strings.
+ postMessage = 'postMessage',
+ addEventListener = 'addEventListener',
+
+ p_receiveMessage,
+
+ // Method: ender.postMessage
+ //
+ // This method will call window.postMessage if available, setting the
+ // targetOrigin parameter to the base of the target_url parameter for maximum
+ // security in browsers that support it. If window.postMessage is not available,
+ // the target window's location.hash will be used to pass the message.
+ //
+ // Please Note: The ender version does not support the jQuery object serialization
+ // for postMessage
+ //
+ // Usage:
+ //
+ // > ender.postMessage( message, target_url [, target ] );
+ //
+ // Arguments:
+ //
+ // message - (String) A message to be passed to the other frame.
+ // target_url - (String) The URL of the other frame this window is
+ // attempting to communicate with. This must be the exact URL (including
+ // any query string) of the other window for this script to work in
+ // browsers that don't support window.postMessage.
+ // target - (Object) A reference to the other frame this window is
+ // attempting to communicate with. If omitted, defaults to `parent`.
+ //
+ // Returns:
+ //
+ // Nothing.
+
+ postMessage = function( message, target_url, target ) {
+ if ( !target_url ) { return; }
+
+ // Default to parent if unspecified.
+ target = target || parent;
+
+ if ( has_postMessage ) {
+ // The browser supports window.postMessage, so call it with a targetOrigin
+ // set appropriately, based on the target_url parameter.
+ target[postMessage]( message, target_url.replace( /([^:]+:\/\/[^\/]+).*/, '$1' ) );
+
+ } else if ( target_url ) {
+ // The browser does not support window.postMessage, so set the location
+ // of the target to target_url#message. A bit ugly, but it works! A cache
+ // bust parameter is added to ensure that repeat messages trigger the
+ // callback.
+ target.location = target_url.replace( /#.*$/, '' ) + '#' + (+new Date) + (cache_bust++) + '&' + message;
+ }
+ };
+
+ // Method: ender.receiveMessage
+ //
+ // Register a single callback for either a window.postMessage call, if
+ // supported, or if unsupported, for any change in the current window
+ // location.hash. If window.postMessage is supported and source_origin is
+ // specified, the source window will be checked against this for maximum
+ // security. If window.postMessage is unsupported, a polling loop will be
+ // started to watch for changes to the location.hash.
+ //
+ // Note that for simplicity's sake, only a single callback can be registered
+ // at one time. Passing no params will unbind this event (or stop the polling
+ // loop), and calling this method a second time with another callback will
+ // unbind the event (or stop the polling loop) first, before binding the new
+ // callback.
+ //
+ // Also note that if window.postMessage is available, the optional
+ // source_origin param will be used to test the event.origin property. From
+ // the MDC window.postMessage docs: This string is the concatenation of the
+ // protocol and "://", the host name if one exists, and ":" followed by a port
+ // number if a port is present and differs from the default port for the given
+ // protocol. Examples of typical origins are https://example.org (implying
+ // port 443), http://example.net (implying port 80), and http://example.com:8080.
+ //
+ // Usage:
+ //
+ // > ender.receiveMessage( callback [, source_origin ] [, delay ] );
+ //
+ // Arguments:
+ //
+ // callback - (Function) This callback will execute whenever a <ender.postMessage>
+ // message is received, provided the source_origin matches. If callback is
+ // omitted, any existing receiveMessage event bind or polling loop will be
+ // canceled.
+ // source_origin - (String) If window.postMessage is available and this value
+ // is not equal to the event.origin property, the callback will not be
+ // called.
+ // source_origin - (Function) If window.postMessage is available and this
+ // function returns false when passed the event.origin property, the
+ // callback will not be called.
+ // delay - (Number) An optional zero-or-greater delay in milliseconds at
+ // which the polling loop will execute (for browser that don't support
+ // window.postMessage). If omitted, defaults to 100.
+ //
+ // Returns:
+ //
+ // Nothing!
+
+ receiveMessage = p_receiveMessage = function( callback, source_origin, delay ) {
+ if ( has_postMessage ) {
+ // Since the browser supports window.postMessage, the callback will be
+ // bound to the actual event associated with window.postMessage.
+
+ if ( callback ) {
+ // Unbind an existing callback if it exists.
+ rm_callback && p_receiveMessage();
+
+ // Bind the callback. A reference to the callback is stored for ease of
+ // unbinding.
+ rm_callback = function(e) {
+ if ( ( typeof source_origin === 'string' && e.origin !== source_origin )
+ || ( $.isFunction( source_origin ) && source_origin( e.origin ) === FALSE ) ) {
+ return FALSE;
+ }
+ callback( e );
+ };
+ }
+
+ if ( window[addEventListener] ) {
+ window[ callback ? addEventListener : 'removeEventListener' ]( 'message', rm_callback, FALSE );
+ } else {
+ window[ callback ? 'attachEvent' : 'detachEvent' ]( 'onmessage', rm_callback );
+ }
+
+ } else {
+ // Since the browser sucks, a polling loop will be started, and the
+ // callback will be called whenever the location.hash changes.
+
+ interval_id && clearInterval( interval_id );
+ interval_id = null;
+
+ if ( callback ) {
+ delay = typeof source_origin === 'number'
+ ? source_origin
+ : typeof delay === 'number'
+ ? delay
+ : 100;
+
+ interval_id = setInterval(function(){
+ var hash = document.location.hash,
+ re = /^#?\d+&/;
+ if ( hash !== last_hash && re.test( hash ) ) {
+ last_hash = hash;
+ callback({ data: hash.replace( re, '' ) });
+ }
+ }, delay );
+ }
+ }
+ };
+ });
+}(ender);
View
@@ -0,0 +1,13 @@
+{
+ "name": "postmessage",
+ "description": "Simple and easy window.postMessage communication",
+ "version": "0.1",
+ "homepage": "https://github.com/thomassturm/ender-postmessage",
+ "authors": ["Thomas Sturm <@thomassturm>"],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/thomassturm/ender-postmessage.git"
+ },
+ "keywords": ["ender", "postmessage"],
+ "main": "./ender-postmessage.js"
+}

0 comments on commit 02dc4c5

Please sign in to comment.