Haxorsnake / New Osu! Nav Search

// ==UserScript==
// @name         New Osu! Nav Search
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try  take over the world!
// @author       Haxorsnake
// @match        https://osu.ppy.sh/*
// @grant        none
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    var open = window.XMLHttpRequest.prototype.open,
        send = window.XMLHttpRequest.prototype.send;

    function openReplacement(method, url, async, user, password) {
        this._url = url;
        return open.apply(this, arguments);
    }

    function sendReplacement(data) {
        if(this.onreadystatechange) {
            this._onreadystatechange = this.onreadystatechange;
        }
        console.log("YEYEYEYYEYEYE");
        this.onreadystatechange = onReadyStateChangeReplacement;
        return send.apply(this, arguments);
    }

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function onReadyStateChangeReplacement() {

        //YOUR CODE FOR READYSTATECHANGE
        await sleep(500);
        createSearch(getElementByXpath('/html/body/div[4]/div[2]/div/div[2]/div[7]/a'));

        if(this._onreadystatechange) {
            return this._onreadystatechange.apply(this, arguments);
        }
    }

    window.XMLHttpRequest.prototype.open = openReplacement;
    window.XMLHttpRequest.prototype.send = sendReplacement;

    function getElementByXpath(path) {
        return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }

    function createSearch(loc) {
        //let location = document.querySelector('.nav2').children[1];
        let inputBar = document.createElement('input');
        let searchLink = 'https://osu.ppy.sh/home/search?mode=all&query=';
        //let searchButton = location.children[location.children.length - 1].firstElementChild;

        inputBar.className = "search-header__input";
        inputBar.style.height = "15px";
        inputBar.style.color = "white";
        inputBar.style.display = "none";
        inputBar.style.margin = "auto";
        loc.style.cursor = "pointer";

        if(loc){
            console.log(loc);
            loc.removeAttribute("href");
            console.log("Removed href");
        }

        loc.onclick = function(){
            if(inputBar.style.display === "none") {
                inputBar.style.display = "inline";
                inputBar.addEventListener("keyup", function(event) {
                    event.preventDefault();

                    if(event.keyCode === 13){
                        console.log("Enter was clicked");
                        loc.href = searchLink + inputBar.value;
                        loc.click();
                    }
                });
            }else if(inputBar.style.display === "inline" && inputBar.value === ""){
                inputBar.style.display = "none";
            }
        };

        loc.parentElement.append(inputBar);
    }

    createSearch(getElementByXpath('/html/body/div[4]/div[2]/div/div[2]/div[7]/a'));
})();