aluizbsilva / Empornium Info Bar

// ==UserScript==
// @name        Empornium Info Bar
// @namespace   http://localhost
// @include     http://torrents.empornium.me/torrents.php*
// @version     1
// @grant       none
// ==/UserScript==


var top_info = document.querySelector('#top_info');
var top_infoPosition = top_info.getBoundingClientRect();
var placeholder = document.createElement('div');
placeholder.style.width = top_infoPosition.width + 'px';
placeholder.style.height = top_infoPosition.height + 'px';
var isAdded = false;

addGlobalStyle('.sticky { top: -12px; position: fixed; left: 50%; transform: translateX(-50%); white-space: nowrap; z-index: 1002; }');

window.addEventListener('scroll', function() {
    if (window.pageYOffset >= 219 && !isAdded) {
        top_info.classList.add('sticky');
        top_info.parentNode.insertBefore(placeholder, top_info);
        isAdded = true;
    }
    else if (window.pageYOffset < 219 && isAdded) {
        top_info.classList.remove('sticky');
        top_info.parentNode.removeChild(placeholder);
        isAdded = false;
    }
});

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}