blocks
5 years ago
build
5 years ago
fonts
8 years ago
genericons
6 years ago
lib
5 years ago
social-logos
5 years ago
accessible-focus.js
5 years ago
class.jetpack-provision.php
6 years ago
connect-button.js
5 years ago
crowdsignal-shortcode.js
5 years ago
crowdsignal-survey.js
5 years ago
facebook-embed.js
5 years ago
footer.php
7 years ago
gallery-settings.js
5 years ago
genericons.php
11 years ago
header.php
7 years ago
idc-notice.js
5 years ago
jetpack-admin.js
5 years ago
jetpack-connection-banner.js
5 years ago
jetpack-deactivate-dialog.js
5 years ago
jetpack-jitm.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-server-sandbox.php
6 years ago
jetpack-wizard-banner.js
5 years ago
jquery.jetpack-resize.js
5 years ago
polldaddy-shortcode.js
5 years ago
postmessage.js
8 years ago
social-logos.php
6 years ago
twitter-timeline.js
5 years ago
postmessage.js
439 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 | |
| 435 | /** |
| 436 | * http://www.JSON.org/json2.js |
| 437 | **/ |
| 438 | if (! ("JSON" in window && window.JSON)){JSON={}}(function(){function f(n){return n<10?"0"+n:n}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(key){return this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z"};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+string+'"'}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==="object"&&typeof value.toJSON==="function"){value=value.toJSON(key)}if(typeof rep==="function"){value=rep.call(holder,key,value)}switch(typeof value){case"string":return quote(value);case"number":return isFinite(value)?String(value):"null";case"boolean":case"null":return String(value);case"object":if(!value){return"null"}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==="[object Array]"){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||"null"}v=partial.length===0?"[]":gap?"[\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"]":"["+partial.join(",")+"]";gap=mind;return v}if(rep&&typeof rep==="object"){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==="string"){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?": ":":")+v)}}}}v=partial.length===0?"{}":gap?"{\n"+gap+partial.join(",\n"+gap)+"\n"+mind+"}":"{"+partial.join(",")+"}";gap=mind;return v}}if(typeof JSON.stringify!=="function"){JSON.stringify=function(value,replacer,space){var i;gap="";indent="";if(typeof space==="number"){for(i=0;i<space;i+=1){indent+=" "}}else{if(typeof space==="string"){indent=space}}rep=replacer;if(replacer&&typeof replacer!=="function"&&(typeof replacer!=="object"||typeof replacer.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":value})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==="object"){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v}else{delete value[k]}}}}return reviver.call(holder,key,value)}cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}}()); |
| 439 |