Snippets
Created by
markdiary
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | // ==UserScript==
// @name 楽器me移調スライダー
// @namespace labs.markdiary.com
// @include http://gakufu.gakki.me/m/*
// @version 1
// @grant none
// ==/UserScript==
(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++) {
// console.log(baseKey + ' : ' + baseKey[i] + '=>' + trans_(baseKey[i], offset));
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;
}
})();
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.