function getPageData() {
    // Parse things for linkedin
    if (window.location.host.includes("linkedin")) {
        if (window.location.host.includes("merchantpool1")) {
            return;
        }
        const jobTitle = document.querySelector("h1").textContent;
        const company = document.querySelector(
            '.relative a[target="_self"]',
        ).textContent;

        const money =
            document
                .querySelector('ul li div div div li-icon[type="job"]')
                ?.parentNode?.parentNode?.parentNode?.parentNode?.children[1]?.children[0]?.textContent?.replaceAll(
                    /\s{2,}/g,
                    "",
                ) ??
            Object.values(
                document.querySelector(
                    'button[class="job-details-preferences-and-skills"]',
                )?.children ?? [],
            ).find(
                (a) => a.innerText.match(/\d/) && !a.innerText.match("skill"),
            )?.innerText ??
            "";

        const inperson_type =
            (Object.values(
                document.querySelector(
                    'button[class="job-details-preferences-and-skills"]',
                )?.children,
            )
                .find((a) => a.innerText.match(/hybrid|remote|on-site/i))
                ?.innerText?.split("\n") ?? [])[0]?.trim() ?? "";

        const location =
            document.querySelector(
                'div[class^="job-details-jobs-unified-top-card__primary-description-container"]',
            )?.children[0]?.children[0]?.innerText ?? "";

        const description = document.querySelector("article").textContent;

        let status = undefined;

        const maybeStatus = document.querySelector(
            ".artdeco-inline-feedback__message",
        )?.innerText;

        if (maybeStatus === "No longer accepting applications") {
            status = "expired";
        }

        browser.runtime.sendMessage({
            type: "GOT_INFO_R",
            company,
            jobTitle,
            money,
            description,
            location,
            inperson_type,
            status,
        });
        return;
    }
    // Ignore everything that is no glassdoor
    if (!window.location.host.includes("glassdoor")) return;
    const company = document
        .querySelector('header[data-test="job-details-header"]')
        .children[0].children[0].querySelector("h4").innerHTML;
    const jobTitle = document
        .querySelector('header[data-test="job-details-header"]')
        .querySelector("h1").innerHTML;

    const description = [
        ...document
            .querySelector('header[data-test="job-details-header"]')
            .parentNode.querySelectorAll("button"),
    ].filter((a) => a.textContent == "Show more")[0]?.parentNode?.parentNode
        ?.textContent;

    let money =
        document.querySelectorAll(
            'div[class^="SalaryEstimate_salaryRange"]',
        )?.[0]?.innerText ?? "";

    const location =
        document.querySelector('div[data-test="location"]')?.innerText ?? "";

    const moneySectionNode = document.querySelector("section>section");
    if (
        moneySectionNode &&
        ["Base pay range", "Base pay"].includes(
            moneySectionNode.querySelector("h2")?.textContent,
        )
    ) {
        money =
            moneySectionNode.querySelector("div>div>div").children[1]
                ?.textContent ?? "";
    }

    let status = undefined;

    if (!!document.querySelector("#expired-job-notice_Heading")) {
        status = "expired";
    }

    browser.runtime.sendMessage({
        type: "GOT_INFO_R",
        company,
        jobTitle,
        money,
        description,
        location,
        inperson_type: "",
        status,
    });
}
browser.runtime.onMessage.addListener((message) => {
    if (message.type === "GET_DATA_FROM_PAGE") {
        getPageData();
    } else if (message.type === "CHANGE_PAGE") {
        window.location.href = message.new_url;
    } else if (
        message.type === "MY_GET_URL_R" ||
        message.type === "GOT_INFO_R" ||
        message.type === "GOT_INTEREST" ||
        message.type === "AUTOLOAD_FOUND_GLASSDOOR_SEARCH"
    ) {
        window.postMessage(message);
    } else if (message.type === "AUTOLOAD_FINISHED") {
        if (
            window.location.href.includes("glassdoor") &&
            document.querySelector('a[data-test="for-you-page-link"]')
        ) {
            browser.runtime.sendMessage({
                type: "AUTOLOAD_FOUND_GLASSDOOR_SEARCH",
            });
            return;
        }
        if (
            !window.location.href.includes("glassdoor") &&
            !window.location.href.includes("linkedin")
        )
            return;
        browser.runtime.sendMessage({
            type: "AUTOLOAD_NOTIFY_URL",
            url: window.location.href,
        });
        setTimeout(getPageData, 1000);
    }
});

window.addEventListener("message", (e) => {
    if (e.data.type === "MY_GET_URL") {
        browser.runtime.sendMessage({ type: "MY_GET_URL" });
    } else if (e.data.type === "HAS_EXTENSION_Q") {
        window.postMessage({ type: "HAS_EXTENSION" });
    } else if (e.data.type === "REGISTER_INTEREST") {
        browser.runtime.sendMessage({ type: "REGISTER_INTEREST" });
    } else if (e.data.type === "R_GET_DATA_FROM_PAGE") {
        browser.runtime.sendMessage({ type: "R_GET_DATA_FROM_PAGE" });
    } else if (e.data.type === "CHANGE_PAGE") {
        browser.runtime.sendMessage({
            type: "CHANGE_PAGE",
            new_url: e.data.new_url,
        });
    }
});
