glitch-soc/app/javascript/core/embed.ts
Claire 1b06e4e1b7 Merge commit 'c76ae7a5c0d247264afa896f081db9d1fd278711' into glitch-soc/merge-upstream
Conflicts:
- `app/javascript/packs/public.jsx`:
  In glitch-soc, this file was split across the following files:
  - `app/javascript/packs/public.jsx`
  - `app/javascript/core/embed.js`
  - `app/javascript/core/settings.js`
  Update all those files accordingly, as well as the related `theme.yml` files.
2024-03-15 22:37:55 +01:00

42 lines
1,010 B
TypeScript

// This file will be loaded on embed pages, regardless of theme.
import 'packs/public-path';
import ready from '../mastodon/ready';
interface SetHeightMessage {
type: 'setHeight';
id: string;
height: number;
}
function isSetHeightMessage(data: unknown): data is SetHeightMessage {
if (
data &&
typeof data === 'object' &&
'type' in data &&
data.type === 'setHeight'
)
return true;
else return false;
}
window.addEventListener('message', (e) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- typings are not correct, it can be null in very rare cases
if (!e.data || !isSetHeightMessage(e.data) || !window.parent) return;
const data = e.data;
ready(() => {
window.parent.postMessage(
{
type: 'setHeight',
id: data.id,
height: document.getElementsByTagName('html')[0].scrollHeight,
},
'*',
);
}).catch((e) => {
console.error('Error in setHeightMessage postMessage', e);
});
});