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.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / _inc / postmessage.js
jetpack / _inc Last commit date
blocks 4 years ago build 4 years ago fonts 8 years ago genericons 6 years ago lib 4 years ago social-logos 5 years ago accessible-focus.js 5 years ago class.jetpack-provision.php 4 years ago connect-button.js 4 years ago crowdsignal-shortcode.js 5 years ago crowdsignal-survey.js 5 years ago facebook-embed.js 5 years ago gallery-settings.js 5 years ago genericons.php 11 years ago header.php 7 years ago jetpack-admin.js 5 years ago jetpack-connection-banner.js 5 years ago jetpack-deactivate-dialog.js 5 years ago jetpack-modules.js 5 years ago jetpack-modules.models.js 6 years ago jetpack-modules.views.js 5 years ago jetpack-recommendations-banner.js 5 years ago jetpack-server-sandbox.php 6 years ago jquery.jetpack-resize.js 5 years ago polldaddy-shortcode.js 5 years ago postmessage.js 5 years ago social-logos.php 5 years ago twitter-timeline.js 5 years ago
postmessage.js
434 lines
1 /**
2 The MIT License
3
4 Copyright (c) 2010 Daniel Park (http://metaweb.com, http://postmessage.freebaseapps.com)
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 **/
24 var NO_JQUERY = {};
25 (function(window, $, undefined) {
26
27 if (!("console" in window)) {
28 var c = window.console = {};
29 c.log = c.warn = c.error = c.debug = function(){};
30 }
31
32 if ($ === NO_JQUERY) {
33 // jQuery is optional
34 $ = {
35 fn: {},
36 extend: function() {
37 var a = arguments[0];
38 for (var i=1,len=arguments.length; i<len; i++) {
39 var b = arguments[i];
40 for (var prop in b) {
41 a[prop] = b[prop];
42 }
43 }
44 return a;
45 }
46 };
47 }
48
49 $.fn.pm = function() {
50 console.log("usage: \nto send: $.pm(options)\nto receive: $.pm.bind(type, fn, [origin])");
51 return this;
52 };
53
54 // send postmessage
55 $.pm = window.pm = function(options) {
56 pm.send(options);
57 };
58
59 // bind postmessage handler
60 $.pm.bind = window.pm.bind = function(type, fn, origin, hash, async_reply) {
61 pm.bind(type, fn, origin, hash, async_reply === true);
62 };
63
64 // unbind postmessage handler
65 $.pm.unbind = window.pm.unbind = function(type, fn) {
66 pm.unbind(type, fn);
67 };
68
69 // default postmessage origin on bind
70 $.pm.origin = window.pm.origin = null;
71
72 // default postmessage polling if using location hash to pass postmessages
73 $.pm.poll = window.pm.poll = 200;
74
75 var pm = {
76
77 send: function(options) {
78 var o = $.extend({}, pm.defaults, options),
79 target = o.target;
80 if (!o.target) {
81 console.warn("postmessage target window required");
82 return;
83 }
84 if (!o.type) {
85 console.warn("postmessage type required");
86 return;
87 }
88 var msg = {data:o.data, type:o.type};
89 if (o.success) {
90 msg.callback = pm._callback(o.success);
91 }
92 if (o.error) {
93 msg.errback = pm._callback(o.error);
94 }
95 if (("postMessage" in target) && !o.hash) {
96 pm._bind();
97 target.postMessage(JSON.stringify(msg), o.origin || '*');
98 }
99 else {
100 pm.hash._bind();
101 pm.hash.send(o, msg);
102 }
103 },
104
105 bind: function(type, fn, origin, hash, async_reply) {
106 pm._replyBind ( type, fn, origin, hash, async_reply );
107 },
108
109 _replyBind: function(type, fn, origin, hash, isCallback) {
110 if (("postMessage" in window) && !hash) {
111 pm._bind();
112 }
113 else {
114 pm.hash._bind();
115 }
116 var l = pm.data("listeners.postmessage");
117 if (!l) {
118 l = {};
119 pm.data("listeners.postmessage", l);
120 }
121 var fns = l[type];
122 if (!fns) {
123 fns = [];
124 l[type] = fns;
125 }
126 fns.push({fn:fn, callback: isCallback, origin:origin || $.pm.origin});
127 },
128
129 unbind: function(type, fn) {
130 var l = pm.data("listeners.postmessage");
131 if (l) {
132 if (type) {
133 if (fn) {
134 // remove specific listener
135 var fns = l[type];
136 if (fns) {
137 var m = [];
138 for (var i=0,len=fns.length; i<len; i++) {
139 var o = fns[i];
140 if (o.fn !== fn) {
141 m.push(o);
142 }
143 }
144 l[type] = m;
145 }
146 }
147 else {
148 // remove all listeners by type
149 delete l[type];
150 }
151 }
152 else {
153 // unbind all listeners of all type
154 for (var i in l) {
155 delete l[i];
156 }
157 }
158 }
159 },
160
161 data: function(k, v) {
162 if (v === undefined) {
163 return pm._data[k];
164 }
165 pm._data[k] = v;
166 return v;
167 },
168
169 _data: {},
170
171 _CHARS: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''),
172
173 _random: function() {
174 var r = [];
175 for (var i=0; i<32; i++) {
176 r[i] = pm._CHARS[0 | Math.random() * 32];
177 };
178 return r.join("");
179 },
180
181 _callback: function(fn) {
182 var cbs = pm.data("callbacks.postmessage");
183 if (!cbs) {
184 cbs = {};
185 pm.data("callbacks.postmessage", cbs);
186 }
187 var r = pm._random();
188 cbs[r] = fn;
189 return r;
190 },
191
192 _bind: function() {
193 // are we already listening to message events on this w?
194 if (!pm.data("listening.postmessage")) {
195 if (window.addEventListener) {
196 window.addEventListener("message", pm._dispatch, false);
197 }
198 else if (window.attachEvent) {
199 window.attachEvent("onmessage", pm._dispatch);
200 }
201 pm.data("listening.postmessage", 1);
202 }
203 },
204
205 _dispatch: function(e) {
206 //console.log("$.pm.dispatch", e, this);
207 try {
208 var msg = JSON.parse(e.data);
209 }
210 catch (ex) {
211 //console.warn("postmessage data invalid json: ", ex); //message wasn't meant for pm
212 return;
213 }
214 if (!msg.type) {
215 //console.warn("postmessage message type required"); //message wasn't meant for pm
216 return;
217 }
218 var cbs = pm.data("callbacks.postmessage") || {},
219 cb = cbs[msg.type];
220 if (cb) {
221 cb(msg.data);
222 }
223 else {
224 var l = pm.data("listeners.postmessage") || {};
225 var fns = l[msg.type] || [];
226 for (var i=0,len=fns.length; i<len; i++) {
227 var o = fns[i];
228 if (o.origin && o.origin !== '*' && e.origin !== o.origin) {
229 console.warn("postmessage message origin mismatch", e.origin, o.origin);
230 if (msg.errback) {
231 // notify post message errback
232 var error = {
233 message: "postmessage origin mismatch",
234 origin: [e.origin, o.origin]
235 };
236 pm.send({target:e.source, data:error, type:msg.errback});
237 }
238 continue;
239 }
240
241 function sendReply ( data ) {
242 if (msg.callback) {
243 pm.send({target:e.source, data:data, type:msg.callback});
244 }
245 }
246
247 try {
248 if ( o.callback ) {
249 o.fn(msg.data, sendReply, e);
250 } else {
251 sendReply ( o.fn(msg.data, e) );
252 }
253 }
254 catch (ex) {
255 if (msg.errback) {
256 // notify post message errback
257 pm.send({target:e.source, data:ex, type:msg.errback});
258 } else {
259 throw ex;
260 }
261 }
262 };
263 }
264 }
265 };
266
267 // location hash polling
268 pm.hash = {
269
270 send: function(options, msg) {
271 //console.log("hash.send", target_window, options, msg);
272 var target_window = options.target,
273 target_url = options.url;
274 if (!target_url) {
275 console.warn("postmessage target window url is required");
276 return;
277 }
278 target_url = pm.hash._url(target_url);
279 var source_window,
280 source_url = pm.hash._url(window.location.href);
281 if (window == target_window.parent) {
282 source_window = "parent";
283 }
284 else {
285 try {
286 for (var i=0,len=parent.frames.length; i<len; i++) {
287 var f = parent.frames[i];
288 if (f == window) {
289 source_window = i;
290 break;
291 }
292 };
293 }
294 catch(ex) {
295 // Opera: security error trying to access parent.frames x-origin
296 // juse use window.name
297 source_window = window.name;
298 }
299 }
300 if (source_window == null) {
301 console.warn("postmessage windows must be direct parent/child windows and the child must be available through the parent window.frames list");
302 return;
303 }
304 var hashmessage = {
305 "x-requested-with": "postmessage",
306 source: {
307 name: source_window,
308 url: source_url
309 },
310 postmessage: msg
311 };
312 var hash_id = "#x-postmessage-id=" + pm._random();
313 target_window.location = target_url + hash_id + encodeURIComponent(JSON.stringify(hashmessage));
314 },
315
316 _regex: /^\#x\-postmessage\-id\=(\w{32})/,
317
318 _regex_len: "#x-postmessage-id=".length + 32,
319
320 _bind: function() {
321 // are we already listening to message events on this w?
322 if (!pm.data("polling.postmessage")) {
323 setInterval(function() {
324 var hash = "" + window.location.hash,
325 m = pm.hash._regex.exec(hash);
326 if (m) {
327 var id = m[1];
328 if (pm.hash._last !== id) {
329 pm.hash._last = id;
330 pm.hash._dispatch(hash.substring(pm.hash._regex_len));
331 }
332 }
333 }, $.pm.poll || 200);
334 pm.data("polling.postmessage", 1);
335 }
336 },
337
338 _dispatch: function(hash) {
339 if (!hash) {
340 return;
341 }
342 try {
343 hash = JSON.parse(decodeURIComponent(hash));
344 if (!(hash['x-requested-with'] === 'postmessage' &&
345 hash.source && hash.source.name != null && hash.source.url && hash.postmessage)) {
346 // ignore since hash could've come from somewhere else
347 return;
348 }
349 }
350 catch (ex) {
351 // ignore since hash could've come from somewhere else
352 return;
353 }
354 var msg = hash.postmessage,
355 cbs = pm.data("callbacks.postmessage") || {},
356 cb = cbs[msg.type];
357 if (cb) {
358 cb(msg.data);
359 }
360 else {
361 var source_window;
362 if (hash.source.name === "parent") {
363 source_window = window.parent;
364 }
365 else {
366 source_window = window.frames[hash.source.name];
367 }
368 var l = pm.data("listeners.postmessage") || {};
369 var fns = l[msg.type] || [];
370 for (var i=0,len=fns.length; i<len; i++) {
371 var o = fns[i];
372 if (o.origin) {
373 var origin = /https?\:\/\/[^\/]*/.exec(hash.source.url)[0];
374 if (o.origin !== '*' && origin !== o.origin) {
375 console.warn("postmessage message origin mismatch", origin, o.origin);
376 if (msg.errback) {
377 // notify post message errback
378 var error = {
379 message: "postmessage origin mismatch",
380 origin: [origin, o.origin]
381 };
382 pm.send({target:source_window, data:error, type:msg.errback, hash:true, url:hash.source.url});
383 }
384 continue;
385 }
386 }
387
388 function sendReply ( data ) {
389 if (msg.callback) {
390 pm.send({target:source_window, data:data, type:msg.callback, hash:true, url:hash.source.url});
391 }
392 }
393
394 try {
395 if ( o.callback ) {
396 o.fn(msg.data, sendReply);
397 } else {
398 sendReply ( o.fn(msg.data) );
399 }
400 }
401 catch (ex) {
402 if (msg.errback) {
403 // notify post message errback
404 pm.send({target:source_window, data:ex, type:msg.errback, hash:true, url:hash.source.url});
405 } else {
406 throw ex;
407 }
408 }
409 };
410 }
411 },
412
413 _url: function(url) {
414 // url minus hash part
415 return (""+url).replace(/#.*$/, "");
416 }
417
418 };
419
420 $.extend(pm, {
421 defaults: {
422 target: null, /* target window (required) */
423 url: null, /* target window url (required if no window.postMessage or hash == true) */
424 type: null, /* message type (required) */
425 data: null, /* message data (required) */
426 success: null, /* success callback (optional) */
427 error: null, /* error callback (optional) */
428 origin: "*", /* postmessage origin (optional) */
429 hash: false /* use location hash for message passing (optional) */
430 }
431 });
432
433 })(this, typeof jQuery === "undefined" ? NO_JQUERY : jQuery);
434