dbarison
4/23/2018 - 3:42 PM

iubenda.js.es6

let Iub = class Iubenda {
  static load() {
    if (typeof Turbolinks !== "undefined" && Turbolinks.supported) {
      return document.addEventListener("turbolinks:load", () => Iubenda.loadTag(), true);
    } else {
      return Iubenda.loadTag();
    }
  }

  static loadTag() {
    // Each time the page changes Iubenda adds a script tag of its code and an
    // empty style block. We delete these before running Iubenda again.
    for (let tag of document.getElementsByTagName("script")) {
      Iubenda.deleteTag(tag);
    }
    for (let style of document.getElementsByTagName("style")) {
      Iubenda.deleteStyle(style);
    }
    return Iubenda.writeTag();
  }

  static writeTag() {
    const s = document.createElement("script");
    s.src = Iubenda.url();
    const tag = document.getElementsByTagName("script")[0];
    return tag.parentNode.insertBefore(s, tag);
  }

  static deleteTag(tag) {
    if (typeof tag !== "undefined" && tag.src === Iubenda.url()) {
      return tag.parentNode.removeChild(tag);
    }
  }

  static deleteStyle(style) {
    if (style.innerHTML === "") {
      return style.parentNode.removeChild(style);
    }
  }

  static url() {
    return document.location.protocol + "//cdn.iubenda.com/iubenda.js";
  }
};


Iub.load();