2009年5月30日土曜日

Operaで使えるニコニコ大百科ポップアップを更新

このエントリーをはてなブックマークに追加
昨日のコードだと、FirefoxとChromeでは動かなかったから動くようにちょっち修正。
後、見た目とかを一部修正。
影は面倒だからつけなかった。
Flashに重なると表示されないのはブラウザの仕様。あきらめてください。
これがドラッグできると良いよね。
え? 作れ?
面倒。

// ==UserScript==
// @name Popup Nico Dict for Iframe
// @namespace http://gigi-net.net
// @include http://dic.nicovideo.jp/*
// @include http://www.nicovideo.jp/watch/*
// @author giginet modified Arc Cosine
// @version 1.1
// ==/UserScript==
(function(){
/** simple version of $X
* $X(exp);
* $X(exp, context);
* @source http://gist.github.com/3242.txt
*/
var $X = function (exp, context) {
context || (context = document);
var expr = (context.ownerDocument || context).createExpression(exp, function (prefix) {
return document.createNSResolver(context.documentElement || context).lookupNamespaceURI(prefix) ||
context.namespaceURI || document.documentElement.namespaceURI || "";
});
var result = expr.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
// not ensure the order.
var ret = [], i = null;
while (i = result.iterateNext()) ret.push(i);
return ret;
}
return null;
}


var tip = null;

var init = function(){
if( checkOption() ){
createNicoDic();
}else{
popup();
}
};

var createNicoDic = function(){
document.getElementById("container").style.display = 'none';
//clear div
var cap = document.getElementsByTagName("h1")[0].innerHTML;
var desc = document.getElementById("article");
var article= desc.innerHTML;

//置き換え用の適当な文字列生成
var rand_st ="st"+(new Date()).getTime();
var rand_ed ="ed"+(new Date()).getTime();

//ページメニューを消去
article =article.replace(/<div id..page-menu.>.*<.div>/i,"");

//見出しを適当な文字に変換
article =article.replace(/<h2.*?>/gm,rand_st);
article =article.replace(/<.h2>/gm,rand_ed);
//ページメニューを消去
article =article.replace(/<div id..page-menu.>.*<.div>/,"");
//タグを全消去
article =article.replace(/<.*?>/mg,"");
//適当な文字を見出しに戻す
article =article.replace(new RegExp(rand_st,"gm"),"<br>【");
article =article.replace(new RegExp(rand_ed,"gm"),"】<br>");
//空の見出しを削除
article =article.replace(/<br>【.*】<br>\s*<br>【/gm,"【");

var ARTICLE_LENGTH = 300;
showText = article.substring(0,ARTICLE_LENGTH-1);
if(article.length>=ARTICLE_LENGTH){
showText +="...";
}
//タイトルを追加
showText ="<h1>"+cap+"</h1>"+showText;
var descDiv = document.body.appendChild(document.createElement('div'));
descDiv.style.display = 'block';
descDiv.style.fontSize='10pt';
descDiv.style.fontFamily='sans-serif';
descDiv.style.textAlign='left';
descDiv.style.lineheight='110%';
descDiv.style.color='#333333';
descDiv.style.paddingLeft='16px';
descDiv.style.paddingRight='5px';
descDiv.style.height = "220px";
descDiv.style.background = 'cornsilk';
descDiv.innerHTML = showText;
};
var popup = function(){
var loc = location.href.match(/nicovideo\.jp\/watch/);
if(loc){
var iconList = $X('//div[@id="video_tags"]/p[@class="tag_txt"]/nobr/a/img[@class="txticon"]');
for( var i=0, l=iconList.length; i<l; i++ ) (function(list){
var link = list.parentNode.href;
list.addEventListener( 'mouseover', function(e){ showTips(e,link); }, false );
list.addEventListener( 'mouseout', function(){ hideTips(); }, false );
})(iconList[i]);
}
};
var checkOption = function(){
var loc =location.href.match(/popup=true/);
if( !loc ){
return false;
}
return true;
};
var showTips = function(e,link){
if( tip != null ){ hideTips(); };
tip = document.createElement("div");
var findPos = function(e){ return { "x":e.clientX, "y": e.clientY }; };
var pos = findPos(e);
tip.style.top = pos.y - 240 +'px';
tip.style.left = pos.x + 20 + 'px';
tip.style.width = '340px';
tip.style.height = '220px';
tip.style.zIndex = '10002';
tip.style.position = 'absolute';
tip.addEventListener( "click", hideTips, false );

var iframe = tip.appendChild(document.createElement("iframe"));
iframe.src = link + "?popup=true";
iframe.width = "340px";
iframe.height= "220px";
iframe.frameborder = "0";
iframe.scrolling = 'no';
iframe.style.zIndex = '10000';

var closeBox = tip.appendChild(document.createElement("div"));
closeBox.style.position = 'absolute';
closeBox.style.top = "2px";
closeBox.style.marginLeft = "2px";
closeBox.style.border = "sold 1px #fcfcfc";
closeBox.style.zIndex = "10003";
closeBox.style.width = '16px';
closeBox.style.height = '16px';
closeBox.style.background= '#333333';
closeBox.style.color= '#ffffff';
closeBox.style.fontWeight= 'bold';
closeBox.appendChild(document.createTextNode('X'));
closeBox.addEventListener( "click", hideTips, false );
document.body.appendChild(tip);

};
var hideTips = function(){
tip.parentNode.removeChild(tip);
tip = null;
}

//fire
var timer = setTimeout(init,1000);
})();

0 件のコメント :

コメントを投稿