Seryiza / Shikimori: Video name to the top

// ==UserScript==
// @name         Shikimori: Video name to the top
// @namespace    http://seryiza.ru/
// @updateURL    https://openuserjs.org/meta/Seryiza/Shikimori_Video_name_to_the_top.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/Seryiza/Shikimori_Video_name_to_the_top.user.js
// @version      1.4
// @description  На странице просмотра перемещает пункт выбранного видео на первую позицию списка
// @author       Seryiza
// @copyright    2018, Seryiza (https://openuserjs.org/users/Seryiza)
// @license      MIT
// @match        *://play.shikimori.org/*/video_online/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // script start
  onReady(moveUp);

  // print error message
  function error(msg) {
    var prompt = "[video name to the top] ";
    console.error(prompt + msg);
  }

  // main logic :p
  function moveUp() {
    var currentVariantTab = document.querySelector(".video-variant-group.active");
    if (currentVariantTab === null) {
      error("Video variant group wasn't found");
      return;
    }

    var currentVideoElem = currentVariantTab.querySelector(".b-video_variant.active");
    if (currentVideoElem === null) {
      error("Active video variant wasn't found");
      return;
    }

    currentVariantTab.insertBefore(currentVideoElem, currentVariantTab.children[0]);
  }

  // helper function to bind events
  function onReady(func) {
    var pageHasLoaded = (document.readyState === "complete" ||
      document.readyState === "loaded" ||
      document.readyState === "interactive");

    if (pageHasLoaded) {
      func();
    }
    else {
      document.addEventListener('DOMContentLoaded', func);
    }

    // update if it goes by next/prev buttons (<a> links)
    var observer = new MutationObserver(func);
    observer.observe(document.querySelector("html"), {
      childList: true
    });
  }
})();