lib/shutterbug/shutterbug.js in shutterbug-0.0.10 vs lib/shutterbug/shutterbug.js in shutterbug-0.0.11
- old
+ new
@@ -1,12 +1,9 @@
/*global $ */
(function(){
var getBaseUrl = function() {
- // var base = window.location.protocol + "//" + window.location.host + "/";
- // var root_path = window.location.pathname.split("/").slice(1,-1).join("/");
- // return base + root_path;
var base = window.location.href;
return base;
};
var cloneDomItem =function(elem, elemTag) {
@@ -108,36 +105,44 @@
getPng: getPng,
getHtmlFragment: getHtmlFragment,
requestHtmlFrag: requestHtmlFrag
};
- var htmlFragRequestListen = function(message) {
+ var handleMessage = function(message, signature, func) {
var data = message.data;
if (typeof data === 'string') {
- data = JSON.parse(data);
+ try {
+ data = JSON.parse(data);
+ if (data.type === signature) {
+ func(data);
+ }
+ } catch(e) {
+ // Not a json message. Ignore it. We only speak json.
+ }
}
- if(data.type === 'htmlFragRequest') {
+ };
+
+ var htmlFragRequestListen = function(message) {
+ var send_response = function(data) {
var response = {
type: 'htmlFragResponse',
value: shutterbugInstance.getHtmlFragment(),
- id: data.id // return to sender only...
+ id: data.id // return to sender only...
};
message.source.postMessage(JSON.stringify(response),"*");
- }
+ };
+ handleMessage(message, 'htmlFragRequest', send_response);
};
var htmlFragResponseListen = function(message) {
- var data = message.data;
- if (typeof data === 'string') {
- data = JSON.parse(data);
- }
- var html = null;
- if(data.type === 'htmlFragResponse') {
- if(data.id == shutterbugInstance.id) {
+ var send_response = function(data) {
+ var html = null;
+ if(data.id === shutterbugInstance.id) {
html = data.value;
- shutterbugInstance.getPng(html);
+ shutterbugInstance.getPng(data.value);
}
- }
+ };
+ handleMessage(message, 'htmlFragResponse', send_response);
};
$(document).ready(function () {
window.addEventListener('message', htmlFragRequestListen, false);
window.addEventListener('message', htmlFragResponseListen, false);
\ No newline at end of file