UI Tweaks By web crawler 1 installs Rating 0.0 (0) approved

Neopets - Search Helper Enhanced

Add icons under item names in various places. Adds icons for new beta pages: trading post, quickstock, etc
neopets search helper
https://www.scriptneo.com/script/neopets-search-helper-enhanced

Version selector


SHA256
b47659bae4fee7f0cb28cb498eacfdae0ac2957a7dd9bdf7b11dadfbd1656d84
No scan flags on this version.

Source code

// ==UserScript==
// @name         Neopets - Search Helper Enhanced
// @version      1.1.4
// @namespace    neopets
// @description  Adds convenient search icons to many Neopets pages with cleaner handling, fewer duplicates, and improved SSW support
// @author       diceroll123, enhanced
// @match        *://*.neopets.com/auctions.phtml*
// @match        *://*.neopets.com/closet.phtml*
// @match        *://*.neopets.com/dome/neopets.phtml*
// @match        *://*.neopets.com/faerieland/darkfaerie.phtml*
// @match        *://*.neopets.com/faerieland/employ/employment.phtml*
// @match        *://*.neopets.com/faerieland/hiddentower938.phtml
// @match        *://*.neopets.com/gallery/index.phtml?*
// @match        *://*.neopets.com/games/kadoatery/*
// @match        *://*.neopets.com/generalstore.phtml*
// @match        *://*.neopets.com/genie.phtml*
// @match        *://*.neopets.com/halloween/esophagor*.phtml*
// @match        *://*.neopets.com/halloween/witchtower*.phtml*
// @match        *://*.neopets.com/hospital.phtml
// @match        *://*.neopets.com/inventory.phtml*
// @match        *://*.neopets.com/island/*training.phtml?*type=status*
// @match        *://*.neopets.com/island/kitchen*.phtml*
// @match        *://*.neopets.com/island/tradingpost.phtml*
// @match        *://*.neopets.com/market.phtml*
// @match        *://*.neopets.com/market_your.phtml*
// @match        *://*.neopets.com/medieval/earthfaerie.phtml*
// @match        *://*.neopets.com/objects.phtml*
// @match        *://*.neopets.com/pirates/academy.phtml?type=status*
// @match        *://*.neopets.com/process_cash_object.phtml
// @match        *://*.neopets.com/quests.phtml
// @match        *://*.neopets.com/quickstock.phtml*
// @match        *://*.neopets.com/safetydeposit.phtml*
// @match        *://*.neopets.com/shops/wizard.phtml*
// @match        *://*.neopets.com/space/coincidence.phtml
// @match        *://*.neopets.com/winter/igloo.phtml*
// @match        *://*.neopets.com/winter/snowfaerie*.phtml*
// @match        *://*.neopets.com/questlog/
// @match        *://*.neopets.com/games/teatime/
// @match        *://*.neopets.com/browseshop.phtml*
// @icon         https://www.neopets.com/favicon.ico
// @grant        none
// @downloadURL  https://www.scriptneo.com/scripts/download.php?id=45
// @updateURL    https://www.scriptneo.com/scripts/download.php?id=45
// ==/UserScript==

