jnaskali / GitHub Inactive Development Warning Redux

// ==UserScript==
// @name        GitHub Inactive Development Warning Redux
// @author      jnaskali
// @copyright   2024, Juhani Naskali (www.naskali.fi); 2019-2021, Zach Hardesty (https://zachhardesty.com/)
// @license     GPL-3.0-only; http://www.gnu.org/licenses/gpl-3.0.txt
// @version     1.2
// @namespace   https://www.naskali.fi
// @updateURL   https://openuserjs.org/meta/jnaskali/GitHub_Inactive_Development_Warning_Redux.meta.js
// @downloadURL https://openuserjs.org/install/jnaskali/GitHub_Inactive_Development_Warning_Redux.user.js
//
// @match       https://github.com/*/*
// @grant       none
// $run-at      document-idle
//
// @require https://cdn.jsdelivr.net/gh/CoeJoder/waitForKeyElements.js@v1.2/waitForKeyElements.js
//
// @description Display big banner if project's last commit over 6 months ago and giant banner if over 1 year ago. Based on work by Zach Hardesty <zachhardesty7@users.noreply.github.com>. Updated when original script broke.
// ==/UserScript==

waitForKeyElements("div[data-testid='latest-commit-details'] relative-time", (element) => {
  if (document.querySelector("#zh-banner-warning")) return

  const date = new Date(element.attributes.datetime.value)
  const daysSinceLastCommit = ( Date.now() - date.getTime() ) / 1000 / 60 / 60 / 24
  const yearsSinceLastCommit = Math.floor(daysSinceLastCommit / 365)

  if (daysSinceLastCommit > 365) {
    renderWarning(yearsSinceLastCommit)
  } else if (daysSinceLastCommit > 182.5) {
    renderCaution()
  }
});

function displayMessage(el) {
  document
    .querySelector("#js-repo-pjax-container")
    .insertAdjacentElement("beforebegin", el)
}

function renderWarning(yearsSinceLastCommit) {
  const banner = document.createElement("div")
  banner.id = "zh-banner-warning"
  banner.setAttribute(
    "style",
    `
    background-color: red;
    height: 100px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 36px;
  `
  )

  banner.textContent = "WARNING: repo hasn't received an update in " + yearsSinceLastCommit + "+ year(s)"

  displayMessage(banner)
}

function renderCaution() {
  const banner = document.createElement("div")
  banner.id = "zh-banner-warning"
  banner.setAttribute(
    "style",
    `
    background-color: yellow;
    height: 50px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
  `
  )

  banner.textContent = "Caution: repo hasn't received an update in 6+ months"

  displayMessage(banner)
}