Files
mastodon/public/embed.js
T
jcoffey-dev 319ed76dbd
Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Import Mastodon v4.7.3
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from
https://github.com/mastodon/mastodon, unmodified.
2026-10-05 22:03:13 -07: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(' ')
: [],
);