wp-embed.js 3.1 KB

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