-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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