Indeed Show Post Date V1

Displays the "days ago" post-date badge on Indeed job pages, as well as a MM/DD/YYYY badge

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Indeed Show Post Date V1
// @version      V1
// @description  Displays the "days ago" post-date badge on Indeed job pages, as well as a MM/DD/YYYY badge
// @author       Tato288 (made with genAI, so uhh, not really)
// @license      MIT
// @match        https://*.indeed.com/viewjob?*
// @grant        none
// @namespace https://greasyfork.org/users/1496168
// ==/UserScript==

(function() {
  'use strict';

  // Helper to find the age text once
  function extractAge() {
    for (let script of document.getElementsByTagName('script')) {
      const txt = script.textContent;
      if (!txt) continue;
      const m = txt.match(/"age"\s*:\s*"([^"]+\s+days?\s+ago)"/);
      if (m) return m[1];
    }
    return null;
  }

  // Builds a badge element with given text and styling
  function buildBadge(text, backgroundColor = '#0073b1') {
    const badge = document.createElement('div');
    badge.className = 'indeed-postdate-badge';
    badge.textContent = text;
    badge.style.cssText = `
      display: inline-block;
      margin-left: 8px;
      padding: 2px 6px;
      font-size: 0.85em;
      font-weight: 600;
      color: #fff;
      background: ${backgroundColor};
      border-radius: 3px;
      vertical-align: middle;
    `;
    return badge;
  }

  // Inserts badges into the title container
  function insertBadges(container, ageText) {
    // If already injected, skip
    if (container.parentNode.querySelector('.indeed-postdate-badge')) return;

    // 1) Age badge
    const ageBadge = buildBadge(`Posted: ${ageText}`);
    container.parentNode.insertBefore(ageBadge, container.nextSibling);

    // 2) Absolute date badge, only if not "30+ days ago"
    if (!/30\+/.test(ageText)) {
      const daysMatch = ageText.match(/(\d+)\s+days?/);
      if (daysMatch) {
        const daysAgo = parseInt(daysMatch[1], 10);
        const now = new Date();
        now.setDate(now.getDate() - daysAgo);
        const mm = String(now.getMonth() + 1).padStart(2, '0');
        const dd = String(now.getDate()).padStart(2, '0');
        const yyyy = now.getFullYear();
        const dateBadge = buildBadge(`${mm}/${dd}/${yyyy}`, '#005582');
        // insert to the right of the age badge
        ageBadge.parentNode.insertBefore(dateBadge, ageBadge.nextSibling);
      }
    }

    console.log('✅ Indeed Post‑Date V1: badges injected/persisted');
  }

  // Main init after page load
  function init() {
    const ageText = extractAge();
    if (!ageText) {
      console.warn('⚠️ Indeed Post‑Date V1: age text not found');
      return;
    }

    // Target the title container
    const titleContainer = document.querySelector('.jobsearch-JobInfoHeader-title-container');
    if (!titleContainer) {
      console.warn('⚠️ Indeed Post‑Date V1: title container not found');
      return;
    }

    // Initial insert
    insertBadges(titleContainer, ageText);

    // Observe for re-renders
    const parent = titleContainer.parentNode;
    const observer = new MutationObserver(() => {
      insertBadges(titleContainer, ageText);
    });
    observer.observe(parent, { childList: true });
    console.log('🔄 Indeed Post‑Date V1: MutationObserver attached');
  }

  // Wait for the header container to appear, then run init
  const readyInterval = setInterval(() => {
    if (document.querySelector('.jobsearch-JobInfoHeader-title-container')) {
      clearInterval(readyInterval);
      init();
    }
  }, 500);

})();