2022-05-04 02:20:44 +01:00
|
|
|
// @ts-check
|
|
|
|
|
2017-09-09 15:23:44 +01:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2022-05-04 02:20:44 +01:00
|
|
|
/**
|
|
|
|
* @param {() => void} loaded
|
|
|
|
*/
|
2017-09-09 15:23:44 +01:00
|
|
|
var ready = function(loaded) {
|
|
|
|
if (['interactive', 'complete'].indexOf(document.readyState) !== -1) {
|
|
|
|
loaded();
|
|
|
|
} else {
|
|
|
|
document.addEventListener('DOMContentLoaded', loaded);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ready(function() {
|
2022-05-04 02:20:44 +01:00
|
|
|
/** @type {Map<number, HTMLIFrameElement>} */
|
|
|
|
var iframes = new Map();
|
2017-09-09 15:23:44 +01:00
|
|
|
|
|
|
|
window.addEventListener('message', function(e) {
|
|
|
|
var data = e.data || {};
|
|
|
|
|
2022-05-04 02:20:44 +01:00
|
|
|
if (typeof data !== 'object' || data.type !== 'setHeight' || !iframes.has(data.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var iframe = iframes.get(data.id);
|
|
|
|
|
|
|
|
if ('source' in e && iframe.contentWindow !== e.source) {
|
2017-09-09 15:23:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-04 02:20:44 +01:00
|
|
|
iframe.height = data.height;
|
2017-09-09 15:23:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
[].forEach.call(document.querySelectorAll('iframe.mastodon-embed'), function(iframe) {
|
2022-05-04 02:20:44 +01: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 15:23:44 +01:00
|
|
|
|
2022-05-04 02:20:44 +01:00
|
|
|
iframes.set(id, iframe);
|
2017-09-09 15:23:44 +01:00
|
|
|
|
2022-05-04 02:20:44 +01:00
|
|
|
iframe.scrolling = 'no';
|
|
|
|
iframe.style.overflow = 'hidden';
|
2017-09-09 15:23:44 +01:00
|
|
|
|
|
|
|
iframe.onload = function() {
|
|
|
|
iframe.contentWindow.postMessage({
|
|
|
|
type: 'setHeight',
|
|
|
|
id: id,
|
|
|
|
}, '*');
|
|
|
|
};
|
|
|
|
|
|
|
|
iframe.onload();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})();
|