2022-05-04 10:20:44 +09:00
|
|
|
// @ts-check
|
|
|
|
|
2022-12-16 00:18:59 +09:00
|
|
|
(function () {
|
2017-09-09 23:23:44 +09:00
|
|
|
'use strict';
|
|
|
|
|
2022-05-04 10:20:44 +09:00
|
|
|
/**
|
|
|
|
* @param {() => void} loaded
|
|
|
|
*/
|
2022-12-16 00:18:59 +09:00
|
|
|
var ready = function (loaded) {
|
|
|
|
if (document.readyState === 'complete') {
|
2017-09-09 23:23:44 +09:00
|
|
|
loaded();
|
|
|
|
} else {
|
2022-12-16 00:18:59 +09:00
|
|
|
document.addEventListener('readystatechange', function () {
|
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
loaded();
|
|
|
|
}
|
|
|
|
});
|
2017-09-09 23:23:44 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-16 00:18:59 +09:00
|
|
|
ready(function () {
|
2022-05-04 10:20:44 +09:00
|
|
|
/** @type {Map<number, HTMLIFrameElement>} */
|
|
|
|
var iframes = new Map();
|
2017-09-09 23:23:44 +09:00
|
|
|
|
2022-12-16 00:18:59 +09:00
|
|
|
window.addEventListener('message', function (e) {
|
2017-09-09 23:23:44 +09:00
|
|
|
var data = e.data || {};
|
|
|
|
|
2022-05-04 10:20:44 +09:00
|
|
|
if (typeof data !== 'object' || data.type !== 'setHeight' || !iframes.has(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var iframe = iframes.get(data.id);
|
|
|
|
|
2024-02-28 00:42:05 +09:00
|
|
|
if(!iframe) return;
|
|
|
|
|
2022-05-04 10:20:44 +09:00
|
|
|
if ('source' in e && iframe.contentWindow !== e.source) {
|
2017-09-09 23:23:44 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-04 10:20:44 +09:00
|
|
|
iframe.height = data.height;
|
2017-09-09 23:23:44 +09:00
|
|
|
});
|
|
|
|
|
2024-02-28 00:42:05 +09:00
|
|
|
document.querySelectorAll('iframe.mastodon-embed').forEach(iframe => {
|
2022-05-04 10:20:44 +09:00
|
|
|
// select unique id for each iframe
|
|
|
|
var id = 0, failCount = 0, idBuffer = new Uint32Array(1);
|
|
|
|
while (id === 0 || iframes.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 = -(iframes.size + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 23:23:44 +09:00
|
|
|
|
2022-05-04 10:20:44 +09:00
|
|
|
iframes.set(id, iframe);
|
2017-09-09 23:23:44 +09:00
|
|
|
|
2022-12-16 00:18:59 +09:00
|
|
|
iframe.scrolling = 'no';
|
2022-05-04 10:20:44 +09:00
|
|
|
iframe.style.overflow = 'hidden';
|
2017-09-09 23:23:44 +09:00
|
|
|
|
2022-12-16 00:18:59 +09:00
|
|
|
iframe.onload = function () {
|
2017-09-09 23:23:44 +09:00
|
|
|
iframe.contentWindow.postMessage({
|
|
|
|
type: 'setHeight',
|
|
|
|
id: id,
|
|
|
|
}, '*');
|
|
|
|
};
|
|
|
|
|
|
|
|
iframe.onload();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})();
|