about summary refs log tree commit diff
path: root/public/embed.js
blob: dac50745376c2742b0fc1888180aedd01ecfc2de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(function() {
  'use strict';

  var ready = function(loaded) {
    if (['interactive', 'complete'].indexOf(document.readyState) !== -1) {
      loaded();
    } else {
      document.addEventListener('DOMContentLoaded', loaded);
    }
  };

  ready(function() {
    var iframes = [];

    window.addEventListener('message', function(e) {
      var data = e.data || {};

      if (data.type !== 'setHeight' || !iframes[data.id]) {
        return;
      }

      iframes[data.id].height = data.height;
    });

    [].forEach.call(document.querySelectorAll('iframe.mastodon-embed'), function(iframe) {
      iframe.scrolling      = 'no';
      iframe.style.overflow = 'hidden';

      iframes.push(iframe);

      var id = iframes.length - 1;

      iframe.onload = function() {
        iframe.contentWindow.postMessage({
          type: 'setHeight',
          id: id,
        }, '*');
      };

      iframe.onload();
    });
  });
})();