(function () {
    "use strict";

    const WAIT_FOR_JQUERY_MS = 0;
    const MAX_JQUERY_WAIT_ATTEMPTS = 100;

    let waitAttempts = 0;

    function waitForJquery() {
        if (typeof window.jQuery === "function") {
            main(window.jQuery);
            return;
        }

        waitAttempts++;

        if (waitAttempts <= MAX_JQUERY_WAIT_ATTEMPTS) {
            setTimeout(waitForJquery, WAIT_FOR_JQUERY_MS);
        }
    }

    waitForJquery();

    function main($) {
        const imgSize = 20;
        const currentUrl = document.URL;

        const linkmap = {
            ssw: {
                title: "Super Shop Wizard",
                img: "https://images.neopets.com/premium/shopwizard/ssw-icon.svg",
            },
            sw: {
                title: "Shop Wizard",
                url: "https://www.neopets.com/shops/wizard.phtml?string=%s",
                img: "https://images.neopets.com/themes/h5/basic/images/shopwizard-icon.png",
            },
            tp: {
                title: "Trading Post",
                url: "https://www.neopets.com/island/tradingpost.phtml?type=browse&criteria=item_exact&search_string=%s",
                img: "https://images.neopets.com/themes/h5/basic/images/tradingpost-icon.png",
            },
            au: {
                title: "Auction Genie",
                url: "https://www.neopets.com/genie.phtml?type=process_genie&criteria=exact&auctiongenie=%s",
                img: "https://images.neopets.com/themes/h5/basic/images/auction-icon.png",
            },
            sdb: {
                title: "Safety Deposit Box",
                url: "https://www.neopets.com/safetydeposit.phtml?obj_name=%s&category=0",
                img: "https://images.neopets.com/themes/h5/basic/images/v3/safetydeposit-icon.svg",
            },
            closet: {
                title: "Closet",
                url: "https://www.neopets.com/closet.phtml?obj_name=%s",
                img: "https://images.neopets.com/items/ffu_illusen_armoire.gif",
            },
            jni: {
                title: "Jellyneo Item Database",
                url: "https://items.jellyneo.net/search/?name=%s&name_type=3",
                img: "https://images.neopets.com/items/toy_plushie_negg_fish.gif",
            },
            battlepedia: {
                title: "Jellyneo Battlepedia",
                url: "https://battlepedia.jellyneo.net/index.php?search=%s",
                img: "https://images.neopets.com/favicon.ico",
            },
            dti: {
                title: "Dress to Impress",
                url: "https://impress.openneo.net/items?q=%scommit=search",
                img: "https://impress.openneo.net/assets/favicon-97a5bb419325a3c6559f3109f2374aa5ebf6c5f282e01c0e6a58407ee3f97c77.png",
            },
            idb: {
                title: "ItemDB",
                url: "https://itemdb.com.br/item/%s",
                img: "https://images.neopets.com/themes/h5/basic/images/v3/quickstock-icon.svg",
            },
        };

        injectCss();

        $.fn.exists = function () {
            return this.length > 0;
        };

        $.fn.justtext = function () {
            return $(this).clone().children().remove().end().text();
        };

        const isBeta = $("[class^='nav-pet-menu-icon']").exists();

        let premium = false;

        if (isBeta) {
            premium = $("[class^='navsub-ssw-icon']").exists();
        } else {
            premium = $("#sswmenu .imgmenu").exists();
        }

        function injectCss() {
            if (document.getElementById("search-helper-enhanced-css")) {
                return;
            }

            $("<style>", {
                id: "search-helper-enhanced-css",
                type: "text/css",
                text: `
                    .searchimg {
                        cursor: pointer;
                        height: ${imgSize}px !important;
                        width: ${imgSize}px !important;
                        border: none !important;
                        border-radius: 0 !important;
                        vertical-align: middle !important;
                       /* margin: 1px 2px !important; */
                    }

                    .search-helper {
                        margin-top: 0;
                        margin-bottom: 0;
                        line-height: 1.4;
                        display: inline-flex;
                        flex-wrap: wrap;
                        align-items: center;
                        gap: 2px;
                    }

                    .search-helper a {
                        display: inline-flex !important;
                        align-items: center;
                        justify-content: center;
                        text-decoration: none !important;
                    }

                    .qs-search-helper {
                        display: inline-flex !important;
                    }

                    .qs-search-helper a {
                        display: inline-flex !important;
                    }
                `,
            }).appendTo("head");
        }

        function inURL(substr) {
            return currentUrl.includes(substr);
        }

        function normalizeItemName(item) {
            return String(item || "")
                .replace(/\s+/g, " ")
                .trim();
        }

        function encodeItem(item) {
            return encodeURIComponent(normalizeItemName(item)).replace(/%20/g, "+");
        }

        function slugfy(itemName) {
            return String(itemName || "")
                .normalize("NFD")
                .replace(/[\u0300-\u036f]/g, "")
                .toLowerCase()
                .trim()
                .replace(/\s+/g, "-")
                .replace(/[^\w-]+/g, "")
                .replace(/--+/g, "-")
                .replace(/^-+|-+$/g, "");
        }

        function makeIcon(src, title) {
            return $("<img>", {
                src: src,
                class: "searchimg",
                alt: title,
                title: title,
                loading: "lazy",
            });
        }

        function makeExternalLink(item, urlTemplate, image, title, useRawValue) {
            const searchValue = useRawValue ? item : encodeItem(item);
            const url = urlTemplate.replace("%s", searchValue);

            return $("<a>", {
                tabindex: "-1",
                target: "_blank",
                rel: "noopener noreferrer",
                href: url,
                title: title,
            }).append(makeIcon(image, title));
        }

        function makeSSWLink(item) {
            return $("<a>", {
                tabindex: "-1",
                href: "#",
                class: "ssw-helper",
                "data-item": normalizeItemName(item),
                title: linkmap.ssw.title,
            }).append(makeIcon(linkmap.ssw.img, linkmap.ssw.title));
        }

        function alreadyHasHelper(element) {
            return $(element).parent().find(".search-helper").length !== 0;
        }

        function makelinks(item, extras) {
            item = normalizeItemName(item);

            if (!item) {
                return $();
            }

            extras = $.extend(
                {
                    cash: false,
                    wearable: false,
                    tradeable: true,
                    itemid: -1,
                },
                extras || {}
            );

            const element = inURL("quests.phtml") ? "div" : "p";
            const container = $("<" + element + ">", {
                class: "search-helper",
                "data-search-helper-item": item,
            });

            if (extras.cash === false && extras.tradeable === true) {
                if (!inURL("quests.phtml")) {
                    if (premium) {
                        container.append(makeSSWLink(item));
                    }

                    container.append(makeExternalLink(item, linkmap.sw.url, linkmap.sw.img, linkmap.sw.title));
                }

                container.append(makeExternalLink(item, linkmap.tp.url, linkmap.tp.img, linkmap.tp.title));
                container.append(makeExternalLink(item, linkmap.au.url, linkmap.au.img, linkmap.au.title));
            }

            if (!inURL("safetydeposit")) {
                container.append(makeExternalLink(item, linkmap.sdb.url, linkmap.sdb.img, linkmap.sdb.title));
            }

            if (extras.wearable && !inURL("closet.phtml")) {
                container.append(makeExternalLink(item, linkmap.closet.url, linkmap.closet.img, linkmap.closet.title));
            }

            if (extras.wearable) {
                let dtiUrl = "https://impress.openneo.net/items?q=%s";
                let dtiValue = item;
                let useRawValue = false;

                if (extras.itemid !== -1 && typeof extras.itemid !== "undefined" && extras.itemid !== "") {
                    dtiUrl = "https://impress.openneo.net/items/%s";
                    dtiValue = String(extras.itemid);
                    useRawValue = true;
                }

                container.append(makeExternalLink(dtiValue, dtiUrl, linkmap.dti.img, linkmap.dti.title, useRawValue));
            }

            container.append(makeExternalLink(item, linkmap.jni.url, linkmap.jni.img, linkmap.jni.title));
            container.append(makeExternalLink(slugfy(item), linkmap.idb.url, linkmap.idb.img, linkmap.idb.title, true));

            if (inURL("dome")) {
                container.append(makeExternalLink(item, linkmap.battlepedia.url, linkmap.battlepedia.img, linkmap.battlepedia.title));
            }

            return container;
        }

        function debounce(fn, delay) {
            let timer = null;

            return function () {
                clearTimeout(timer);

                const args = arguments;
                const context = this;

                timer = setTimeout(function () {
                    fn.apply(context, args);
                }, delay);
            };
        }

        function observeElement(element, callback) {
            if (!element) {
                return null;
            }

            const debouncedCallback = debounce(callback, 150);

            const observer = new MutationObserver(function () {
                debouncedCallback();
            });

            observer.observe(element, {
                childList: true,
                subtree: true,
            });

            return observer;
        }

        function clickPriceLimitedIfNeeded() {
            const priceLimited = document.querySelector("#price-limited");

            if (priceLimited && !priceLimited.checked) {
                priceLimited.click();
            }
        }

        function clickSSWSearchButton() {
            const searchButton =
                document.querySelector("#ssw-button-search") ||
                document.querySelector(".ssw-button-search__2020") ||
                document.querySelector("#button-search") ||
                document.querySelector(".button-search-white__2020");

            if (searchButton) {
                searchButton.click();
            }
        }

        function setSSWFields(item) {
            item = normalizeItemName(item);

            const criteria = $("#ssw-criteria");

            if (criteria.length) {
                criteria.val("exact").trigger("change");
            }

            clickPriceLimitedIfNeeded();

            const searchInput = $("#searchstr");

            if (searchInput.length) {
                searchInput.val(item).trigger("input").trigger("change");
            }
        }

        function clickNewSSWSearchIfAvailable() {
            const newSearchButton =
                document.querySelector("#ssw-button-new-search") ||
                document.querySelector(".ssw-button-new-search__2020");

            if (newSearchButton) {
                newSearchButton.click();
            }
        }

        function addAfterOnce(target, item, extras) {
            item = normalizeItemName(item);

            if (!item || !target || !target.length) {
                return;
            }

            if (target.parent().find(".search-helper").length !== 0) {
                return;
            }

            target.after(makelinks(item, extras));
        }

        function addAppendOnce(target, item, extras) {
            item = normalizeItemName(item);

            if (!item || !target || !target.length) {
                return;
            }

            if (target.find(".search-helper").length !== 0) {
                return;
            }

            target.append(makelinks(item, extras));
        }

        function genericQuest() {
            $(".item-img + .item-name")
                .find("b")
                .each(function (index, element) {
                    if (!alreadyHasHelper(element)) {
                        $(element).after(makelinks($(element).text()));
                    }
                });

            $("img[src*='/items/'] + br + p > b").each(function (index, element) {
                if (!alreadyHasHelper(element)) {
                    $(element).after(makelinks($(element).text()));
                }
            });
        }


        function addUpdatedTradingPostLinks() {
            if (!inURL("/island/tradingpost.phtml")) {
                return;
            }

            // Updated Trading Post browse cards. Example item name selector:
            // <p class="... item-name-text ...">Chic Bruce Wig</p>
            $(".item-name-text").each(function () {
                const nameEl = $(this);
                const itemName = normalizeItemName(nameEl.text());
                const itemBox = nameEl.closest("div");

                if (!itemName || itemBox.find(".search-helper").length !== 0) {
                    return;
                }

                const links = makelinks(itemName);

                links.css({
                    display: "inline-flex",
                    "margin-left": "0",
                    "margin-top": "2px",
                    "vertical-align": "middle",
                });

                nameEl.after(links);
            });

            // Updated Trading Post "View Details" grid. These item names do not have
            // stable classes, so target item image tiles and use the first direct <p> as the name.
            $(".bg-white img[src*='/items/'], .bg-white img[src*='images.neopets.com/items/']").each(function () {
                const img = $(this);
                const tile = img.closest(".flex.flex-col.items-center");

                if (!tile.length || tile.find(".search-helper").length !== 0) {
                    return;
                }

                const nameEl = tile.children("p").first();
                const itemName = normalizeItemName(nameEl.text());

                if (!itemName) {
                    return;
                }

                const links = makelinks(itemName);

                links.css({
                    display: "inline-flex",
                    "justify-content": "center",
                    "margin-top": "2px",
                });

                nameEl.after(links);
            });
        }

        function runBetaHandlers() {

            if (inURL("safetydeposit.phtml")) {
                function addModernSDBLinks() {
                    $(".sdb-item-info .sdb-item-name").each(function () {
                        const nameEl = $(this);
                        const infoBox = nameEl.closest(".sdb-item-info");
                        const itemName = normalizeItemName(nameEl.text());

                        if (!itemName || infoBox.find(".search-helper").length !== 0) {
                            return;
                        }

                        const links = makelinks(itemName);

                        links.css({
                            display: "inline-flex",
                            "margin-left": "6px",
                            "vertical-align": "middle",
                        });

                        nameEl.after(links);
                    });
                }

                addModernSDBLinks();

                const sdbContainer =
                    document.querySelector(".sdb-container") ||
                    document.querySelector("#content") ||
                    document.body;

                observeElement(sdbContainer, addModernSDBLinks);
            }
            if (inURL("objects.phtml") || inURL("type=shop")) {
                $(".item-img + .item-name").each(function (index, element) {
                    const item = $(element).text();

                    if ($(element).parent().find(".search-helper").length === 0) {
                        $(element).after(makelinks(item));
                    }
                });
            }

            if (inURL("inventory")) {
                const addInventoryLinks = function () {
                    $(".item-img + .item-name").each(function (index, element) {
                        if (!alreadyHasHelper(element)) {
                            const invDisplay = document.getElementById("invDisplay");

                            const extras = {
                                cash: invDisplay && invDisplay.dataset ? invDisplay.dataset.type === "nc" : false,
                                wearable: $(element).parent().find(":contains('wearable')").length > 0,
                                tradeable: $(element).parent().find(":contains('(no trade)')").length === 0,
                                itemid: -1,
                            };

                            $(element).after(makelinks($(element).text(), extras));
                        }
                    });
                };

                addInventoryLinks();
                $(document).ajaxSuccess(addInventoryLinks);
            }

            if (
                inURL("island/kitchen") ||
                inURL("winter/snowfaerie") ||
                inURL("halloween/esophagor") ||
                inURL("halloween/witchtower")
            ) {
                genericQuest();
                $(document).ajaxSuccess(genericQuest);
            }

            if ($(".faerie-img").exists()) {
                const itemname = normalizeItemName($(":contains('Where is my') > b").text());

                if (itemname) {
                    $(":contains('Where is my')")
                        .parent()
                        .find("img[src*='/items/']")
                        .each(function () {
                            const img = $(this);

                            if (img.parent().find(".search-helper").length === 0) {
                                img.after(makelinks(itemname));
                            }
                        });
                }
            }

            if (inURL("wizard.phtml?string=")) {
                $("#criteria").val("exact");
            }

            if (inURL("/hospital.phtml")) {
                $(".nh-disease-cure > span > b").each(function (k, v) {
                    const b = $(v);

                    if (b.parent().parent().find(".search-helper").length === 0) {
                        b.parent().after(makelinks(b.text()));
                    }
                });
            }

            if (inURL("/questlog")) {
                function addQuestLogLinks() {
                    $(".ql-reward-item + .ql-reward-label + .search-helper").remove();

                    $(".ql-reward-item + .ql-reward-label").each(function (k, v) {
                        const label = $(v);
                        const itemname = normalizeItemName(label.text());

                        if (itemname) {
                            label.after(makelinks(itemname));
                        }
                    });

                    $(".ql-bonus-reward > .search-helper").remove();

                    $(".ql-bonus-reward > .ql-bonus-name").each(function (k, v) {
                        const label = $(v);
                        const itemname = normalizeItemName(label.text());

                        if (!itemname || itemname.endsWith(",000 NP")) {
                            return;
                        }

                        label.after(makelinks(itemname));
                    });
                }

                addQuestLogLinks();

                const questLogContent = document.getElementById("QuestLogContent");
                observeElement(questLogContent, addQuestLogLinks);
            }

            if (inURL("/games/teatime")) {
                function addTaviLinks() {
                    $(".ttwt-item > img[src*='/items/'] + span").each(function (index, element) {
                        if (!alreadyHasHelper(element)) {
                            $(element).after(makelinks($(element).text()));
                        }
                    });
                }

                addTaviLinks();

                $(document).ajaxSuccess(function () {
                    setTimeout(addTaviLinks, 1000);
                });
            }

            if (inURL("/winter/igloo.phtml")) {
                $(".igs-item b").each(function (index, element) {
                    if (!alreadyHasHelper(element)) {
                        $(element).after(makelinks($(element).text()));
                    }
                });
            }

            if (inURL("generalstore.phtml")) {
                $(".item-title").each(function (index, element) {
                    const title = $(element);

                    if (title.parent().find(".search-helper").length !== 0) {
                        return;
                    }

                    const links = makelinks(title.text());

                    links.css({
                        "margin-top": "-10px",
                    });

                    title.after(links);
                });
            }


            if (inURL("/dome/neopets")) {
                $(".equipFrame").each(function (index, element) {
                    const frame = $(element);
                    const itemname = normalizeItemName(frame.text());

                    if (itemname && frame.find(".search-helper").length === 0) {
                        makelinks(itemname).appendTo(frame);
                    }
                });

                $(".equipTable").css({
                    "overflow-y": "scroll",
                });
            }
        }

        function runClassicHandlers() {
            if (inURL("/island/tradingpost.phtml")) {
                $("img[src*='/items/']").each(function (k, v) {
                    const img = $(v);
                    const itemTextNode = v.nextSibling;
                    const itemName = itemTextNode ? normalizeItemName(itemTextNode.nodeValue) : "";

                    if (itemName && img.parent().find(".search-helper").length === 0) {
                        $(itemTextNode).after(makelinks(itemName));
                    }
                });
            }

            if (inURL("process_cash_object")) {
                const extras = {
                    cash: true,
                    wearable: true,
                };

                $("img[src*='/items/']")
                    .parent()
                    .find("b")
                    .each(function (k, v) {
                        const b = $(v);

                        if (b.parent().find(".search-helper").length === 0) {
                            b.before("<br>").after(makelinks(b.text(), extras));
                        }
                    });
            }

            if (inURL("auction_id")) {
                const nameb = $("b:contains('owned by')");
                let fixname = nameb.html() || "";

                fixname = fixname.substring(0, fixname.indexOf(" (own"));

                if (fixname && nameb.parent().find(".search-helper").length === 0) {
                    nameb.parent().find("img").after(makelinks(fixname));
                }
            }

            if (inURL("auctions.phtml") || inURL("genie.phtml")) {
                $("a[href*='?type=bids&auction_id=']:not(:has('img'))").each(function (index, element) {
                    const link = $(element);
                    const itemname = normalizeItemName(link.text());

                    if (itemname && link.parent().find(".search-helper").length === 0) {
                        link.after(makelinks(itemname));
                    }
                });
            }

            const isSDB = inURL("safetydeposit");
            const isCloset = inURL("closet");

            if (isSDB || isCloset) {
                $("img[src*='/items/']").each(function (k, v) {
                    const img = $(v);
                    const row = img.parent().parent();
                    const itemInput = row.find("td").eq(5).find("input");

                    let id = itemInput.attr("data-item_id") || "";

                    if (!id && itemInput.attr("name")) {
                        const idMatch = itemInput.attr("name").match(/\d+/g);
                        id = idMatch ? idMatch[0] : "";
                    }

                    const isWearable =
                        isCloset ||
                        row.find("td").eq(1).text().includes("(wearable)");

                    let isNeoCash = false;

                    if (isSDB) {
                        const category = row.find("td").eq(3);
                        isNeoCash = normalizeItemName(category.text()) === "Neocash";
                    } else if (isCloset) {
                        isNeoCash = img
                            .parent()
                            .next("td")
                            .text()
                            .includes("(Artifact - 500)");
                    }

                    const extras = {
                        cash: isNeoCash,
                        wearable: isWearable,
                        itemid: id || -1,
                    };

                    const nametd = row.find("td").eq(1);
                    const itemNameEl = nametd.find("b").eq(0);

                    if (itemNameEl.length && nametd.find(".search-helper").length === 0) {
                        itemNameEl.after(makelinks(itemNameEl.justtext(), extras));
                    }
                });
            }

            if (
                inURL("type=your") ||
                inURL("market_your") ||
                $("[name=subbynext]").length === 2
            ) {
                $("img[src*='/items/']").each(function (k, v) {
                    const row = $(v).parent().parent();
                    const nametd = row.find("td").eq(0);

                    if (nametd.find(".search-helper").length !== 0) {
                        return;
                    }

                    let itemname = nametd.text();
                    itemname = itemname.replace(nametd.find(".medText").text(), "");
                    itemname = normalizeItemName(itemname);

                    if (itemname) {
                        nametd.find("b").eq(0).after(makelinks(itemname));
                    }
                });
            }

            if (inURL("space/coincidence")) {
                $("img[src*='/items/']").each(function (k, v) {
                    const nametd = $(v).parent();

                    if (nametd.find(".search-helper").length === 0) {
                        nametd.find("b").eq(0).after(makelinks(nametd.justtext()));
                    }
                });
            }

            if (inURL("/island/training.phtml?type=status")) {
                $("img[src*='/items/']").each(function (k, v) {
                    const img = $(v);

                    if (img.parent().find(".search-helper").length === 0) {
                        img.after(makelinks(img.prev().text()));
                    }
                });
            }

            if (inURL("/island/fight_training.phtml?type=status")) {
                $("img[src*='/items/']").each(function (k, v) {
                    const img = $(v);

                    if (img.parent().find(".search-helper").length === 0) {
                        img.after(makelinks(img.prev().text()));
                    }
                });
            }

            if (inURL("/pirates/academy.phtml?type=status")) {
                $("img[src*='/items/']").each(function (k, v) {
                    const nametd = $(v).parent();
                    const row = nametd.parent();
                    const itemNameEl = row.find("td > b").eq(0);
                    const itemname = itemNameEl.text();

                    if (itemname && row.find(".search-helper").length === 0) {
                        itemNameEl.after(makelinks(itemname));
                    }
                });
            }

            if (inURL("employment")) {
                if (inURL("type=jobs")) {
                    $("b:contains('Find')").each(function (k, v) {
                        const b = $(v);

                        if (b.parent().find(".search-helper").length !== 0) {
                            return;
                        }

                        let itemname = b
                            .parent()
                            .clone()
                            .find("b")
                            .remove()
                            .end()
                            .html()
                            .split("<br>")[0];

                        itemname = normalizeItemName($("<div>").html(itemname).text());

                        if (itemname) {
                            $($(v)[0].nextSibling).after(makelinks(itemname));
                        }
                    });
                }

                if (inURL("job_id")) {
                    const findEl = $("b:contains('Find')").eq(0);

                    if (findEl.length && findEl.parent().find(".search-helper").length === 0) {
                        findEl.after(makelinks(findEl.justtext()));
                    }
                }
            }

            if (inURL("quests.phtml")) {
                $("img[src*='/items/']").each(function (k, v) {
                    const itemname = $(v).parent().find("b");

                    if (itemname.length && itemname.parent().find(".search-helper").length === 0) {
                        itemname.after(makelinks(itemname.text()));
                    }
                });
            }

            if (inURL("games/kadoatery")) {
                $("td:contains('You should give it'):not(:contains('Thanks,'))").each(function (k, v) {
                    const itemname = $(v).find("strong").last();

                    if (itemname.length && $(v).find(".search-helper").length === 0) {
                        itemname.after(makelinks(itemname.text()));
                    }
                });
            }

            if (inURL("/gallery/index.phtml")) {
                $("img.itemimg[src*='/items/']").each(function (k, img) {
                    const imgEl = $(img);

                    imgEl
                        .siblings("b.textcolor")
                        .first()
                        .each(function (i, nameEl) {
                            const name = $(nameEl);

                            if (name.parent().find(".search-helper").length === 0) {
                                name.after(makelinks(name.text()));
                            }
                        });
                });
            }

            if (inURL("browseshop.phtml")) {
                $(".content table")
                    .find("b:not([style*='red;'])")
                    .each(function (index, element) {
                        const b = $(element);

                        if (b.parent().find(".search-helper").length === 0) {
                            b.after(makelinks(b.text()));
                        }
                    });
            }

            if (inURL("hiddentower938.phtml")) {
                $(".content table")
                    .find("b:not([style*='red;'])")
                    .each(function (index, element) {
                        const b = $(element);

                        if (b.parent().find(".search-helper").length === 0) {
                            b.after(makelinks(b.text()));
                        }
                    });
            }

            if (inURL("market.phtml?type=sales")) {
                $('[value="Clear Sales History"]')
                    .parent()
                    .parent()
                    .parent()
                    .parent()
                    .find("tr")
                    .each(function (index, element) {
                        const cell = $(element).find("td").eq(1);

                        if (cell.attr("bgcolor") === "#ffffcc" && cell.find(".search-helper").length === 0) {
                            cell.append(makelinks(cell.text()));
                        }
                    });
            }
        }

        function sswlimited(item) {
            item = normalizeItemName(item);

            return (
                /Nerkmid($|.X+$)/.test(item) ||
                item.endsWith("Paint Brush") ||
                item.endsWith("Transmogrification Potion") ||
                item.endsWith("Laboratory Map")
            );
        }

        function sswopenBeta(item) {
            $(".premium-widget__2024").hide();

            if (typeof window.toggleWidget__2020 === "function") {
                window.toggleWidget__2020("ssw");
            }

            setTimeout(function () {
                clickNewSSWSearchIfAvailable();

                setTimeout(function () {
                    setSSWFields(item);
                    clickSSWSearchButton();
                }, 200);
            }, 200);
        }

        function sswopenClassic(item) {
            if ($(".sswdrop").hasClass("panel_hidden")) {
                $("#sswmenu .imgmenu").click();
            }

            if ($("#ssw-tabs-1").hasClass("ui-tabs-hide")) {
                try {
                    $("#ssw-tabs").tabs("select", 0);
                } catch (error) {
                    $("#ssw-tabs a[href='#ssw-tabs-1']").trigger("click");
                }
            }

            setTimeout(function () {
                setSSWFields(item);
                clickSSWSearchButton();
            }, 200);
        }

        function sswopen(item) {
            item = normalizeItemName(item);

            if (!item) {
                return;
            }

            if (isBeta) {
                sswopenBeta(item);
            } else {
                sswopenClassic(item);
            }
        }


        function runQuickstockHandler() {
            if (!inURL("quickstock.phtml")) {
                return;
            }

            function addQuickstockLinks() {
                const quickstockRows = $("table.quickstock-table tbody.np-table-tbody tr, tbody.np-table-tbody tr");

                quickstockRows.each(function (index, row) {
                    const rowEl = $(row);
                    const cells = rowEl.children("td");
                    const cell = cells.first();

                    if (!cell.length) {
                        return;
                    }

                    // Skip Check All row.
                    if (cell.text().trim().toLowerCase().includes("check all")) {
                        return;
                    }

                    if (cell.find(".search-helper").length !== 0) {
                        return;
                    }

                    const nameSpan = cell.children("span").first();
                    const itemname = normalizeItemName(nameSpan.length ? nameSpan.text() : cell.clone().find(".search-helper").remove().end().text());

                    if (!itemname) {
                        return;
                    }

                    const isNeoCash = cell.find(".qs-cash-marker").length !== 0;
                    const hasClosetOption = cells.eq(6).find("input[type='radio'][value='closet']").length !== 0;

                    const extras = {
                        cash: isNeoCash,
                        wearable: hasClosetOption,
                    };

                    const links = makelinks(itemname, extras).addClass("qs-search-helper");

                    links.css({
                        display: "flex",
                        "flex-wrap": "wrap",
                        "align-items": "center",
                        gap: "2px",
                        "margin-top": "4px",
                        "margin-left": "0",
                    });

                    cell.css({
                        display: "flex",
                        "flex-direction": "column",
                        "align-items": "flex-start",
                        gap: "3px",
                    });

                    if (nameSpan.length) {
                        nameSpan.after(links);
                    } else {
                        cell.append(links);
                    }
                });
            }

            addQuickstockLinks();

            // Quickstock can render after the initial page load, and this page does not
            // always trip the beta-layout detector, so watch the page body.
            observeElement(document.body, addQuickstockLinks);
        }

        if (inURL("/island/tradingpost.phtml")) {
            addUpdatedTradingPostLinks();
            observeElement(document.body, addUpdatedTradingPostLinks);
        }

        runQuickstockHandler();

        if (isBeta) {
            runBetaHandlers();
        } else {
            runClassicHandlers();
        }

        $("body").on("click", "a.ssw-helper", function (event) {
            event.preventDefault();

            const item = $(this).attr("data-item");

            sswopen(item);
        });
    }
})();