|
5 | 5 | window.console.log = window.console.warn = window.console.error = window.console.debug = function(){}; |
6 | 6 | } |
7 | 7 |
|
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; |
17 | 11 | }; |
18 | 12 |
|
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); |
28 | 15 | }; |
29 | 16 |
|
30 | 17 | /** |
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 |
38 | 19 | */ |
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 | + } |
42 | 31 | var msg = {data:o.data, type:o.type}; |
43 | 32 | if (o.success) { |
44 | 33 | msg.callback = pm._callback(o.success); |
|
47 | 36 | msg.errback = pm._callback(o.error); |
48 | 37 | } |
49 | 38 |
|
50 | | - if (("postMessage" in window) && !o.hash) { |
| 39 | + if (("postMessage" in target) && !o.hash) { |
51 | 40 | pm._bind(); |
52 | | - win.postMessage(JSON.stringify(msg), o.origin || '*'); |
| 41 | + target.postMessage(JSON.stringify(msg), o.origin || '*'); |
53 | 42 | } |
54 | 43 | else { |
55 | 44 | pm.hash._bind(); |
56 | | - pm.hash.send(win, o, msg); |
| 45 | + pm.hash.send(o, msg); |
57 | 46 | } |
58 | 47 | }; |
59 | 48 |
|
60 | 49 |
|
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) { |
69 | 51 | if (("postMessage" in window) && !hash) { |
70 | 52 | pm._bind(); |
71 | 53 | } |
|
85 | 67 | fns.push({fn:fn, origin:origin || pm.origin}); |
86 | 68 | }; |
87 | 69 |
|
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) { |
94 | 71 | var l = $(document).data("listeners.postmessage"); |
95 | 72 | if (l) { |
96 | 73 | if (type) { |
|
122 | 99 | * default options |
123 | 100 | */ |
124 | 101 | 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) */ |
140 | 110 | }; |
141 | 111 |
|
142 | 112 | pm._CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); |
|
180 | 150 | var msg = JSON.parse(e.data); |
181 | 151 | } |
182 | 152 | catch (ex) { |
183 | | - console.error("postmessage data invalid json: ", ex); |
| 153 | + console.warn("postmessage data invalid json: ", ex); |
184 | 154 | return; |
185 | 155 | } |
186 | 156 |
|
187 | 157 | if (!msg.type) { |
188 | | - console.error("postmessage message type required"); |
| 158 | + console.warn("postmessage message type required"); |
189 | 159 | return; |
190 | 160 | } |
191 | 161 |
|
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]; |
194 | 164 | if (cb) { |
195 | 165 | cb(msg.data); |
196 | 166 | } |
|
206 | 176 | message: "postmessage origin mismatch", |
207 | 177 | origin: [e.origin, o.origin] |
208 | 178 | }; |
209 | | - pm.send(e.source, {data: error, type: msg.errback}); |
| 179 | + pm.send({target:e.source, data: error, type: msg.errback}); |
210 | 180 | } |
211 | 181 | return; |
212 | 182 | } |
213 | 183 | try { |
214 | 184 | var r = o.fn(msg.data); |
215 | 185 | if (msg.callback) { |
216 | | - pm.send(e.source, {data: r, type: msg.callback}); |
| 186 | + pm.send({target:e.source, data: r, type: msg.callback}); |
217 | 187 | } |
218 | 188 | } |
219 | 189 | catch (ex) { |
220 | 190 | if (msg.errback) { |
221 | 191 | // 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}); |
223 | 193 | } |
224 | 194 | } |
225 | 195 | }); |
|
228 | 198 |
|
229 | 199 | pm.hash = { |
230 | 200 |
|
231 | | - send: function(target_window, options, msg) { |
| 201 | + send: function(options, msg) { |
232 | 202 | //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; |
234 | 205 | if (!target_url) { |
235 | 206 | console.warn("postmessage target window url is required"); |
236 | 207 | return; |
237 | 208 | } |
238 | 209 | 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); |
241 | 212 |
|
242 | 213 | if (window == target_window.parent) { |
243 | 214 | source_window = "parent"; |
|
319 | 290 | return; |
320 | 291 | } |
321 | 292 |
|
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]; |
325 | 296 | if (cb) { |
326 | 297 | cb(msg.data); |
327 | 298 | } |
|
347 | 318 | message: "postmessage origin mismatch", |
348 | 319 | origin: [origin, o.origin] |
349 | 320 | }; |
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}); |
351 | 322 | } |
352 | 323 | return; |
353 | 324 | } |
354 | 325 | } |
355 | 326 | try { |
356 | 327 | var r = o.fn(msg.data); |
357 | 328 | 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}); |
359 | 330 | } |
360 | 331 | } |
361 | 332 | catch (ex) { |
362 | 333 | if (msg.errback) { |
363 | 334 | // 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}); |
365 | 336 | } |
366 | 337 | } |
367 | 338 | }); |
|
0 commit comments