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.

118 lines
3.1 KiB

1 year ago
  1. /**
  2. * WordPress inline HTML embed
  3. *
  4. * @since 4.4.0
  5. * @output wp-includes/js/wp-embed.js
  6. *
  7. * Single line comments should not be used since they will break
  8. * the script when inlined in get_post_embed_html(), specifically
  9. * when the comments are not stripped out due to SCRIPT_DEBUG
  10. * being turned on.
  11. */
  12. (function ( window, document ) {
  13. 'use strict';
  14. /* Abort for ancient browsers. */
  15. if ( ! document.querySelector || ! window.addEventListener || typeof URL === 'undefined' ) {
  16. return;
  17. }
  18. /** @namespace wp */
  19. window.wp = window.wp || {};
  20. /* Abort if script was already executed. */
  21. if ( !! window.wp.receiveEmbedMessage ) {
  22. return;
  23. }
  24. /**
  25. * Receive embed message.
  26. *
  27. * @param {MessageEvent} e
  28. */
  29. window.wp.receiveEmbedMessage = function( e ) {
  30. var data = e.data;
  31. /* Verify shape of message. */
  32. if (
  33. ! ( data || data.secret || data.message || data.value ) ||
  34. /[^a-zA-Z0-9]/.test( data.secret )
  35. ) {
  36. return;
  37. }
  38. var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ),
  39. blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ),
  40. allowedProtocols = new RegExp( '^https?:$', 'i' ),
  41. i, source, height, sourceURL, targetURL;
  42. for ( i = 0; i < blockquotes.length; i++ ) {
  43. blockquotes[ i ].style.display = 'none';
  44. }
  45. for ( i = 0; i < iframes.length; i++ ) {
  46. source = iframes[ i ];
  47. if ( e.source !== source.contentWindow ) {
  48. continue;
  49. }
  50. source.removeAttribute( 'style' );
  51. if ( 'height' === data.message ) {
  52. /* Resize the iframe on request. */
  53. height = parseInt( data.value, 10 );
  54. if ( height > 1000 ) {
  55. height = 1000;
  56. } else if ( ~~height < 200 ) {
  57. height = 200;
  58. }
  59. source.height = height;
  60. } else if ( 'link' === data.message ) {
  61. /* Link to a specific URL on request. */
  62. sourceURL = new URL( source.getAttribute( 'src' ) );
  63. targetURL = new URL( data.value );
  64. if (
  65. allowedProtocols.test( targetURL.protocol ) &&
  66. targetURL.host === sourceURL.host &&
  67. document.activeElement === source
  68. ) {
  69. window.top.location.href = data.value;
  70. }
  71. }
  72. }
  73. };
  74. function onLoad() {
  75. var iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ),
  76. i, source, secret;
  77. for ( i = 0; i < iframes.length; i++ ) {
  78. /** @var {IframeElement} */
  79. source = iframes[ i ];
  80. secret = source.getAttribute( 'data-secret' );
  81. if ( ! secret ) {
  82. /* Add secret to iframe */
  83. secret = Math.random().toString( 36 ).substring( 2, 12 );
  84. source.src += '#?secret=' + secret;
  85. source.setAttribute( 'data-secret', secret );
  86. }
  87. /*
  88. * Let post embed window know that the parent is ready for receiving the height message, in case the iframe
  89. * loaded before wp-embed.js was loaded. When the ready message is received by the post embed window, the
  90. * window will then (re-)send the height message right away.
  91. */
  92. source.contentWindow.postMessage( {
  93. message: 'ready',
  94. secret: secret
  95. }, '*' );
  96. }
  97. }
  98. window.addEventListener( 'message', window.wp.receiveEmbedMessage, false );
  99. document.addEventListener( 'DOMContentLoaded', onLoad, false );
  100. })( window, document );