You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

122 lines
3.0 KiB

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