Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Embed js height fix (mastodon#22141)
Browse files Browse the repository at this point in the history
* only begin iframe reheight once document state is complete

* format

* lint fixes

* Update public/embed.js to use readystatechange event listener

Co-authored-by: Claire <[email protected]>

* Call loaded() if ready, otherwise add listenter

* lint fix

Co-authored-by: Claire <[email protected]>
  • Loading branch information
hodgesmr and ClearlyClaire authored Dec 15, 2022
1 parent 04c611d commit 4114a70
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions public/embed.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
// @ts-check

(function() {
(function () {
'use strict';

/**
* @param {() => void} loaded
*/
var ready = function(loaded) {
if (['interactive', 'complete'].indexOf(document.readyState) !== -1) {
var ready = function (loaded) {
if (document.readyState === 'complete') {
loaded();
} else {
document.addEventListener('DOMContentLoaded', loaded);
document.addEventListener('readystatechange', function () {
if (document.readyState === 'complete') {
loaded();
}
});
}
};

ready(function() {
ready(function () {
/** @type {Map<number, HTMLIFrameElement>} */
var iframes = new Map();

window.addEventListener('message', function(e) {
window.addEventListener('message', function (e) {
var data = e.data || {};

if (typeof data !== 'object' || data.type !== 'setHeight' || !iframes.has(data.id)) {
Expand All @@ -34,7 +38,7 @@
iframe.height = data.height;
});

[].forEach.call(document.querySelectorAll('iframe.mastodon-embed'), function(iframe) {
[].forEach.call(document.querySelectorAll('iframe.mastodon-embed'), function (iframe) {
// select unique id for each iframe
var id = 0, failCount = 0, idBuffer = new Uint32Array(1);
while (id === 0 || iframes.has(id)) {
Expand All @@ -49,10 +53,10 @@

iframes.set(id, iframe);

iframe.scrolling = 'no';
iframe.scrolling = 'no';
iframe.style.overflow = 'hidden';

iframe.onload = function() {
iframe.onload = function () {
iframe.contentWindow.postMessage({
type: 'setHeight',
id: id,
Expand Down

0 comments on commit 4114a70

Please sign in to comment.