Skip to content

Instantly share code, notes, and snippets.

@loadletter
Last active October 1, 2021 15:39
Show Gist options
  • Save loadletter/8fc4560f9742ac3b86fdfc7009f2e82b to your computer and use it in GitHub Desktop.
Save loadletter/8fc4560f9742ac3b86fdfc7009f2e82b to your computer and use it in GitHub Desktop.
Converts 4chan file names like 1276731803495.jpg to corresponding dates so you can see when the image was originally posted.
// ==UserScript==
// @id org.userscripts.users.kuehlschrank.4chanFileAge
// @name 4chan: File Age
// @description Converts 4chan file names like 1276731803495.jpg to corresponding dates so you can see when the image was originally posted.
// @author kuehlschrank
// @version 2017.01.30
// @include http*://boards.4chan.org/*
// ==/UserScript==
/* original source @ http://userscripts-mirror.org/scripts/review/94520 */
(function() {
function schedule() {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(convert, 250);
}
function convert() {
var now = Date.now();
var re = /^(\d+)s?\.([a-z]{3})$/;
var list = root.querySelectorAll('div.fileText a');
var parsed;
for(var i = list.length, span; i-- && (span = list[i]);) {
if(re.exec(span.innerHTML) && (parsed = parseInt(RegExp.$1)) && parsed > 1064966400 && parsed < 2147483647000) {
var date = new Date(parsed);
span.title = date.toLocaleString();
span.innerHTML = 'Age: ' + age(now - date.getTime()); /* comment/delete this line if you want only date on hover */
}
}
}
function age(millis) {
var days = Math.floor(millis / 86400000);
if(days > 720) {
return Math.floor(days / 365) + ' years';
} else if(days > 60) {
return Math.floor(days / 30) + ' months';
} else if(days > 21) {
return Math.floor(days / 7) + ' weeks';
} else if(days > 1) {
return days + ' days';
} else {
var mins = Math.floor(millis/60000);
return mins > 120 ? Math.floor(mins / 60) + ' hours' : mins + ' minutes';
}
}
var root = document.body.querySelector('div.board');
var timeoutId = 0;
var M = window.MutationObserver || window.WebKitMutationObserver;
new M(schedule).observe(root, {childList:true, subtree:true});
schedule();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment