mastodon/public/embed.js
Renaud Chaput e24737c612
Some checks failed
Check i18n / check-i18n (push) Waiting to run
Chromatic / Check for relevant changes (push) Waiting to run
Chromatic / Run Chromatic (push) Blocked by required conditions
CodeQL / Analyze (actions) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (ruby) (push) Waiting to run
Crowdin / Upload translations / upload-translations (push) Waiting to run
Check formatting / lint (push) Waiting to run
CSS Linting / lint (push) Waiting to run
JavaScript Linting / lint (push) Waiting to run
Ruby Linting / lint (push) Waiting to run
JavaScript Testing / test (push) Waiting to run
Historical data migration test / test (14-alpine) (push) Waiting to run
Historical data migration test / test (15-alpine) (push) Waiting to run
Historical data migration test / test (16-alpine) (push) Waiting to run
Historical data migration test / test (17-alpine) (push) Waiting to run
Ruby Testing / build (production) (push) Waiting to run
Ruby Testing / build (test) (push) Waiting to run
Ruby Testing / test (.ruby-version) (push) Blocked by required conditions
Ruby Testing / test (3.2) (push) Blocked by required conditions
Ruby Testing / test (3.3) (push) Blocked by required conditions
Ruby Testing / End to End testing (.ruby-version) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.2) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.3) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.10.2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.2, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Bundler Audit / security (push) Has been cancelled
Haml Linting / lint (push) Has been cancelled
Replace prettier with oxfmt (#37893)
2026-02-19 18:04:25 +00:00

154 lines
3.7 KiB
JavaScript

// @ts-check
(function (allowedPrefixes) {
'use strict';
/**
* @param {() => void} loaded
*/
var ready = function (loaded) {
if (document.readyState === 'complete') {
loaded();
} else {
document.addEventListener('readystatechange', function () {
if (document.readyState === 'complete') {
loaded();
}
});
}
};
/**
* @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;
};
ready(function () {
/** @type {Map<number, HTMLQuoteElement | HTMLIFrameElement>} */
var embeds = new Map();
window.addEventListener('message', function (e) {
var data = e.data || {};
if (
typeof data !== 'object' ||
data.type !== 'setHeight' ||
!embeds.has(data.id)
) {
return;
}
var embed = embeds.get(data.id);
if (embed instanceof HTMLIFrameElement) {
embed.height = data.height;
}
if (embed instanceof HTMLQuoteElement) {
var iframe = embed.querySelector('iframe');
if (!iframe || ('source' in e && iframe.contentWindow !== e.source)) {
return;
}
iframe.height = data.height;
var placeholder = embed.querySelector('a');
if (!placeholder) return;
embed.removeChild(placeholder);
}
});
// Legacy embeds
document.querySelectorAll('iframe.mastodon-embed').forEach((iframe) => {
var id = generateId(embeds);
embeds.set(id, iframe);
iframe.allow = 'fullscreen';
iframe.sandbox = 'allow-scripts allow-same-origin allow-popups';
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'));
if (embedUrl.protocol !== 'https:' && embedUrl.protocol !== 'http:')
return;
if (
allowedPrefixes.every(
(allowedPrefix) => !embedUrl.toString().startsWith(allowedPrefix),
)
)
return;
iframe.src = embedUrl.toString();
iframe.width = container.clientWidth;
iframe.height = 0;
iframe.allow = 'fullscreen';
iframe.sandbox = 'allow-scripts allow-same-origin allow-popups';
iframe.style.border = 0;
iframe.style.overflow = 'hidden';
iframe.style.display = 'block';
iframe.onload = function () {
iframe.contentWindow.postMessage(
{
type: 'setHeight',
id: id,
},
'*',
);
};
container.appendChild(iframe);
});
});
})(
document.currentScript &&
document.currentScript.tagName.toUpperCase() === 'SCRIPT' &&
document.currentScript.dataset.allowedPrefixes
? document.currentScript.dataset.allowedPrefixes.split(' ')
: [],
);