Skip to content

Instantly share code, notes, and snippets.

@btamayo
Forked from rf5860/GistAgeHighlighter.user.js
Created April 8, 2020 19:48
Show Gist options
  • Save btamayo/ef1aba3ece4af206c83d7a31176ed27c to your computer and use it in GitHub Desktop.
Save btamayo/ef1aba3ece4af206c83d7a31176ed27c to your computer and use it in GitHub Desktop.
[Gist Age Highlighter] Highlights the age of different Github Gist results, based on age #UserScript
// ==UserScript==
// @name Gist Age Highlighter
// @description Highlights the age of different Github Gist results, based on age
// @version 0.4
// @author rjf89
// @match https://gist.github.com/search*
// @grant none
// ==/UserScript==
Array.prototype.flatMap = function(lambda) { return Array.prototype.concat.apply([], this.map(lambda)); };
const setColor = (e, bgColor) => e.parentElement.style = `background-color: ${bgColor}; color: ivory`;
function highlightElement(e) {
if (e.innerText) {
if (/years? ago/.test(e.innerText)) setColor(e, 'darksalmon');
if (/months? ago/.test(e.innerText)) setColor(e, 'darkorange');
if (/weeks? ago/.test(e.innerText)) setColor(e, 'aliceblue');
if (/days? ago/.test(e.innerText)) setColor(e, 'greenyellow');
}
}
[...document.querySelectorAll('time-ago')].forEach(highlightElement);
const gistResults = document.querySelector('div.repository-content.gist-content > div > div.column.three-fourths');
const mutationConditions = { childList: true, subtree: true };
new MutationObserver((mutationsList) => [...mutationsList]
.filter(mutation => !!mutation.addedNodes)
.flatMap(mutation => [...mutation.addedNodes])
.filter(node => node.querySelectorAll)
.flatMap(node => [...node.querySelectorAll('time-ago')])
.forEach(highlightElement)
).observe(gistResults, mutationConditions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment