/*
* @title bookmarklet
* @description a goofy script
* @include http://gakufu.gakki.me/m/data/*
* @license MIT License
* @require
*/
(function () {
var chords = [
'C',
'D♭',
'D',
'E♭',
'E',
'F',
'F#',
'G',
'A♭',
'A',
'B♭',
'B'
];
var chord_sharp = [
'C',
'C#',
'D',
'D#',
'E',
'F',
'F#',
'G',
'G#',
'A',
'A#',
'B'
];
var chord_flat = [
'C',
'D♭',
'D',
'E♭',
'E',
'F',
'G♭',
'G',
'A♭',
'A',
'B♭',
'B'
];
var keyChanger = document.getElementById('chg_key');
if (!keyChanger) return;
var sliderUnit = document.createElement('input');
sliderUnit.id = 'chg_chord_slider';
sliderUnit.type = 'range';
sliderUnit.setAttribute('min', '-6');
sliderUnit.setAttribute('max', '5');
sliderUnit.setAttribute('step', '1');
sliderUnit.value = '0';
keyChanger.appendChild(sliderUnit);
var showValue = document.createElement('input');
showValue.id = 'sliderValue';
showValue.type = 'text';
showValue.size = '2';
showValue.value = '0';
keyChanger.appendChild(showValue);
setTimeout(function () {
var elm = document.getElementById('chg_chord_slider');
elm.addEventListener('change', function(e){
var newValue = e.target.value;
var val = document.getElementById('sliderValue');
val.value = newValue;
transepose(Number(newValue));
}, false);
}, 300);
function transepose(offset){
var meChord = document.querySelectorAll('a[onclick] u.blue');
var f = Array.prototype;
f.forEach.call(meChord, function (chord) {
var chordTemp = chord.firstChild.nodeValue;
var baseKey = chordTemp.match(/([CDEFGAB]{1}[♭#]?)/g);
for (var i = 0; i < baseKey.length; i++) {
chord.firstChild.nodeValue = chord.firstChild.nodeValue.replace(baseKey[i], trans_(baseKey[i], offset));
}
});
}
// TRANSPOSE CHORD
function trans_(baseKey, offset){
var res = '';
var index = get_index(baseKey);
res += baseKey.replace(baseKey, chords[get_key(index + Number(offset))]);
return res;
}
function get_index(key) {
var res;
if (key.indexOf('#') > 0) {
res = chord_sharp.indexOf(key);
} else if (key.indexOf('♭') > 0) {
res = chord_flat.indexOf(key);
} else {
res = chords.indexOf(key);
}
return res;
}
function get_key(key) {
if (key < 0) {
key = key + 12;
} else if (key > 11) {
key = key - 12;
}
return key;
}
})();
/*
for example
http://gakufu.gakki.me/p/data/N03251.html
cf.
http://homepage2.nifty.com/maruichi/ez-eg/elt.html
TODO :
In the case of sharp or flat chords, want to be able to select either.
*/