wp-embed.js 3.6 KB

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