Created
October 21, 2015 08:29
-
-
Save yuuan/5a9b181a6a5020c609be to your computer and use it in GitHub Desktop.
This file contains 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 Translate DateTime in GitLab | |
// @namespace http://www.yuuan.net/item/1054 | |
// @description 英語で書かれた日付を数字に変える | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js | |
// @include http://gitlab.com/* | |
// @version 0.1 | |
// @grant none | |
// ==/UserScript== | |
(function($) { | |
var selectors = [{ | |
selector: '.commits-row-date > span', | |
target: 'html', | |
}, { | |
selector: 'time', | |
target: [ 'title', 'data-original-title' ], | |
}]; | |
var months = { | |
Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6, Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12 | |
}; | |
var format = function(year, month, day, origin) { | |
if (month in months) { | |
return [ | |
year, | |
('0' + months[month]).slice(-2), | |
('0' + day).slice(-2), | |
].join('/'); | |
} | |
else return origin; | |
}; | |
var patterns = [{ | |
regex: /(\d{1,2}) ([A-Z][a-z]{2}), (\d{4})/, | |
replacer: function(match, day, month, year) { | |
return format(year, month, day, match); | |
}, | |
}, { | |
regex: /([A-Z][a-z]{2}) (\d{1,2}), (\d{4})/, | |
replacer: function(match, month, day, year) { | |
return format(year, month, day, match); | |
}, | |
}]; | |
var translate = function(date) { | |
patterns.forEach(function(pattern) { | |
if (pattern.regex.test(date)) { | |
date = date.replace(pattern.regex, pattern.replacer); | |
return false; | |
} | |
}); | |
return date; | |
}; | |
var DateTag = function(tag, target) { | |
this.tag = tag; | |
this.target = target; | |
DateTag.prototype.get = function() { | |
if (target == 'html') return this.tag.html(); | |
else return this.tag.attr(this.target); | |
}; | |
DateTag.prototype.set = function(translated) { | |
if (target == 'html') this.tag.html(translated); | |
else this.tag.attr(this.target, translated); | |
}; | |
DateTag.prototype.translate = function() { | |
this.set(translate(this.get())); | |
}; | |
}; | |
var replace = function() { | |
selectors.forEach(function(selector) { | |
var tags = $(selector.selector); | |
if (tags) { | |
tags.each(function() { | |
var tag = $(this); | |
var targets = $.isArray(selector.target) ? selector.target : [ selector.target ]; | |
targets.forEach(function(target) { | |
var dateTag = new DateTag(tag, target); | |
dateTag.translate(); | |
}); | |
}); | |
} | |
}); | |
}; | |
$(document).ready(replace); | |
$(window).load(replace); | |
})(jQuery.noConflict(true)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment