embedResize.js 887 B

123456789101112131415161718192021222324252627
  1. (function() {
  2. var embedElement = document.querySelector('.embed-wrapper, .tweet-embed, .embed-video');
  3. if (!embedElement) return;
  4. var lastHeight = 0;
  5. function sendHeight() {
  6. var currentHeight = embedElement.offsetHeight;
  7. if (currentHeight !== lastHeight && currentHeight > 0) {
  8. lastHeight = currentHeight;
  9. window.parent.postMessage(['resizeIframe', { h: currentHeight, url: location.href }], '*');
  10. }
  11. }
  12. // Respond to height requests from parent via MessageChannel
  13. window.addEventListener('message', function(event) {
  14. if (event.source === window.parent && event.ports && event.ports[0]) {
  15. event.ports[0].postMessage(embedElement.offsetHeight);
  16. }
  17. });
  18. window.addEventListener('load', sendHeight);
  19. new ResizeObserver(sendHeight).observe(embedElement);
  20. // Expose for embedTweet.js
  21. window._nitterSendHeight = sendHeight;
  22. })();