← All examples

JavaScript Reading & focus stackoverflow.com

Stack Overflow: flag old answers

Adds a warning above answers older than a set year to remind you the solution may be out of date.

Code to copy

/* Flag answers older than a chosen year as possibly outdated */
(function () {
  var CUTOFF = 2018;
  function mark() {
    document.querySelectorAll('#answers .answer').forEach(function (a) {
      if (a.dataset.sofAge) return;
      var t = a.querySelector('.user-action-time time, .relativetime');
      var iso = t && (t.getAttribute('datetime') || t.getAttribute('title'));
      if (!iso) return;
      var year = new Date(iso).getFullYear();
      if (!isNaN(year) && year < CUTOFF) {
        a.dataset.sofAge = '1';
        var tag = document.createElement('div');
        tag.textContent = '⚠ Possibly outdated (' + year + ')';
        tag.style.cssText = 'background:#fff4d6;color:#7a5b00;padding:4px 8px;border-radius:4px;margin:6px 0;font-size:12px;font-weight:600;';
        var cell = a.querySelector('.answercell') || a;
        cell.insertBefore(tag, cell.firstChild);
      }
    });
  }
  mark();
  new MutationObserver(mark).observe(document.body, { childList: true, subtree: true });
})();

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases