Simpli-Applied

Tracks applied jobs and highlights them in the Simplify Github page

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Simpli-Applied
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Tracks applied jobs and highlights them in the Simplify Github page
// @author       Agni
// @match        https://github.com/SimplifyJobs/New-Grad-Positions*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    console.log('script running');

    const APPLIED_JOBS = 'appliedJobs';

    function getAppliedJobs() {
        const stored = localStorage.getItem(APPLIED_JOBS);
        return stored ? JSON.parse(stored) : [];
    }

    function saveAppliedJob(trHTML) {
        const appliedJobs = getAppliedJobs();
        if (!appliedJobs.includes(trHTML)) {
            appliedJobs.push(trHTML);
            localStorage.setItem(APPLIED_JOBS, JSON.stringify(appliedJobs));
        }
    }

    function removeAppliedJob(trElement) {
        const appliedJobs = getAppliedJobs();
        const hrefToRemove = trElement.querySelector('a[href]')?.href;

        const filtered = appliedJobs.filter(jobHTML => {
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = jobHTML;
            const jobHref = tempDiv.querySelector('a[href]')?.href;
            return jobHref !== hrefToRemove;
        });

        localStorage.setItem(APPLIED_JOBS, JSON.stringify(filtered));
    }

    function getTrHTMLWithoutCheckbox(tr) {
        const clone = tr.cloneNode(true);
        const checkbox = clone.querySelector('.applied-checkbox');
        if (checkbox) {
            checkbox.parentElement.remove();
        }
        return clone.outerHTML;
    }

    function highlightAppliedJobs(allTrs) {
        const appliedJobs = getAppliedJobs();
        allTrs.forEach(tr => {
            const trHTMLWithoutCheckbox = getTrHTMLWithoutCheckbox(tr);
            if (appliedJobs.includes(trHTMLWithoutCheckbox)) {
                tr.style.backgroundColor = 'green';
                const checkbox = tr.querySelector('.applied-checkbox');
                if (checkbox) checkbox.checked = true;
            }
        });
    }

    function addColumn(thead) {
        if (thead.querySelector('th:first-child')?.textContent === 'Applied') return;

        const tr = thead.querySelector('tr');
        const newTh = document.createElement('th');
        newTh.textContent = 'Applied';
        tr.insertBefore(newTh, tr.firstChild);
    }

    function addMarkAsAppliedCheckbox(allTrs, thead) {
        addColumn(thead);

        allTrs.forEach(tr => {
            if (tr.querySelector('.applied-checkbox')) return;

            const newTd = document.createElement('td');
            const checkbox = document.createElement('input');
            checkbox.type = 'checkbox';
            checkbox.className = 'applied-checkbox';
            checkbox.style.transform = 'scale(1.2)';

            checkbox.addEventListener('change', () => {
                const trHTML = getTrHTMLWithoutCheckbox(tr);
                if (checkbox.checked) {
                    saveAppliedJob(trHTML);
                    tr.style.backgroundColor = 'green';
                } else {
                    removeAppliedJob(tr);
                    tr.style.backgroundColor = '';
                }
            });

            newTd.appendChild(checkbox);
            newTd.style.textAlign = 'center';
            tr.insertBefore(newTd, tr.firstChild);

            const links = tr.querySelectorAll('a[href]');
            links.forEach(link => {
                link.addEventListener('click', () => {
                    const trHTML = getTrHTMLWithoutCheckbox(tr);
                    saveAppliedJob(trHTML);
                    tr.style.backgroundColor = 'green';
                    const checkbox = tr.querySelector('.applied-checkbox');
                    if (checkbox) checkbox.checked = true;
                });
            });
        });
    }

    function markApplied() {
        const article = document.querySelector('article.markdown-body');
        if (!article) return;

        const thead = article.querySelector('thead');
        const tbody = article.querySelector('tbody');
        if (!tbody) return;

        const allTrs = [...tbody.querySelectorAll('tr')];

        addMarkAsAppliedCheckbox(allTrs, thead);
        highlightAppliedJobs(allTrs);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', markApplied);
    } else {
        markApplied();
    }

})();