2022-05-04 03:20:44 +02:00
|
|
|
// @ts-check
|
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
const allowedPrefixes = (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.dataset.allowedPrefixes) ? document.currentScript.dataset.allowedPrefixes.split(' ') : [];
|
|
|
|
|
2022-12-15 16:18:59 +01:00
|
|
|
(function () {
|
2017-09-09 16:23:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
2022-05-04 03:20:44 +02:00
|
|
|
/**
|
|
|
|
* @param {() => void} loaded
|
|
|
|
*/
|
2022-12-15 16:18:59 +01:00
|
|
|
var ready = function (loaded) {
|
|
|
|
if (document.readyState === 'complete') {
|
2017-09-09 16:23:44 +02:00
|
|
|
loaded();
|
|
|
|
} else {
|
2022-12-15 16:18:59 +01:00
|
|
|
document.addEventListener('readystatechange', function () {
|
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
loaded();
|
|
|
|
}
|
|
|
|
});
|
2017-09-09 16:23:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
/**
|
|
|
|
* @param {Map} map
|
|
|
|
*/
|
|
|
|
var generateId = function (map) {
|
|
|
|
var id = 0, failCount = 0, idBuffer = new Uint32Array(1);
|
|
|
|
|
|
|
|
while (id === 0 || map.has(id)) {
|
|
|
|
id = crypto.getRandomValues(idBuffer)[0];
|
|
|
|
failCount++;
|
|
|
|
|
|
|
|
if (failCount > 100) {
|
|
|
|
// give up and assign (easily guessable) unique number if getRandomValues is broken or no luck
|
|
|
|
id = -(map.size + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2022-12-15 16:18:59 +01:00
|
|
|
ready(function () {
|
2024-09-12 11:41:19 +02:00
|
|
|
/** @type {Map<number, HTMLQuoteElement | HTMLIFrameElement>} */
|
|
|
|
var embeds = new Map();
|
2017-09-09 16:23:44 +02:00
|
|
|
|
2022-12-15 16:18:59 +01:00
|
|
|
window.addEventListener('message', function (e) {
|
2017-09-09 16:23:44 +02:00
|
|
|
var data = e.data || {};
|
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
if (typeof data !== 'object' || data.type !== 'setHeight' || !embeds.has(data.id)) {
|
2022-05-04 03:20:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
var embed = embeds.get(data.id);
|
2024-02-27 16:42:05 +01:00
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
if (embed instanceof HTMLIFrameElement) {
|
|
|
|
embed.height = data.height;
|
2017-09-09 16:23:44 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
if (embed instanceof HTMLQuoteElement) {
|
|
|
|
var iframe = embed.querySelector('iframe');
|
2017-09-09 16:23:44 +02:00
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
if (!iframe || ('source' in e && iframe.contentWindow !== e.source)) {
|
|
|
|
return;
|
2022-05-04 03:20:44 +02:00
|
|
|
}
|
2024-09-12 11:41:19 +02:00
|
|
|
|
|
|
|
iframe.height = data.height;
|
|
|
|
|
|
|
|
var placeholder = embed.querySelector('a');
|
|
|
|
|
|
|
|
if (!placeholder) return;
|
|
|
|
|
|
|
|
embed.removeChild(placeholder);
|
2022-05-04 03:20:44 +02:00
|
|
|
}
|
2024-09-12 11:41:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Legacy embeds
|
|
|
|
document.querySelectorAll('iframe.mastodon-embed').forEach(iframe => {
|
|
|
|
var id = generateId(embeds);
|
|
|
|
|
|
|
|
embeds.set(id, iframe);
|
|
|
|
|
|
|
|
iframe.allow = 'fullscreen';
|
2024-09-30 10:20:20 +02:00
|
|
|
iframe.sandbox = 'allow-scripts allow-same-origin allow-popups';
|
2024-09-12 11:41:19 +02:00
|
|
|
iframe.style.border = 0;
|
|
|
|
iframe.style.overflow = 'hidden';
|
|
|
|
iframe.style.display = 'block';
|
|
|
|
|
|
|
|
iframe.onload = function () {
|
|
|
|
iframe.contentWindow.postMessage({
|
|
|
|
type: 'setHeight',
|
|
|
|
id: id,
|
|
|
|
}, '*');
|
|
|
|
};
|
|
|
|
|
|
|
|
iframe.onload(); // In case the script is executing after the iframe has already loaded
|
|
|
|
});
|
|
|
|
|
|
|
|
// New generation of embeds
|
|
|
|
document.querySelectorAll('blockquote.mastodon-embed').forEach(container => {
|
|
|
|
var id = generateId(embeds);
|
|
|
|
|
|
|
|
embeds.set(id, container);
|
|
|
|
|
|
|
|
var iframe = document.createElement('iframe');
|
|
|
|
var embedUrl = new URL(container.getAttribute('data-embed-url'));
|
2017-09-09 16:23:44 +02:00
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
if (embedUrl.protocol !== 'https:' && embedUrl.protocol !== 'http:') return;
|
|
|
|
if (allowedPrefixes.every((allowedPrefix) => !embedUrl.toString().startsWith(allowedPrefix))) return;
|
2017-09-09 16:23:44 +02:00
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
iframe.src = embedUrl.toString();
|
|
|
|
iframe.width = container.clientWidth;
|
|
|
|
iframe.height = 0;
|
|
|
|
iframe.allow = 'fullscreen';
|
2024-09-30 10:20:20 +02:00
|
|
|
iframe.sandbox = 'allow-scripts allow-same-origin allow-popups';
|
2024-09-12 11:41:19 +02:00
|
|
|
iframe.style.border = 0;
|
2022-05-04 03:20:44 +02:00
|
|
|
iframe.style.overflow = 'hidden';
|
2024-09-12 11:41:19 +02:00
|
|
|
iframe.style.display = 'block';
|
2017-09-09 16:23:44 +02:00
|
|
|
|
2022-12-15 16:18:59 +01:00
|
|
|
iframe.onload = function () {
|
2017-09-09 16:23:44 +02:00
|
|
|
iframe.contentWindow.postMessage({
|
|
|
|
type: 'setHeight',
|
|
|
|
id: id,
|
|
|
|
}, '*');
|
|
|
|
};
|
|
|
|
|
2024-09-12 11:41:19 +02:00
|
|
|
container.appendChild(iframe);
|
2017-09-09 16:23:44 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
})();
|