Skip to content

Instantly share code, notes, and snippets.

@SeinopSys
Last active August 22, 2017 13:58
Show Gist options
  • Save SeinopSys/661de4c0c90f20a41c4e to your computer and use it in GitHub Desktop.
Save SeinopSys/661de4c0c90f20a41c4e to your computer and use it in GitHub Desktop.
DeviantArt comment timestamp parser + userscript
function parseCommentDate(el){
var _months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Nov','Dec'],
_pad = function(n){return n<10?'0'+n:n},
time = el.title.replace(/^.*\(at (\d{1,2}):(\d{1,2}):(\d{1,2}) ([AP]M)\)$/, function(_,h,m,s,ampm){
if (ampm === 'PM'){
h = parseInt(h,10)+12;
if (h >= 24)
h -= 24;
}
else if (ampm === 'AM' && h == 12) h = 0;
return [_pad(h),_pad(m),_pad(s)].join(':');
}),
date = el.innerHTML.replace(/([A-Z][a-z]{2}) (\d{1,2}), (\d{4})/,function(_,m,d,y){
return [y,_pad(_months.indexOf(m)+1),_pad(d)].join('-');
});
return new Date(date+' '+time).toISOString();
}
// ==UserScript==
// @name Standard DA Timestamps
// @namespace http://localhost/
// @version 1.0
// @description Replaces non-standard DeviantArt comment timestamps with standard <time> tags
// @author SeinopSys
// @include *.deviantart.com*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function parseCommentDate(el){
var _months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
_pad = function(n){return n<10?'0'+n:n;},
time = el.title.replace(/^.*(?:\(at|,) (\d{1,2}):(\d{1,2}):(\d{1,2}) ([AP]M)\)?$/, function(_,h,m,s,ampm){
if (ampm === 'PM'){
h = parseInt(h,10)+12;
if (h >= 24)
h -= 24;
}
else if (ampm === 'AM' && h == 12) h = 0;
return [_pad(h),_pad(m),_pad(s)].join(':');
}),
date = (/\(at /.test(el.title) ? el.innerHTML : el.title).replace(/.*([A-Z][a-z]{2}) (\d{1,2})(?:, (\d{4}))?.*/,function(_,m,d,y){
return [y?y:new Date().getFullYear(),_pad(_months.indexOf(m)+1),_pad(d)].join('-');
});
return new Date(date+' '+time).toISOString();
}
var listener = function(){
// Comments
var time = document.querySelectorAll('.cc-time'), el, time_tag;
if (time.length)
for (var i=0,l=time.length; i<l; i++){
el = time[i].querySelector('a[title]');
time_tag = document.createElement('time');
time_tag.setAttribute('datetime', parseCommentDate(el));
time_tag.innerHTML = el.innerHTML;
el.innerHTML = '';
el.appendChild(time_tag);
}
// Image post date
el = document.querySelector('.dev-metainfo-content dl dd span[title][ts]');
if (el){
time_tag = document.createElement('time');
var datetime = new Date(parseInt(el.getAttribute('ts')+'000',10)).toISOString();
time_tag.setAttribute('datetime', datetime);
time_tag.setAttribute('title', el.getAttribute('title'));
time_tag.innerHTML = el.innerHTML;
el.parentNode.replaceChild(time_tag, el);
var first_metainf_copy = document.querySelector('.dev-metainfo-content .dev-metainfo-copy-control'),
time_copy = first_metainf_copy.cloneNode(true);
time_copy.innerHTML = "<strong>Posted at (ISO)</strong><br><input class='embed-code' readonly value='"+datetime+"'>";
time_copy.addEventListener('click',function(){
console.log(this);
this.select();
}.bind(time_copy.querySelector('.embed-code')));
first_metainf_copy.parentNode.insertBefore(time_copy, first_metainf_copy);
}
};
listener();
window.addEventListener('popstate', listener);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment