/*
* @title GoogleGuitarlet
* @description my bookmarklet
* @include http://www.google.co.jp
* @license MIT License
* @require
*/
// Firefox 4.0.1で動作確認
// Google Chromeでは動作しない
javascript:
(function(){
var state_ = '_';
window.addEventListener('keydown', function(e){
var key = e.keyCode;
switch( state_ ){
case '_':
if( key === 'R'.charCodeAt(0) ){
state_ = 'R';
document.title = '録音中';
startRecording();
e.stopPropagation();
}else if( key === 'S'.charCodeAt(0) ){
state_ = 'P';
document.title = '再生中';
perform(function(){
state_ = '_';
document.title = '再生終了';
});
e.stopPropagation();
}
break;
case 'R':
if( key === 'E'.charCodeAt(0) ){
state_ = '_';
stopRecording();
document.title = '録音終了';
e.stopPropagation();
}
break;
case 'P':
break;
}
}, true);
var r = [];
function startRecording(){
r = [];
document.body.addEventListener('keydown', keyRecorder, true);
}
function stopRecording(){
document.body.removeEventListener('keydown', keyRecorder, true);
}
function keyRecorder(e){
r.push({key: e.keyCode, time: e.timeStamp});
}
function perform(f){
var i = 0;
(function(){
var e = r[i++];
sendKey(e.key);
if( i < r.length )
setTimeout(arguments.callee, r[i].time - e.time);
else f();
})();
}
function sendKey(k){
var e = document.createEvent('KeyboardEvent');
e.initKeyEvent('keydown', !0, !0, window, !1, !1, !1, !1, k, 0);
document.body.dispatchEvent(e);
}
})();