// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0

if (!document.getElementsByClassName) {
    document.getElementsByClassName = function(className) {

        if (document.querySelectorAll)
            return document.querySelectorAll("." + className);

        var results = [];
        for (var i = 0; i < document.all.length; i++) {
            if (new RegExp("\\b" + className + "\\b").test(document.all[i].className))
                results.push(document.all[i]);
        }
        return results;
    }
}

if (!document.querySelector) {
    var style = document.createStyleSheet();

    // https://stackoverflow.com/a/53554212
    document.querySelector = function(query) {
        style.addRule(query, "foo:bar");
        for (var i = 0; i < document.all.length; i++) {
            if (document.all[i].currentStyle.foo === "bar")
                return document.all[i];
        }
        style.removeRule(0);
    }
}

if (!window.addEventListener) {
    window.addEventListener = function(event, callback) {
        window.attachEvent("on" + event, callback);
    }
}

if (!document.head) {
    document.head = document.getElementsByTagName("head")[0];
}

if (!window.HTMLPictureElement) {
    document.createElement("picture");
}

function toArray(iter) {
    var arr = [];
    for (var i = 0; i < iter.length; i++) arr.push(iter[i]);
    return arr;
}

// Fix images not updating on Opera 12.1 - 12.16 (earlier versions still broken)
// https://stackoverflow.com/a/71828058/8618686
if(window.opera && Object.defineProperty) {
    /* if browser is opera presto, updating image elements' sources will not upload the DOM visual.
    So we need to do some hacking. Only thing that works is to remove and reAppend the relevant node... */
    Object.defineProperty(HTMLImageElement.prototype, "src", {
        enumerable: true,
        configurable: true,
        get: function() {
            return this.getAttribute("src");
        },
        set: function(newSrc)
        {
            /*max-size confinement is required for presto if parent is display flex. Image will go out of its available size otherwise*/
            this.style.maxHeight = this.style.height;
            this.style.maxWidth = this.style.width;

            this.setAttribute("src", newSrc);

            /*we have to put this node back to exactly where we rip it from*/
            var parent = this.parentNode;
            if(this.nextElementSibling != null)
            {
                var reference = this.nextElementSibling;
                parent.removeChild(this);
                reference.insertAdjacentElement("beforebegin", this);
            }
            else if (parent)
            {
                parent.removeChild(this);
                parent.appendChild(this);
            }
        }
    });
}

// @license-end
