/*
* @title nicosearchで自動補完
* @description niconicoにおいて、検索ワードのサジェストを表示します
* @include http://www.nicovideo.jp/*
* @include http://seiga.nicovideo.jp/*
* @include http://live.nicovideo.jp/*
* @include http://ch.nicovideo.jp/*
* @include http://com.nicovideo.jp/*
* @include http://dic.nicovideo.jp/*
* @license NYSL
*/
// 個人的にニコニコ静画、ニコニコ生放送、他"ニコニコ動画以外"を全く使わないので、動作検証は不十分です。
// IE9以降と最新のFirefox, Chromeで動作すると思います。Operaは知りません。
if (!Element.prototype.matchesSelector) {
Object.defineProperty(Element.prototype, "matchesSelector", {
enumerable: false,
configurable: true,
value: function(property){
if (this.webkitMatchesSelector) {
return this.webkitMatchesSelector(property);
}
else if (this.mozMatchesSelector) {
return this.mozMatchesSelector(property);
}
else if (this.msMatchesSelector) {
return this.msMatchesSelector(property);
}
else if (this.oMatchesSelector) {
return this.oMatchesSelector(property);
}
else
return null;
}
});
}
// ref: http://nicoapi.com/docs/nicosearchapi.html
// ref: https://chrome.google.com/webstore/detail/pafehjbkojbbenbdklnlajbmlojnecjl
(function main(){
var REQUEST_METHOD = "post";
var REQUEST_URL = "http://nicoapi.com/ns/suggest.xml";
var RESPONSE_LENGTH = 10;
var cache = {}; // 検索ワードと結果のペアを保持
var walker = null; // カーソルキーで動かした場合のTreeWalkerを保持
(function createStyle(){
if (document.querySelector("#nicosearch-style")) {
return false;
}
var style = document.createElement("style"), i = 0;
style.id = "nicosearch-style";
document.getElementsByTagName("head")[0].appendChild(style);
([
"#nicosearch-suggestbox {\
position: absolute;\
border: 1px solid #778899;\
background-color: #fff;\
z-index: 100000;\
}",
"#nicosearch-suggestbox > div {\
position: relative;\
}",
"#nicosearch-suggestbox > div.selected\
,#nicosearch-suggestbox > div:hover {\
background-color: #afeeee;\
}",
"#nicosearch-suggestbox a {\
text-align: left;\
}",
".nicosearch-dic-icon {\
padding-top: 1px;\
padding-bottom: 1px;\
}",
".nicosearch-name {\
position: absolute;\
top: 0;\
padding-left: 18px;\
height: 18px;\
font-size: 14px;\
}"
]).forEach(function(val){
style.sheet.insertRule(val, i++);
});
})();
var searchBox = (function getSearhBox(){
var target = document.getElementById("searchWord") // トップページ
|| document.getElementById("bar_search") // ニコニコ動画
|| document.getElementById("search_united") // 検索結果
|| document.querySelector("#searchResultSearchBox .searchInput") // 動画ページの検索結果
|| document.getElementById("bar_search") // ニコニコ静画
|| document.querySelector(".search_program .search_word") // ニコニコ生放送
|| document.getElementById("searchtxt") // ニコニコチャンネル
|| document.getElementById("search_text") // ニコニココミュニティ
|| document.getElementById("search-box") // ニコニコ大百科
;
return target;
})();
if (searchBox === null) {
window.alert("入力欄が見つかりません。");
//return false;
hoge();
}
var XmlHttp = function(word){
var xhr = new XMLHttpRequest();
xhr.open(REQUEST_METHOD, REQUEST_URL, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.addEventListener("readystatechange", function(evt){
if (xhr.readyState === 4) {
switch(xhr.status) {
case 200:
cache[word.toLowerCase()] = xhr.responseXML; // キャッシュに追加
createSuggestWindow(xhr.responseXML);
break;
}
}
}, false);
return xhr;
};
document.addEventListener("click", function(evt){
if (!evt.target.matchesSelector("#nicosearch-suggestbox")) {
removeSuggestWindow();
}
}, false);
searchBox.addEventListener("keydown", function(evt){
var suggestbox = document.getElementById("nicosearch-suggestbox");
if (suggestbox) {
if (!walker) walker = document.createTreeWalker(
suggestbox,
NodeFilter.SHOW_ELEMENT,
function(node){
return node.parentElement === suggestbox
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP;
},
false
);
switch(evt.keyCode) {
case 38: // ↑
if (!suggestbox.querySelector(".selected")) {
walker.lastChild();
walker.currentNode.className = "selected";
}
else if (walker.previousNode()) {
walker.currentNode.nextElementSibling.className = "";
walker.currentNode.className = "selected";
}
searchBox.value = walker.currentNode.textContent;
break;
case 40: // ↓
if (!suggestbox.querySelector(".selected")) {
walker.firstChild();
walker.currentNode.className = "selected";
}
else if (walker.nextNode()) {
walker.currentNode.previousElementSibling.className = "";
walker.currentNode.className = "selected";
}
searchBox.value = walker.currentNode.textContent;
break;
case 13: // Enter
if (suggestbox.querySelector(".selected")) {
walker.currentNode.getElementsByTagName("p")[0].click();
}
break;
default:
if (suggestbox.querySelector(".selected")) {
suggestbox.getElementsByClassName("selected")[0].className = "";
}
}
}
}, false);
searchBox.addEventListener("keyup", function(evt){
// 選択状態の場合はなにもしない
var suggestbox = document.getElementById("nicosearch-suggestbox");
if (suggestbox && suggestbox.querySelector(".selected")) {
return false;
}
var word = evt.target.value;
// 文字列が空の場合はサジェストを消す
if (word.length === 0) {
removeSuggestWindow();
return false;
}
// キャッシュをチェック
// 既に結果をもらっている場合は二度送らない
if (cache[word.toLowerCase()]) {
createSuggestWindow(cache[word.toLowerCase()]);
}
else {
window.setTimeout(function(){
var query = "i=" + encodeURI(word) + "&l=" + RESPONSE_LENGTH;
var xhr = new XmlHttp(word);
xhr.send(query);
}, 500);
}
}, false);
function removeSuggestWindow(){
var suggestbox = document.getElementById("nicosearch-suggestbox");
walker = null;
if (suggestbox) {
suggestbox.parentElement.removeChild(suggestbox);
}
}
function createSuggestWindow(xml){
if (xml.documentElement.getAttribute("status") !== "ok") return false;
walker = null;
var wrapper = document.getElementById("nicosearch-suggestbox");
if (!wrapper) {
wrapper = (function createWrapper(){
var wrapper = document.createElement("div");
var boxRect = searchBox.getBoundingClientRect();
wrapper.id = "nicosearch-suggestbox";
wrapper.style.top = boxRect.top + boxRect.height + "px";
wrapper.style.left = boxRect.left + "px";
wrapper.style.minWidth = searchBox.offsetWidth + "px";
return wrapper;
})();
}
else {
while (wrapper.hasChildNodes()) {
wrapper.removeChild(wrapper.firstChild);
}
}
var img = (function createImage(){
var img = document.createElement("img");
img.className = "nicosearch-dic-icon";
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKTWlDQ1BQaG90b3Nob3AgSUNDIHBy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Hv03/6asDVc9f41y5dn3m978bsG7duJt0cuCW69fh29u0XdwruTNxdeo94r/y+2v3qB/oP6n+0/rFlwG3g+GDAYM/DWQ/vDgmHnv6U/9OH4dJHzEfVI0YjjY+dHx8bDRq98mTOk+GnsqcTz8p+Vv9563Or59/94vtLz1j82PAL+YvPv655qfNy76uprzrHI8cfvM55PfGm/K3O233vuO+638e9H5ko/ED+UPPR+mPHp9BP9z7nfP78L/eE8/sl0p8zAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAE5SURBVHjapFOxasJQFD0vBFyqhVpQqJNDJyEghc4SMvghkh/IHtz9AAchENwdRTPEgmOcpR06pLUaselUBAdPh9JATAwpnu3ed+4799x3nyCJNDhCxA40UqTxJFwIKY/6uRwAiBMLzCkc2ZEB4LDd8qlSiTEeFwuUmk04Imn9vtfjXacDuVQSEgB8DIeQy+Xcvl8MA++DwW8Qui6nAD1NY154msYpwNB1Ka8sCwBQVJS/YWWqaySKioLQcbCyLEhr244RjiezyMLatpPPKAXBv/ZATmvxogu8Viuz4MF14xfUTROv3W6U+JrNzldXq7GwbpqQa7qO3XyeJG02mZ1cqypqug6QxN73GYxGuXbg2TD41u9z7/skyegvHA8Hfi+X+JxMsBuPU1Vv223cqCquGg1IhYIAgJ8BAFDSxqq0TSfNAAAAAElFTkSuQmCC";
return img;
})();
Array.prototype.forEach.call(xml.getElementsByTagName("item"), function(val){
var div = document.createElement("div");
var a = (function createAnchor(){
var a = document.createElement("a");
a.href = "http://dic.nicovideo.jp/a/" + encodeURIComponent(val.textContent);
a.target = "_blank";
return a;
})();
var p = (function createParagraph(){
var p = document.createElement("p");
p.className = "nicosearch-name";
return p;
})();
p.addEventListener("click", function(evt){
searchBox.value = val.textContent;
removeSuggestWindow();
}, false);
a.appendChild(img.cloneNode(false));
div.appendChild(a);
p.appendChild(document.createTextNode(val.textContent));
div.appendChild(p);
wrapper.appendChild(div);
});
document.body.appendChild(wrapper);
}
})();