(() => {
  if (!(window.Options && localStorage && (document.querySelector ('div.post.op') != null))) { return; }

  const setloopsto = (root, loop) => {
    if (typeof setupVideosIn === "function") {
      // expand-video.js
      const label = loop ? '[loop]' : '[play once]'
      $(root).find ('p.fileinfo > span').filter ((k, e) => e.innerText === label).each ((k, e) => e.click ())
    } else {
      const flag = loop ? '1' : '0'
      Array.from (document.querySelectorAll ("div.file > a.file")).filter (e => e.hasAttribute ("href")).map (e => [e, e.getAttribute ("href").match (/^(\/player[.]php[?].+&loop=)([01])$/)]).filter (e_match => e_match [1] != null).forEach (e_match => e_match [0].setAttribute ("href", e_match [1] [1] + flag))
    }
  }

  const addgui = () => {
    const gui       = '<span>'
      + _('Loop:')
      + ' <label><input type="checkbox" id="videoloop-onoff">' + _('On') + '</label>'
      + ' <button id="videoloop-setnow">' + _('Set now') + '</button>'
      + '</span>';
    Options.extend_tab ("webm", gui)
    $("#videoloop-setnow").on("click", () => {
      setloopsto (document, document.getElementById ('videoloop-onoff').checked)
    })
  }

  addgui()
})()