|
1 /** |
|
2 * WordPress inline HTML embed |
|
3 * |
|
4 * @since 4.4.0 |
|
5 * |
|
6 * This file cannot have ampersands in it. This is to ensure |
|
7 * it can be embedded in older versions of WordPress. |
|
8 * See https://core.trac.wordpress.org/changeset/35708. |
|
9 */ |
|
10 (function ( window, document ) { |
|
11 'use strict'; |
|
12 |
|
13 var supportedBrowser = false, |
|
14 loaded = false; |
|
15 |
|
16 if ( document.querySelector ) { |
|
17 if ( window.addEventListener ) { |
|
18 supportedBrowser = true; |
|
19 } |
|
20 } |
|
21 |
|
22 /** @namespace wp */ |
|
23 window.wp = window.wp || {}; |
|
24 |
|
25 if ( !! window.wp.receiveEmbedMessage ) { |
|
26 return; |
|
27 } |
|
28 |
|
29 window.wp.receiveEmbedMessage = function( e ) { |
|
30 var data = e.data; |
|
31 if ( ! ( data.secret || data.message || data.value ) ) { |
|
32 return; |
|
33 } |
|
34 |
|
35 if ( /[^a-zA-Z0-9]/.test( data.secret ) ) { |
|
36 return; |
|
37 } |
|
38 |
|
39 var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ), |
|
40 blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ), |
|
41 i, source, height, sourceURL, targetURL; |
|
42 |
|
43 for ( i = 0; i < blockquotes.length; i++ ) { |
|
44 blockquotes[ i ].style.display = 'none'; |
|
45 } |
|
46 |
|
47 for ( i = 0; i < iframes.length; i++ ) { |
|
48 source = iframes[ i ]; |
|
49 |
|
50 if ( e.source !== source.contentWindow ) { |
|
51 continue; |
|
52 } |
|
53 |
|
54 source.removeAttribute( 'style' ); |
|
55 |
|
56 /* Resize the iframe on request. */ |
|
57 if ( 'height' === data.message ) { |
|
58 height = parseInt( data.value, 10 ); |
|
59 if ( height > 1000 ) { |
|
60 height = 1000; |
|
61 } else if ( ~~height < 200 ) { |
|
62 height = 200; |
|
63 } |
|
64 |
|
65 source.height = height; |
|
66 } |
|
67 |
|
68 /* Link to a specific URL on request. */ |
|
69 if ( 'link' === data.message ) { |
|
70 sourceURL = document.createElement( 'a' ); |
|
71 targetURL = document.createElement( 'a' ); |
|
72 |
|
73 sourceURL.href = source.getAttribute( 'src' ); |
|
74 targetURL.href = data.value; |
|
75 |
|
76 /* Only continue if link hostname matches iframe's hostname. */ |
|
77 if ( targetURL.host === sourceURL.host ) { |
|
78 if ( document.activeElement === source ) { |
|
79 window.top.location.href = data.value; |
|
80 } |
|
81 } |
|
82 } |
|
83 } |
|
84 }; |
|
85 |
|
86 function onLoad() { |
|
87 if ( loaded ) { |
|
88 return; |
|
89 } |
|
90 |
|
91 loaded = true; |
|
92 |
|
93 var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ), |
|
94 isIE11 = !!navigator.userAgent.match( /Trident.*rv:11\./ ), |
|
95 iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ), |
|
96 iframeClone, i, source, secret; |
|
97 |
|
98 for ( i = 0; i < iframes.length; i++ ) { |
|
99 source = iframes[ i ]; |
|
100 |
|
101 if ( ! source.getAttribute( 'data-secret' ) ) { |
|
102 /* Add secret to iframe */ |
|
103 secret = Math.random().toString( 36 ).substr( 2, 10 ); |
|
104 source.src += '#?secret=' + secret; |
|
105 source.setAttribute( 'data-secret', secret ); |
|
106 } |
|
107 |
|
108 /* Remove security attribute from iframes in IE10 and IE11. */ |
|
109 if ( ( isIE10 || isIE11 ) ) { |
|
110 iframeClone = source.cloneNode( true ); |
|
111 iframeClone.removeAttribute( 'security' ); |
|
112 source.parentNode.replaceChild( iframeClone, source ); |
|
113 } |
|
114 } |
|
115 } |
|
116 |
|
117 if ( supportedBrowser ) { |
|
118 window.addEventListener( 'message', window.wp.receiveEmbedMessage, false ); |
|
119 document.addEventListener( 'DOMContentLoaded', onLoad, false ); |
|
120 window.addEventListener( 'load', onLoad, false ); |
|
121 } |
|
122 })( window, document ); |