Skip to content

Instantly share code, notes, and snippets.

@rf5860
Last active May 19, 2018 16:39
Show Gist options
  • Save rf5860/eb539c2cb4d35f8de3950b20f9354b54 to your computer and use it in GitHub Desktop.
Save rf5860/eb539c2cb4d35f8de3950b20f9354b54 to your computer and use it in GitHub Desktop.
[BitBucket DateDisplay] Display non-fuzzy dates for BitBucket #UserScript
// ==UserScript==
// @name BitBucket DateDisplay
// @version 0.1
// @description Display non-fuzzy dates for BitBucket
// @author rjf89
// @match http*://bitbucket.org/*/pull-requests/*
// @grant none
// ==/UserScript==
const setNonFuzzy = (time) => time.textContent = `${new Date(time.getAttribute('datetime')).toISOString().slice(0, 16).replace('T', ' ')} (${time.textContent})`;
const isTimeNode = (node) => node.nodeName === 'TIME';
const getTimeNodes = (node) => node && node.nodeType === 1 ? [...node.getElementsByTagName('time')] : [];
const hasTimeNodes = (node) => getTimeNodes(node).length > 0;
const isNotUpdated = (target) => !target.textContent.includes(' (');
const timeChildrenAdded = (type, nodes) => type === 'childList' && nodes.length >= 1 && [...nodes].some(hasTimeNodes);
const timeChanged = (type, target) => (type === 'characterData' || type === 'attributes') && isTimeNode(target) && isNotUpdated(target);
function updateDates(mutations) {
[...mutations]
.filter(({type, target, addedNodes}) => timeChildrenAdded(type, addedNodes) || timeChanged(type, target))
.forEach(function({type, target, addedNodes}) {
if (type === 'childList') { [].concat(...[...addedNodes].map(getTimeNodes)).forEach(setNonFuzzy); }
else { setNonFuzzy(target); }
});
}
(function() {
'use strict';
[...document.getElementsByTagName('time')].forEach(setNonFuzzy);
new MutationObserver(updateDates).observe(document.documentElement, { characterData: true, childList: true, subtree: true, attributes: true, attributeFilter: ['datetime'] });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment