操作

MediaWiki

「Common.js」の版間の差分

提供: WikiArc

(ページの作成:「ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます: // initalize TXT_m = new TXT_...」)
 
70行目: 70行目:
 
left = "0px";
 
left = "0px";
 
visibility = "visible";
 
visibility = "visible";
 +
                font-size:12px;
 
}
 
}
 
div.innerHTML = '<div id="searchlayer" style="margin:4px 4px 4px 12px;padding:0px;">'
 
div.innerHTML = '<div id="searchlayer" style="margin:4px 4px 4px 12px;padding:0px;">'

2016年10月21日 (金) 16:46時点における版

/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */

// initalize
TXT_m = new TXT_mouse();
TXT_id = "__searchBoxWiki";
TXT_str = "";
TXT_timeout = null;
document.onmouseup = TXT_main;

// main
function TXT_main(e) {
	TXT_getMousePosition(e);
	
	if ( !e ) { e = event; }
	
	// left button ?
	var button;
	if ( e.button ) {
		button = e.button;
	} else {
		button = e.which;
	}
	if ( button != 1 ) { return; }
	
	// element check
	if ( TXT_elementCheck(e) ) {
		TXT_closeSearchBox();
		return;
	}

	// get word
	var str = String(TXT_getSelectedText(window));
	str = str.replace(/^\s+|\s+$/g, "");
	if ( str.match(/\r|\n/) ) {
		TXT_closeSearchBox();
		return;
	}
	str = str.replace(/[\r\n\s ]/g, " ").substr(0, 100);

	// same word ?
	if ( TXT_str == str ) { return; }

	// show search box
	TXT_closeSearchBox();
	if ( str != '' ) {
		TXT_openSearchBox(str, TXT_m.x - 20, TXT_m.y + 15);
		TXT_str = str;
	}
}

// set search box
function TXT_openSearchBox(str, x, y) {
	if ( TXT_timeout ) { clearTimeout(TXT_timeout); }
	
	// count bytes
	var num = 0;
	for(i = 0; i < str.length; i++) {
		(escape(str.charAt(i)).length > 4) ? (num += 2) : num++;
	}
	
	// set html
	var div = document.createElement("div");
	div.id = TXT_id;
	with(div.style) {
		border = "1px solid #666666";
		backgroundColor = "#FFFFCC";
		padding = "0px";
		position = "absolute";
		top = "-100px";
		left = "0px";
	visibility = "visible";
                font-size:12px;
	}
	div.innerHTML = '<div id="searchlayer" style="margin:4px 4px 4px 12px;padding:0px;">'
		+ str 
		+ ' を<br />'
		+ '<a href="http://www.google.co.jp/search'
		+ '?hl=ja&q='
		+ encodeURIComponent(str) + '" target="_blank">'
		+  ' Googleで検索</a><br />'
		+ '<a href="http://dic.search.yahoo.co.jp/search'
		+ '?ei=UTF-8&p='
		+ encodeURIComponent(str) 
		+ '&stype=prefix&fr=dic'
		+ '" target="_blank">'
		+ ' NET辞書で引く</a><br />'
		+ '<a href="http://ja.wikipedia.org/wiki/'
		+ encodeURIComponent(str) + '" target="_blank">'
		+ ' Wikipediaで引く</a><br />'		
		+ '<a href="http://labo.wikidharma.org/index.php/特別:Search?search='
		+ encodeURIComponent(str) + '" target="_blank">'
		+  ' 聖典(WikiArc)で引く</a></div>';
//bodyContent	
	document.body.appendChild(div);
	
	// flow check
	var pageWidth = TXT_getBrowserWidth() - 60;
	var w = (div.offsetWidth > pageWidth ? pageWidth : div.offsetWidth);
	if ( x + w > pageWidth ) {
		x = pageWidth - w + 30;
	}
	
	// appear
	with(div.style) {
		width = w + "px";
		top = y + "px";
		left = x + "px";
	}
	div.style.zIndex = 4;
//	 alert(document.getElementById(TXT_id).style.zIndex);
//document.getElementById(TXT_id).focus();
	// set timer
	TXT_timeout = setTimeout(TXT_closeSearchBox, 5000);
}

// close search box
function TXT_closeSearchBox() {
	if ( TXT_timeout ) { clearTimeout(TXT_timeout); }
	var obj = document.getElementById(TXT_id);
	if ( obj ) {
		document.body.removeChild(obj);
	}
	TXT_str = "";
}

// get mouse position
function TXT_getMousePosition(e) {
	var brw = new TXT_checkBrowser();
	TXT_m.scrollX = (document.body.scrollLeft || document.documentElement.scrollLeft);
	TXT_m.scrollY = (document.body.scrollTop || document.documentElement.scrollTop);
	if ( brw.ie ) {
		// IE
		TXT_m.x = event.clientX + TXT_m.scrollX;
		TXT_m.y = event.clientY + TXT_m.scrollY;
	} else if ( brw.ff || brw.moz ) {
		// Firefox & Mozilla
		TXT_m.x = e.pageX;
		TXT_m.y = e.pageY;
	} else if ( brw.saf ) {
		// Safari
		TXT_m.x = e.x;
		TXT_m.y = e.y;
	}
}

// get selected strings
function TXT_getSelectedText(obj) {
	var txt = '', i;
	try {
		if ( obj.document.selection ) {
			txt = obj.document.selection.createRange().text;
		} else if ( obj.getSelection ) {
			txt = obj.getSelection();
		} else if ( obj.document.getSelection ) {
			txt = obj.document.getSelection();
		} else {
			txt = '';
		}
	} catch(e) {}
	
	// search frames
	if ( txt == '' && obj.frames.length ) {
		for(i = 0; i < obj.frames.length; i++) {
			if ( (txt = TXT_getSelectedText(obj.frames[i])) != '' ) { break; }
		}
	}
	if ( txt == null ) { txt = ''; }
	
	return txt;
}

// mouse object
function TXT_mouse() {
	this.x = 0;
	this.y = 0;
	this.scrollX = 0;
	this.scrollY = 0;
}

// check browser object
function TXT_checkBrowser() {
	this.ie  = 0;
	this.ff  = 0;
	this.saf = 0;
	this.moz = 0;
	var ua = navigator.userAgent;
	if ( ( ua.indexOf("MSIE") > -1 ) ) { this.ie = 1; }
	else if ( ( ua.indexOf("Firefox") > -1 ) ) { this.ff = 1; }
	else if ( ( ua.indexOf("Safari") > -1 ) ) { this.saf = 1; }
	else if ( ( ua.indexOf("Mozilla") > -1 ) ) { this.moz = 1; }
	
	try {
		if ( event ) {
			this.ie = 1;
		}
	} catch(e) {}
}

// browser width
function TXT_getBrowserWidth() {
	if ( window.innerWidth ) {
		return window.innerWidth;
	} else if ( document.documentElement && document.documentElement.clientWidth ) {
		return document.documentElement.clientWidth;
	} else if ( document.body ) {
		return document.body.clientWidth;
	}
	return 0;
}

// element check
function TXT_elementCheck(e) {
	var tag;
	if ( e.srcElement ) {
		tag = e.srcElement.tagName;
	} else {
		tag = e.target.tagName;
	}
	var forbidden = new Array('INPUT', 'TEXTAREA', 'BUTTON');
	for(var i = 0; i < forbidden.length; i++) {
		if ( tag == forbidden[i] ) { return 1; }
	}
}