crossframe
Advanced tools
Comparing version 1.0.2 to 2.0.0
{ | ||
"name": "crossframe", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Send messages between a page and its (sandboxed) iframes with ease.", | ||
@@ -5,0 +5,0 @@ "main": "src/crossframe.js", |
@@ -9,45 +9,2 @@ # crossframe | ||
_index.html_ | ||
```javascript | ||
<script type="text/javascript" src="../src/crossframe.js"></script> | ||
<script type="text/javascript"> | ||
window.addEventListener('load', function(){ | ||
var frameEl = document.getElementById('myframe'); | ||
var cf = new CrossFrame( frameEl ); | ||
cf.ready(function(){ | ||
cf.on('iframe_to_parent', function( data, callback ) { | ||
console.log('on iframe_to_parent', data); | ||
callback( null, true ); | ||
}); | ||
cf.emit('parent_to_iframe', { x: 'y' }, function( err, result ){ | ||
console.log('parent_to_iframe callback', 'err:', err, 'result:', result); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
<iframe id="myframe" src="frame.html" width="600" height="400" sandbox="allow-scripts"></iframe> | ||
``` | ||
_frame.html_ | ||
```javascript | ||
<script type="text/javascript" src="../src/crossframe.js"></script> | ||
<script type="text/javascript"> | ||
window.addEventListener('load', function(){ | ||
var cf = new CrossFrame(); | ||
cf.ready(function(){ | ||
cf.on('parent_to_iframe', function( data, callback ){ | ||
console.log('on parent_to_iframe', data); | ||
callback( new Error('Test error!') ); | ||
}); | ||
cf.emit('iframe_to_parent', { foo: 'bar' }, function( err, result ){ | ||
console.log('my_message callback', 'err:', err, 'result:', result); | ||
}); | ||
}); | ||
}); | ||
</script> | ||
``` | ||
For more examples, see `./demo/`. | ||
See `/demo` |
/* | ||
CrossFrame.js - v1.0.0 | ||
Created by Emile Nijssen - www.emilenijssen.nl | ||
CrossFrame.js | ||
Created by Emile Nijssen - https://www.emilenijssen.nl | ||
*/ | ||
@@ -8,4 +8,4 @@ | ||
var EVENT_MESSAGE = 'CROSSFRAME_MESSAGE'; | ||
var EVENT_READY = 'CROSSFRAME_READY'; | ||
var EVENT_MESSAGE = 'CF2_MESSAGE'; | ||
var EVENT_READY = 'CF2_READY'; | ||
@@ -102,3 +102,6 @@ var webviewReady = false; | ||
var obj = jsonToObj( e.data.substr( EVENT_MESSAGE.length ) ); | ||
var str = e.data; | ||
str = str.substr( EVENT_MESSAGE.length ); | ||
str = hexDecode(str); | ||
var obj = jsonToObj( str ); | ||
if( obj.type === 'tx' ) { | ||
@@ -185,3 +188,3 @@ | ||
CrossFrame.prototype.postMessage = function( message ) { | ||
this._post( EVENT_MESSAGE + objToJson(message) ); | ||
this._post( EVENT_MESSAGE + hexEncode( objToJson(message) ) ); | ||
} | ||
@@ -278,2 +281,25 @@ | ||
function hexEncode(input){ | ||
var hex, i; | ||
var result = ""; | ||
for (i=0; i<input.length; i++) { | ||
hex = input.charCodeAt(i).toString(16); | ||
result += ("000"+hex).slice(-4); | ||
} | ||
return result | ||
} | ||
function hexDecode(input){ | ||
var j; | ||
var hexes = input.match(/.{1,4}/g) || []; | ||
var back = ""; | ||
for(j = 0; j<hexes.length; j++) { | ||
back += String.fromCharCode(parseInt(hexes[j], 16)); | ||
} | ||
return back; | ||
} | ||
})() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
240
9253
9