Skip to content

Commit 7a47dc0

Browse files
committed
to send: $.postmessage(...), to receive: $.postmessage.bind(...)
1 parent 742c9c9 commit 7a47dc0

File tree

1 file changed

+48
-77
lines changed

1 file changed

+48
-77
lines changed

jquery.postmessage.js

Lines changed: 48 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,29 @@
55
window.console.log = window.console.warn = window.console.error = window.console.debug = function(){};
66
}
77

8-
$.fn.postmessage = function(options /* ["bind", fn, origin]*/) {
9-
var args = Array.prototype.slice.call(arguments);
10-
var method = "send";
11-
if (args.length > 1) {
12-
method = args.shift();
13-
}
14-
return this.each(function() {
15-
$.postmessage.apply(null, [method, this].concat(args));
16-
});
8+
$.fn.postmessage = function() {
9+
console.log("usage: \nto send: $.postmessage(options)\nto receive: $.postmessage.bind(type, fn, [origin])");
10+
return this;
1711
};
1812

19-
var pm = $.postmessage = function(method, w) {
20-
if (method in $.postmessage) {
21-
var args = Array.prototype.slice.call(arguments);
22-
args.shift();
23-
pm[method].apply(null, args);
24-
}
25-
else {
26-
console.warn("postmessage method not supported", method);
27-
}
13+
var pm = $.postmessage = $.pm = function(options) {
14+
pm.send(options);
2815
};
2916

3017
/**
31-
* options :
32-
* {
33-
* type: // message type ( type used for $.postmessage.bind )
34-
* data: // JSON data message
35-
* success: // success handler
36-
* error: // error handler
37-
* }
18+
* options - @see $.postmessage.defaults
3819
*/
39-
pm.send = function(w, options) {
40-
var win = pm._window(w);
41-
var o = $.extend({}, pm.defaults, options);
20+
pm.send = function(options) {
21+
var o = $.extend({}, pm.defaults, options),
22+
target = o.target;
23+
if (!o.target) {
24+
console.warn("postmessage target window required");
25+
return;
26+
}
27+
if (!o.type) {
28+
console.warn("postmessage type required");
29+
return;
30+
}
4231
var msg = {data:o.data, type:o.type};
4332
if (o.success) {
4433
msg.callback = pm._callback(o.success);
@@ -47,25 +36,18 @@
4736
msg.errback = pm._callback(o.error);
4837
}
4938

50-
if (("postMessage" in window) && !o.hash) {
39+
if (("postMessage" in target) && !o.hash) {
5140
pm._bind();
52-
win.postMessage(JSON.stringify(msg), o.origin || '*');
41+
target.postMessage(JSON.stringify(msg), o.origin || '*');
5342
}
5443
else {
5544
pm.hash._bind();
56-
pm.hash.send(win, o, msg);
45+
pm.hash.send(o, msg);
5746
}
5847
};
5948

6049

61-
pm.bind = function(w, type, fn, origin, hash) {
62-
// TODO: assert w === window
63-
// you can only bind to current window
64-
var win = pm._window(w);
65-
if (win !== window) {
66-
console.warn("postmessage bind only allowed on current window");
67-
return;
68-
}
50+
pm.bind = function(type, fn, origin, hash) {
6951
if (("postMessage" in window) && !hash) {
7052
pm._bind();
7153
}
@@ -85,12 +67,7 @@
8567
fns.push({fn:fn, origin:origin || pm.origin});
8668
};
8769

88-
pm.unbind = function(w, type, fn) {
89-
var win = pm._window(w);
90-
if (win !== window) {
91-
console.warn("postmessage unbind only allowed on current window");
92-
return;
93-
}
70+
pm.unbind = function(type, fn) {
9471
var l = $(document).data("listeners.postmessage");
9572
if (l) {
9673
if (type) {
@@ -122,21 +99,14 @@
12299
* default options
123100
*/
124101
pm.defaults = {
125-
type: null,
126-
data: null,
127-
success: null,
128-
error: null,
129-
origin: "*"
130-
};
131-
132-
pm._window = function(w) {
133-
// if w is an iframe try to get contentWindow if we have access, otherwise, return w
134-
try {
135-
return w.contentWindow || w;
136-
}
137-
catch(ex) {
138-
return w;
139-
}
102+
target: null, /* target window (required) */
103+
url: null, /* target window url (required if no window.postMessage or hash == true) */
104+
type: null, /* message type (required) */
105+
data: null, /* message data (required) */
106+
success: null, /* success callback (optional) */
107+
error: null, /* error callback (optional) */
108+
origin: "*", /* postmessage origin (optional) */
109+
hash: false /* use location hash for message passing (optional) */
140110
};
141111

142112
pm._CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
@@ -180,17 +150,17 @@
180150
var msg = JSON.parse(e.data);
181151
}
182152
catch (ex) {
183-
console.error("postmessage data invalid json: ", ex);
153+
console.warn("postmessage data invalid json: ", ex);
184154
return;
185155
}
186156

187157
if (!msg.type) {
188-
console.error("postmessage message type required");
158+
console.warn("postmessage message type required");
189159
return;
190160
}
191161

192-
var cbs = $(document).data("callbacks.postmessage") || {};
193-
var cb = cbs[msg.type];
162+
var cbs = $(document).data("callbacks.postmessage") || {},
163+
cb = cbs[msg.type];
194164
if (cb) {
195165
cb(msg.data);
196166
}
@@ -206,20 +176,20 @@
206176
message: "postmessage origin mismatch",
207177
origin: [e.origin, o.origin]
208178
};
209-
pm.send(e.source, {data: error, type: msg.errback});
179+
pm.send({target:e.source, data: error, type: msg.errback});
210180
}
211181
return;
212182
}
213183
try {
214184
var r = o.fn(msg.data);
215185
if (msg.callback) {
216-
pm.send(e.source, {data: r, type: msg.callback});
186+
pm.send({target:e.source, data: r, type: msg.callback});
217187
}
218188
}
219189
catch (ex) {
220190
if (msg.errback) {
221191
// notify post message errback
222-
pm.send(e.source, {data: ex, type: msg.errback});
192+
pm.send({target:e.source, data: ex, type: msg.errback});
223193
}
224194
}
225195
});
@@ -228,16 +198,17 @@
228198

229199
pm.hash = {
230200

231-
send: function(target_window, options, msg) {
201+
send: function(options, msg) {
232202
//console.log("hash.send", target_window, options, msg);
233-
var target_url = options.url;
203+
var target_window = options.target,
204+
target_url = options.url;
234205
if (!target_url) {
235206
console.warn("postmessage target window url is required");
236207
return;
237208
}
238209
target_url = pm.hash._url(target_url);
239-
var source_url = pm.hash._url(window.location.href);
240-
var source_window = null;
210+
var source_window,
211+
source_url = pm.hash._url(window.location.href);
241212

242213
if (window == target_window.parent) {
243214
source_window = "parent";
@@ -319,9 +290,9 @@
319290
return;
320291
}
321292

322-
var msg = hash.postmessage;
323-
var cbs = $(document).data("callbacks.postmessage") || {};
324-
var cb = cbs[msg.type];
293+
var msg = hash.postmessage,
294+
cbs = $(document).data("callbacks.postmessage") || {},
295+
cb = cbs[msg.type];
325296
if (cb) {
326297
cb(msg.data);
327298
}
@@ -347,21 +318,21 @@
347318
message: "postmessage origin mismatch",
348319
origin: [origin, o.origin]
349320
};
350-
$.postmessage.send(source_window, {data: error, type: msg.errback, hash:true, url:hash.source.url});
321+
$.postmessage({target: source_window, data: error, type: msg.errback, hash:true, url:hash.source.url});
351322
}
352323
return;
353324
}
354325
}
355326
try {
356327
var r = o.fn(msg.data);
357328
if (msg.callback) {
358-
$.postmessage.send(source_window, {data: r, type: msg.callback, hash:true, url:hash.source.url});
329+
$.postmessage({target:source_window, data: r, type: msg.callback, hash:true, url:hash.source.url});
359330
}
360331
}
361332
catch (ex) {
362333
if (msg.errback) {
363334
// notify post message errback
364-
$.postmessage.send(source_window, {data: ex, type: msg.errback, hash:true, url:hash.source.url});
335+
$.postmessage({target:source_window, data: ex, type: msg.errback, hash:true, url:hash.source.url});
365336
}
366337
}
367338
});

0 commit comments

Comments
 (0)