wp-embed.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. var supportedBrowser = false,
  13. loaded = false;
  14. if ( document.querySelector ) {
  15. if ( window.addEventListener ) {
  16. supportedBrowser = true;
  17. }
  18. }
  19. /** @namespace wp */
  20. window.wp = window.wp || {};
  21. if ( !! window.wp.receiveEmbedMessage ) {
  22. return;
  23. }
  24. window.wp.receiveEmbedMessage = function( e ) {
  25. var data = e.data;
  26. if ( ! data ) {
  27. return;
  28. }
  29. if ( ! ( data.secret || data.message || data.value ) ) {
  30. return;
  31. }
  32. if ( /[^a-zA-Z0-9]/.test( data.secret ) ) {
  33. return;
  34. }
  35. var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ),
  36. blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ),
  37. i, source, height, sourceURL, targetURL;
  38. for ( i = 0; i < blockquotes.length; i++ ) {
  39. blockquotes[ i ].style.display = 'none';
  40. }
  41. for ( i = 0; i < iframes.length; i++ ) {
  42. source = iframes[ i ];
  43. if ( e.source !== source.contentWindow ) {
  44. continue;
  45. }
  46. source.removeAttribute( 'style' );
  47. /* Resize the iframe on request. */
  48. if ( 'height' === data.message ) {
  49. height = parseInt( data.value, 10 );
  50. if ( height > 1000 ) {
  51. height = 1000;
  52. } else if ( ~~height < 200 ) {
  53. height = 200;
  54. }
  55. source.height = height;
  56. }
  57. /* Link to a specific URL on request. */
  58. if ( 'link' === data.message ) {
  59. sourceURL = document.createElement( 'a' );
  60. targetURL = document.createElement( 'a' );
  61. sourceURL.href = source.getAttribute( 'src' );
  62. targetURL.href = data.value;
  63. /* Only continue if link hostname matches iframe's hostname. */
  64. if ( targetURL.host === sourceURL.host ) {
  65. if ( document.activeElement === source ) {
  66. window.top.location.href = data.value;
  67. }
  68. }
  69. }
  70. }
  71. };
  72. function onLoad() {
  73. if ( loaded ) {
  74. return;
  75. }
  76. loaded = true;
  77. var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ),
  78. isIE11 = !!navigator.userAgent.match( /Trident.*rv:11\./ ),
  79. iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ),
  80. iframeClone, i, source, secret;
  81. for ( i = 0; i < iframes.length; i++ ) {
  82. source = iframes[ i ];
  83. if ( ! source.getAttribute( 'data-secret' ) ) {
  84. /* Add secret to iframe */
  85. secret = Math.random().toString( 36 ).substr( 2, 10 );
  86. source.src += '#?secret=' + secret;
  87. source.setAttribute( 'data-secret', secret );
  88. }
  89. /* Remove security attribute from iframes in IE10 and IE11. */
  90. if ( ( isIE10 || isIE11 ) ) {
  91. iframeClone = source.cloneNode( true );
  92. iframeClone.removeAttribute( 'security' );
  93. source.parentNode.replaceChild( iframeClone, source );
  94. }
  95. }
  96. }
  97. if ( supportedBrowser ) {
  98. window.addEventListener( 'message', window.wp.receiveEmbedMessage, false );
  99. document.addEventListener( 'DOMContentLoaded', onLoad, false );
  100. window.addEventListener( 'load', onLoad, false );
  101. }
  102. })( window, document );