Skip to content

Instantly share code, notes, and snippets.

@R167
Created April 23, 2023 19:43
Show Gist options
  • Save R167/e480c1c9333603d2728f20007b455d50 to your computer and use it in GitHub Desktop.
Save R167/e480c1c9333603d2728f20007b455d50 to your computer and use it in GitHub Desktop.
Bookmarklet to flip-flop GitHub's `relative-time` tag to use `format="datetime"`
javascript:(()=>{
const relativeTimes = document.querySelectorAll('relative-time[format="relative"], relative-time:not([format])');
if (relativeTimes.length > 0) {
for (let elem of relativeTimes) {
elem.setAttribute('data-old-config', JSON.stringify({
format: elem.getAttribute('format'),
hour: elem.getAttribute('hour'),
minute: elem.getAttribute('minute')
}));
elem.setAttribute('format', 'datetime');
elem.setAttribute('hour', '2-digit');
elem.setAttribute('minute', '2-digit');
}
console.log('Done setting attributes on ' + relativeTimes.length + ' relative-time tag(s).');
} else {
const oldRelativeTimes = document.querySelectorAll('relative-time[data-old-config]');
if (oldRelativeTimes.length > 0) {
for (let elem of oldRelativeTimes) {
const oldConfig = JSON.parse(elem.getAttribute('data-old-config'));
for (let attr in oldConfig) {
const value = oldConfig[attr];
if (value != null) {
elem.setAttribute(attr, value)
} else {
elem.removeAttribute(attr)
}
}
elem.removeAttribute('data-old-config');
}
console.log('Restored attributes on ' + oldRelativeTimes.length + ' relative-time tag(s).');
} else {
console.log('No relative-time tags found on this page.');
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment