// ==UserScript==
// @name My command for Minibuffer
// @namespace Minibuffer.My.Command
// @description my user.js of the 俺, for the 俺, by the 俺
// @include *
// ==/UserScript==
(function () {
var Search = function() { this.initialize.apply(this, arguments); };
Search.prototype = {
initialize: function() {
var self = this;
var divEl = document.createElement('div');
var inputEl = document.createElement('input');
var labelEl = document.createElement('label');
inputEl.id = 'searchWord';
labelEl.setAttribute('for', inputEl.id);
divEl.setAttribute('style', [
'-moz-border-radius: 10px 10px 10px 10px;',
'background-color: #333;',
'bottom: 0;',
'left: 0;',
'margin: .3em;',
'padding: .6em;',
'opacity: 0.8;',
'paddign: 0 6px;',
'position: fixed;',
'text-align: center;',
'width: 320px;',
'font-size: 9pt;',
'z-index: 999;'
].join(' '));
inputEl.setAttribute('style', [
'background-color: #222;',
'border-bottom: 1px solid #cfccc6;',
'border-left: 1px solid #696660;',
'border-right: 1px solid #cfccc6;',
'border-top: 1px solid #696660;',
'color: #fafafa;',
'font-family: Monaco, monospace;',
'font-size: 9pt;',
'width: 80%;'
].join(' '));
labelEl.setAttribute('style', [
'color: #fafafa;',
'font-weight: bold;',
'font-size: 9pt;',
'margin-right: .3em;'
].join(' '));
divEl.appendChild(labelEl);
divEl.appendChild(inputEl);
document.body.appendChild(divEl);
var post = function() {
if ( inputEl.value != '' ) {
inputEl.disabled = true;
self.post(inputEl.value, function() {
self.endCall.call(self, inputEl.value);
});
}
};
inputEl.addEventListener('keypress', function(e) {
if ( e.keyCode == 27 ) {
self.endCall.call(self);
e.preventDefault();
e.stopPropagation();
}
if ( e.keyCode == 13 ) {
post();
e.preventDefault();
e.stopPropagation();
}
}, false);
this.title = labelEl;
this.form = inputEl;
this.content = divEl;
},
set: function() {
this.content.style.display = 'block';
this.form.focus();
this.title.innerHTML = this.label;
},
endCall: function(mes) {
this.content.style.display = 'none';
this.form.disabled = false;
},
post: function(s, callback) {
switch ( this.type ) {
case 2:
stdin = [ this.url + s.replace(/\s+/, '+') ];
break;
case 3:
stdin = [ this.url + s ];
break;
default:
stdin = [ this.url + encodeURIComponent(s) ];
break;
}
window.Minibuffer.execute('open', stdin);
callback();
}
};
if ( typeof window.Minibuffer != 'undefined' ) {
// Google
var mySearch;
window.Minibuffer.addCommand({
name: 'My::GoogleSearch',
command: function() {
if ( !mySearch ) mySearch = new Search();
mySearch.label = 'Google';
mySearch.type = 1;
mySearch.url = 'http://google.com/search?q=';
mySearch.set();
}
});
window.Minibuffer.addShortcutkey({
key: 'M G',
command: function() { window.Minibuffer.execute('My::GoogleSearch'); }
});
// CPAN
window.Minibuffer.addCommand({
name: 'My::CPANSearch',
command: function() {
if ( !mySearch ) mySearch = new Search();
mySearch.label = 'CPAN';
mySearch.type = 1;
mySearch.url = 'http://search.cpan.org/search?mode=all&query=';
mySearch.set();
}
});
window.Minibuffer.addShortcutkey({
key: 'M C',
command: function() { window.Minibuffer.execute('My::CPANSearch'); }
});
// Yahoo!辞書
window.Minibuffer.addCommand({
name: 'My::DictionarySearch',
command: function() {
if ( !mySearch ) mySearch = new Search();
mySearch.label = 'Y! Dict.';
mySearch.type = 1;
mySearch.url = 'http://dic.yahoo.co.jp/dsearch?enc=UTF-8&dtype=2&stype=0&p=';
mySearch.set();
}
});
window.Minibuffer.addShortcutkey({
key: 'M D',
command: function() { window.Minibuffer.execute('My::DictionarySearch'); }
});
// ニコニコ動画
window.Minibuffer.addCommand({
name: 'My::NicoVideoSearch',
command: function() {
if ( !mySearch ) mySearch = new Search();
mySearch.label = 'NicoVideo',
mySearch.type = 2;
mySearch.url = 'http://www.nicovideo.jp/search/';
mySearch.set();
}
});
window.Minibuffer.addShortcutkey({
key: 'M N',
command: function() { window.Minibuffer.execute('My::NicoVideoSearch'); }
});
// Amazon.co.jp
window.Minibuffer.addCommand({
name: 'My::AmazonSearch',
command: function() {
if ( !mySearch ) mySearch = new Search();
mySearch.label = 'Amazon';
mySearch.type = 3;
mySearch.url = 'http://www.amazon.co.jp/s/?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=';
mySearch.set();
}
});
window.Minibuffer.addShortcutkey({
key: 'M A',
command: function() { window.Minibuffer.execute('My::AmazonSearch'); }
});
// Subscribe current url
window.Minibuffer.addCommand({
name: 'My::Subscribe',
command: function(stdin) {
return stdin.map(function(url) { return "javascript:location.href='http://reader.livedoor.com/subscribe/" + url + "'"; } );
}
});
window.Minibuffer.addShortcutkey({
key: 'M a',
command: function() {
window.Minibuffer.execute('location | My::Subscribe | open');
}
});
}
}) ();