wp-embed.js 3.1 KB

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