Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compatibility fix for Nikki’s Paste-a-Date!* for better script interaction (innerHTML → standard DOM methods) (*) http://userscripts.org/scripts/show/121217
// ==UserScript==
// @name MusicBrainz: Paste-A-Date!
// @description This script adds a field after the existing date fields where you can paste a (numeric) date and it will try to parse it.
// @version 2012-01-05 modified by j99, please compare and check, it's ok for me now
// @author -
// @namespace df069240-fe79-11dc-95ff-0800200c9a66
//
// @include http://*musicbrainz.org/*
// ==/UserScript==
//**************************************************************************//
/* Javascript sucks! */
function unique(arr) {
var r = new Array();
o:for(var i = 0, n = arr.length; i < n; i++) {
for(var x = 0, y = r.length; x < y; x++) {
if(r[x]==arr[i])
continue o;
}
r[r.length] = arr[i];
}
return r;
}
/* End of workaround because Javascript sucks! */
function nikkis_messy_date_extractor (str) {
var ymd = /^\W*((?:[0-9]{2})?[0-9]{2})(?:(?:\W+||\W?)(0?[1-9]|1[0-2])(?:(?:\W+||\W?)(0?[1-9]|[12][0-9]|3[01])(?:|)?)?)?\W*$/;
var dmy = /^\W*(?:(0?[1-9]|[12][0-9]|3[01])\W+)?(0?[1-9]|1[0-2])\W+((?:[0-9]{2})?[0-9]{2})\W*$/;
var mdy = /^\W*(0?[1-9]|1[0-2])\W+(0?[1-9]|[12][0-9]|3[01])\W+((?:1[89]|20)?[0-9]{2})\W*$/;
var dmy_text = /^\W*(?:(0?[1-9]|[12][0-9]|3[01])\W+)?([\w ]+)\W+((?:[0-9]{2})?[0-9]{2})\W*$/;
var ymd_text = /^\W*((?:[0-9]{2})?[0-9]{2})\W+([\w ]+)(?:\W+(0?[1-9]|[12][0-9]|3[01]))?\W*$/;
var mdy_text = /^\W*([\w+]+)\W+(0?[1-9]|[12][0-9]|3[01])\W+((?:[0-9]{2})?[0-9]{2})\W*$/;
var dates = Array();
if (ymd.test(str)) {
var matches = ymd.exec(str);
var newdate = clean_date(matches[1], matches[2], matches[3]);
if (newdate) dates.push(newdate);
}
if (dmy.test(str)) {
var matches = dmy.exec(str);
if (matches[3] > 2484) matches[3] = matches[3] - 543; // Thai years
var newdate = clean_date(matches[3], matches[2], matches[1]);
if (newdate) dates.push(newdate);
}
if (mdy.test(str)) {
var matches = mdy.exec(str);
var newdate = clean_date(matches[3], matches[1], matches[2])
if (newdate) dates.push(newdate);
}
if (dmy_text.test(str)) {
var matches = dmy_text.exec(str);
var month = get_month(matches[2]);
var newdate = clean_date(matches[3], month, matches[1]);
if (newdate) dates.push(newdate);
}
if (ymd_text.test(str)) {
var matches = ymd_text.exec(str);
var month = get_month(matches[2]);
var newdate = clean_date(matches[1], month, matches[3]);
if (newdate) dates.push(newdate);
}
if (mdy_text.test(str)) {
var matches = mdy_text.exec(str);
var month = get_month(matches[1]);
var newdate = clean_date(matches[3], month, matches[2]);
if (newdate) dates.push(newdate);
}
return dates;
}
function clean_date(y, m, d) {
if (y.length == 2) y = (y > 20) ? "19"+y : "20"+y;
if (m && m.length == 1) m = "0"+m;
if (d && d.length == 1) d = "0"+d;
if (d == 29 && m == 2 && y%4 != 0 && y%400 != 0) return false;
if (d == 30 && m == 2) return false;
if (d == 31 && (m == 2 || m == 4 || m == 6 || m == 9 || m == 11)) return false;
var newdate = [ y ];
if (m) newdate.push(m);
if (d) newdate.push(d);
return newdate.join("-");
}
function get_date(id) {
var div = document.getElementById("display"+id);
var str = document.getElementById(id).value;
var dates = unique(nikkis_messy_date_extractor(str).sort());
if (str.length < 1) {
div.style.borderColor = 'white';
div.style.backgroundColor = 'white';
div.innerHTML = "";
div.style.display = 'inline';
} else if (dates.length == 1) {
set_date(id, dates[0].split('-')[0], dates[0].split('-')[1], dates[0].split('-')[2]);
div.style.borderColor = 'green';
div.style.backgroundColor = '#CDFFBD';
div.innerHTML = "✓";
div.style.display = 'inline';
} else if (dates.length > 1) {
div.style.borderColor = 'yellow';
div.style.backgroundColor = '#FFEEBD';
div.style.display = 'block';
div.innerHTML = "multiple possibilities:";
for (var j = 0; j < dates.length; j++) {
div.appendChild( document.createElement("br"));
var a = document.createElement("a");
a.innerHTML = dates[j];
a.style.cursor = "pointer";
a.addEventListener("click", (function(i,j,k,l) { return function(){ set_date(i,j,k,l); }})(id, dates[j].split('-')[0], dates[j].split('-')[1], dates[j].split('-')[2]), false);
div.appendChild(a, div);
}
} else {
div.style.borderColor = 'red';
div.style.backgroundColor = '#FFBDCD';
div.innerHTML = "✗";
div.style.display = 'inline';
}
}
function set_date(id, year, month, day) {
var e = document.getElementById(id).parentNode.getElementsByTagName("input");
e[0].value = year ? year : "";
e[1].value = month ? month : "";
e[2].value = day ? day : "";
}
function get_month (str) {
var months = Array();
months["january"] = "1";
months["jan"] = "1";
months["janv"] = "1";
months["januari"] = "1";
months["janeiro"] = "1";
months["januar"] = "1";
months["janvier"] = "1";
months["february"] = "2";
months["feb"] = "2";
months["févr"] = "2";
months["februari"] = "2";
months["fevereiro"] = "2";
months["februar"] = "2";
months["février"] = "2";
months["march"] = "3";
months["mar"] = "3";
months["mars"] = "3";
months["março"] = "3";
months["märz"] = "3";
months["mars"] = "3";
months["april"] = "4";
months["apr"] = "4";
months["avr"] = "4";
months["abril"] = "4";
months["avril"] = "4";
months["may"] = "5";
months["mai"] = "5";
months["maj"] = "5";
months["maio"] = "5";
months["mai"] = "5";
months["june"] = "6";
months["jun"] = "6";
months["juin"] = "6";
months["juni"] = "6";
months["junho"] = "6";
months["juin"] = "6";
months["july"] = "7";
months["jul"] = "7";
months["juil"] = "7";
months["juli"] = "7";
months["julho"] = "7";
months["juillet"] = "7";
months["august"] = "8";
months["aug"] = "8";
months["augusti"] = "8";
months["agosto"] = "8";
months["août"] = "8";
months["september"] = "9";
months["sep"] = "9";
months["sept"] = "9";
months["setembro"] = "9";
months["septembre"] = "9";
months["october"] = "10";
months["oct"] = "10";
months["oktober"] = "10";
months["outubro"] = "10";
months["octobre"] = "10";
months["november"] = "11";
months["nov"] = "11";
months["novembro"] = "11";
months["novembre"] = "11";
months["december"] = "12";
months["dec"] = "12";
months["déc"] = "12";
months["dezembro"] = "12";
months["dezember"] = "12";
months["décembre"] = "12";
months["enero"] = "1";
months["febrero"] = "2";
months["marzo"] = "3";
months["mayo"] = "4";
months["junio"] = "6";
months["julio"] = "7";
months["septiembre,"] = "9";
months["octubre"] = "10";
months["noviembre"] = "11";
months["diciembre"] = "12";
// roman numerals
months["i"] = "1";
months["ii"] = "2";
months["iii"] = "3";
months["iv"] = "4";
months["v"] = "5";
months["vi"] = "6";
months["vii"] = "7";
months["viii"] = "8";
months["ix"] = "9";
months["x"] = "10";
months["xi"] = "11";
months["xii"] = "12";
months["Ⅰ"] = "1";
months["Ⅱ"] = "2";
months["Ⅲ"] = "3";
months["Ⅳ"] = "4";
months["Ⅴ"] = "5";
months["Ⅵ"] = "6";
months["Ⅵ"] = "7";
months["Ⅷ"] = "8";
months["Ⅸ"] = "9";
months["Ⅹ"] = "10";
months["Ⅺ"] = "11";
months["Ⅻ"] = "12";
if (months[str.toLowerCase()])
return months[str.toLowerCase()];
return str;
}
var e = document.getElementsByClassName('partial-date');
for (var i = 0; i < e.length; i++) {
if (!document.location.pathname.match(/^\/release\//))
e[i].style.display = "block";
// e[i].innerHTML += ' <input type="text" id="dateinput'+i+'" size="12" /><div id="displaydateinput'+i+'" style="border:1px solid white"></div>';
var it = e[i].appendChild(document.createElement("input"));
it.setAttribute("type", "text");
it.setAttribute("size", "12");
it.setAttribute("id", "dateinput"+i);
it.style.marginLeft = "4px";
var div = e[i].appendChild(document.createElement("div"));
div.setAttribute("id", "displaydateinput"+i);
div.style.border = "1px solid white";
document.getElementById('dateinput'+i).addEventListener("input", (function(i){ return function(){ get_date('dateinput'+i); }})(i), false);
}
/* workaround for weird placeholder text in release editor */
var idy = document.getElementById('id-date.year');
var idm = document.getElementById('id-date.month');
var idd = document.getElementById('id-date.day');
if (idy && idm && idd) {
var iyear = document.createElement("input");
iyear.id="id-date.year";
iyear.name="date.year";
iyear.size="4";
iyear.maxlength="4";
iyear.value=idy.value;
idy.parentNode.replaceChild(iyear, idy);
var imonth = document.createElement("input");
imonth.id="id-date.month";
imonth.name="date.month";
imonth.size="2";
imonth.maxlength="2";
imonth.value=idm.value;
idm.parentNode.replaceChild(imonth, idm);
var iday = document.createElement("input");
iday.id="id-date.day";
iday.name="date.day";
iday.size="2";
iday.maxlength="2";
iday.value=idd.value;
idd.parentNode.replaceChild(iday, idd);
}
/* end workaround for weird behaviour of placeholder text in release editor */
function removeChildren(p) {
while (p && p.hasChildNodes()) { p.removeChild(p.firstChild); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment