Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.6 kB
2
Indexable
Never
(function() {
    try {
        /* main variables */
        var debug = 0;
        var variation_name = "";
        var $;
        /* all Pure helper functions */

        function waitForElement(selector, trigger, delayInterval, delayTimeout) {
            var interval = setInterval(function() {
                if (
                    document &&
                    document.querySelector(selector) &&
                    document.querySelectorAll(selector).length > 0
                ) {
                    clearInterval(interval);
                    trigger();
                }
            }, delayInterval);
            setTimeout(function() {
                clearInterval(interval);
            }, delayTimeout);
        }


        function listener() {
            /* These are the modifications: */
            window.addEventListener("locationchange", function() {
                if(window.location.href.indexOf("/datasheet") !== -1){
                    waitForElement('#__next > .datasheet .header > div > ul > li:last-child a', init, 50, 15000);
                } else {
                    document.querySelector(".eg-table-container") && document.querySelector(".eg-table-container").remove();
                }
            });
            history.pushState = ((f) =>
                function pushState() {
                    var ret = f.apply(this, arguments);
                    window.dispatchEvent(new Event("pushstate"));
                    window.dispatchEvent(new Event("locationchange"));
                    return ret;
                })(history.pushState);
            history.replaceState = ((f) =>
                function replaceState() {
                    var ret = f.apply(this, arguments);
                    window.dispatchEvent(new Event("replacestate"));
                    window.dispatchEvent(new Event("locationchange"));
                    return ret;
                })(history.replaceState);
            window.addEventListener("popstate", () => {
                window.dispatchEvent(new Event("locationchange"));
            });
        }


        /* Variation Init */
        function init() {
            if (window.location.href.indexOf("/datasheet") != -1) {
                let egURL = document.querySelector("#__next > .datasheet .header > div > ul > li:last-child a").href;
                getTable(egURL);
            } else {
                document.querySelector(".eg-table-container") && document.querySelector(".eg-table-container").remove();
            }
        }

        async function getTable(url) {
            let response = await fetch(url);
            let data = await response.text();
            let ele = document.createElement("div");
            ele.innerHTML = data;
            let egTable = ele.querySelector("#__next .page-container > div:nth-child(2)");
            if (!document.querySelector(".eg-table-container")) {
                egTable.classList.add("eg-table");
                waitForElement("#__next > .datasheet", function() {
                    document.querySelector("#__next > .datasheet").insertAdjacentHTML("afterend", `<div class="eg-table-container"><div class="eg-wrapper"></div></div>`);
                    document.querySelector(".eg-table-container > .eg-wrapper").insertAdjacentElement("afterbegin", egTable);
                }, 100, 15000);
            }
        }

        /* Initialize variation */
        waitForElement('#__next > .datasheet .header > div > ul > li:last-child a', init, 50, 15000);
        listener();
    } catch (e) {
        if (debug) console.log(e, "error in Test" + variation_name);
    }
})();