2013年11月21日


IE拡張 : テキストエリア入力拡張





テキストエリアの内容を転送して、簡単な拡張編集ができます。( 簡易的なプレビューも可能です )

▼ IE に登録された画面を伴うコード部分
<meta http-equiv="content-type" content="text/html; charset=SHIFT_JIS">
<SCRIPT language="VBScript">
	Dim WshShell,RegName,strLocation,obj,doc,Fs
	Dim nFirstLen,objFirstTextRange,BakPath,bMax

	bMax = False

	Set WshShell = CreateObject("WScript.Shell")
	Set Fs = CreateObject( "Scripting.FileSystemObject" )

	RegName = "−【テキストエリア入力拡張】"

	BakPath = "C:\laylaClass\menuex"

	' *************************************************
	' ウインドウサイズ
	' *************************************************
	window.dialogWidth = "900px"
	window.dialogHeight = "610px"

'	window.dialogTop = "100px"
'	window.dialogLeft = (window.screen.width/2)&"px"

	on error resume next
	ExecuteGlobal "function dummy(): end function"
	on error goto 0

Function setObj( src )
	Set obj = src
End Function
</SCRIPT>

<SCRIPT language="JavaScript">
setObj(external.menuArguments.event.srcElement);
var shift_key;
var ctrl_key;

function easyPreview() {

	document.getElementById("preview").style.display = '';
	document.getElementById("text").style.height = '330px';
	document.getElementById("preclose").style.display = '';

	if ( bMax ) {
		
		document.getElementById("preview").style.height = (window.screen.height - 450) + "px";
	}

	var target = document.getElementById("preview").contentWindow.document;
	target.open;

	var str="";
str+="<style type=\"text/css\"> \n";
str+="* { \n";
str+="	font-size:12px; \n";
str+="} \n";
str+="</style> ";
	target.write(str);
str="";
str+="<"+"script type=\"text/javascript\"> \n";
str+="window.onerror = function() { \n";
str+="	return true; \n";
str+="} \n";
str+="</"+"script> ";
	target.write(str);

	if ( event.shiftKey ) {
		target.write("<pre>\n");
	}

	var cnvtext,cnvarr;

	if ( event.ctrlKey ) {
		cnvtext = document.getElementById("text").value;
		if ( event.shiftKey ) {
			cnvarr = cnvtext.match(/(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/g );
			cnvtext = "";
			for( i = 0; i < cnvarr.length; i++ ) {
				cnvtext += "<a href=" + cnvarr[i] + " target='_blank'>" + cnvarr[i] + "</a>\n";
			}
		}
		else {
			cnvtext = cnvtext.replace(/&/g, "&amp;" );
			cnvtext = cnvtext.replace(/</g, "&lt;" );
			cnvtext = cnvtext.replace(/>/g, "&gt;" );
			cnvtext = cnvtext.replace(/(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/g, "<a href='$1$2' target='_blank'>$1$2</a>" );
		}
	}
	else {
		cnvtext = document.getElementById("text").value;
	}

	target.write(cnvtext);

	if ( event.shiftKey ) {
		target.write("</pre>\n");
	}

	target.close();

}

function closePreview() {

	document.getElementById("preview").style.display="none";
	document.getElementById("preclose").style.display="none";
	document.getElementById("text").style.height = '550px';

	if ( bMax ) {
		document.getElementById("text").style.height = (window.screen.height - 120) + "px"
	}

}


function createURLLink() {

	var txt = "";
	var strUrl = "";
	var strTarget = "";

	var shift_key_local = event.shiftKey;

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		return;
	}
	try {
		if ( shift_key_local ) {
			strTarget = "_self"
		}
		else {
			strTarget = "_blank"
		}
		strUrl = external.menuArguments.prompt("URLリンク作成( " + strTarget + " )","http://");
		if ( strUrl != "" && strUrl != "http://" && strUrl+"" != "null" ) {
			txt = objTextRange.text
			txt = "<a href=\""+strUrl+"\" target=\"" + strTarget + "\">"+txt+"</a>"
			objTextRange.text = txt;
		}
	}catch( e ){}

}

function createIFrame() {

	document.getElementById("text").focus();
	setTimeout("createIFrameCore()", 100 );
}
function createIFrameCore() {

	var txt = "";
	var strUrl = "";
	var str;

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		try {
			strUrl = external.menuArguments.prompt("IFRAME作成","http://");
			if ( strUrl != "" && strUrl != "http://" && strUrl+"" != "null" ) {
				str="";
				str+="<"+"iframe \n";
				str+="	src=\""+strUrl+"\" \n";
				str+="	name=\"myframe\" \n";
				str+="	frameborder=\"no\" \n";
				str+="	scrolling=\"no\" \n";
				str+="	width=\"600\" \n";
				str+="	height=\"480\" \n";
				str+="></"+"iframe> \n";
				objTextRange.text = str;
			}
		}catch( e ){ }
	}
}

function createBold() {

	document.getElementById("text").focus();
	setTimeout("createBoldCore()", 100 );
}
function createBoldCore() {

	var txt = "";
	var strUrl = "";

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		try {
			objTextRange.text = "<b></b>";
		}catch( e ){}
		return;
	}
	try {
		txt = objTextRange.text;
		txt = "<b>"+txt+"</b>";
		objTextRange.text = txt;
	}catch( e ){}

}
function createTable() {

	document.getElementById("text").focus();
	setTimeout("createTableCore()", 100 );
}
function createTableCore() {

	var txt = "";
	var strUrl = "";

	var str1,str2;
str1="";
str1+="<table>\n";
str1+="<tr>\n";
str1+="<td>\n";
str1+="<!-- (left) -->\n";
str2="";
str2+=" \n";
str2+="</td> \n";
str2+="<td>\n";
str2+="<!-- (right) -->\n";
str2+="</td>\n";
str2+="</tr>\n";
str2+="</table>\n";

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		try {
			objTextRange.text = str1+str2;
		}catch( e ){}
		return;
	}
	try {
		txt = objTextRange.text;
		txt = str1+txt+str2;
		objTextRange.text = txt;
	}catch( e ){}

}
function convertHtml() {

	var txt = "";
	var strUrl = "";

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		return;
	}
	try {
		txt = objTextRange.text
		txt = txt.replace(/&/g, "&amp;" );
		txt = txt.replace(/</g, "&lt;" );
		txt = txt.replace(/>/g, "&gt;" );
		objTextRange.text = txt;
	}catch( e ){}

}
function decodeHtml() {

	var txt = "";
	var strUrl = "";

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		return;
	}
	try {
		txt = objTextRange.text
		txt = txt.replace(/&lt;/g, "<" );
		txt = txt.replace(/&gt;/g, ">" );
		txt = txt.replace(/&amp;/g,'&')
		objTextRange.text = txt;
	}catch( e ){}

}

function removeSpace() {

	var txt = "";
	var strUrl = "";

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		return;
	}
	try {
		txt = objTextRange.text
		txt = txt.replace(/\s+/g, " " );
		objTextRange.text = txt;
	}catch( e ){}

}


function insertPre() {


	shift_key = false
	if ( event.shiftKey ) {
		shift_key = true
	}
	document.getElementById("text").focus();
	setTimeout("insertPreCore()", 100 );
}
function insertPreCore() {

	var txt = "";

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		try {
			if ( shift_key ) {
				objTextRange.text = "<"+"script type=\"text/javascript\">\n\n\n</"+"script>\n";
			}
			else {
				objTextRange.text = "<pre>\n\n\n</pre>\n";
			}
		}catch( e ){}
	}
	else {
		try {
			txt = objTextRange.text
			if ( shift_key ) {
				txt = "<"+"script type=\"text/javascript\">\n"+txt+"\n</"+"script>\n"
			}
			else {
				txt = "<pre>\n"+txt+"\n</pre>"
			}
			objTextRange.text = txt;
		}catch( e ){}
	}

}

function insertBr() {

	document.getElementById("text").focus();
	setTimeout("insertBrCore()", 100 );
}
function insertBrCore() {

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		try {
			objTextRange.text = "<br />\n";
		}catch( e ){}
	}

}

function insertStyle() {

	shift_key = false;
	ctrl_key = false;
	if ( event.shiftKey ) {
		shift_key = true;
	}
	if ( event.ctrlKey ) {
		ctrl_key = true;
	}
	document.getElementById("text").focus();
	setTimeout("insertStyleCore()", 100 );
}
function insertStyleCore() {

	var objSelectedText = document.selection
	var objTextRange = objSelectedText.createRange( )

	if (objTextRange.text.length == 0 ) {
		try {
			if ( shift_key ) {
				objTextRange.text = " style='color:#0000ff'";
			}
			else {
				if ( ctrl_key ) {
					objTextRange.text = " style='color:#228b22'";
				}
				else {
					objTextRange.text = " style='color:#ff0000'";
				}
			}
		}catch( e ){}
	}

}

</SCRIPT>
<html>
<head>
<title>−【テキストエリア入力拡張】</title>
<meta http-equiv="content-type" content="text/html; charset=SHIFT_JIS">
<STYLE type="text/css">
* {
	font-size:12px;
}
body {
	margin:0;
	background-color:#C5D1A5;
}
</STYLE>

<SCRIPT language="VBScript">

' 関数定義用
Function ReturnText()

	if Not confirm( "データを転送しますか?   " ) then
		Exit Function
	end if

	if UCase( obj.tagName ) = "TEXTAREA" then
		if nFirstLen <> 0 then
			objFirstTextRange.text = document.getElementById("text").value
		else
			obj.value = document.getElementById("text").value
		end if
	end if

	if UCase( obj.tagName ) = "INPUT" then
		if UCase( obj.type ) = "TEXT" then
			if nFirstLen <> 0 then
				objFirstTextRange.text = document.getElementById("text").value
			else
				obj.value = document.getElementById("text").value
			end if
		end if
	end if

	on error resume next
	Set OutObj = Fs.OpenTextFile( BakPath & "\TextareaEx.bak", 2, True )
	OutObj.Write document.getElementById("text").value & ""
	OutObj.Close
	on error goto 0

	window.close()

End Function

function SetTab( )

	if window.event.keyCode <> 9 then
		exit function
	end if

	window.event.returnValue = false

	Dim objTextArea,objTextRange,nLen,nChars,strData

	set objTextArea = document.selection
	set objTextRange = objTextArea.createRange( )
	on error resume next
	strData = objTextRange.text
	nLen = Len( strData )
	if window.event.shiftKey then
		strData = Replace( strData, vbLf & vbTab, vbLf )
		if Left( strData, 1 ) = vbTab then
			strData = Right( strData, Len( strData ) - 1 )
		end if
	else
		strData = Replace( strData, vbLf, vbLf & vbTab )
		strData = vbTab & strData
	end if

	if nLen = 0 then
		objTextRange.text = strData
	else
		objTextRange.text = strData & vbLf
	end if

	on error goto 0

end function 

Function CursorTop()

	document.getElementById("text").focus()
	Set objFocus = document.getElementById("text").createTextRange()
	Call objFocus.collapse(True)

End Function


Dim objFocus
' **********************************************************
' サーチ
' **********************************************************
function TextSearch()

	if Trim(document.getElementById("search").value&"") = "" then
		Exit Function
	end if

	Dim objTextArea,objTextRange
	Set objTextArea = document.selection
	Set objTextRange = objTextArea.createRange( )

	if objTextRange.text = "" then
		Set objFocus = document.getElementById("text").createTextRange()
		if objFocus.findText( document.getElementById("search").value ) then
			objFocus.select()
		end if
	else
		objFocus.collapse( false )
		if objFocus.findText( document.getElementById("search").value ) then
			objFocus.select()
		end if
	end if

End Function

Function keyCheck(  )
	if window.event.keyCode = 27 then
		Call closePreview()
	end if

End Function

Function maxWindow()

	window.dialogLeft = "0"
	window.dialogTop = "0"
	window.dialogWidth = (window.screen.width-5) & "px"
	window.dialogHeight = (window.screen.height - 70) & "px"
	bMax = True
	document.getElementById("text").style.height = (window.screen.height - 120) & "px"
	document.getElementById("maxw").style.display = "none"

End Function
</SCRIPT>
<base target="_self">
</head>
<BODY onKeydown='keyCheck()'>
<INPUT
	id="retbutton"
	type="button"
	value="戻す"
	onClick="Call ReturnText()"
>
<INPUT
	id="search"
	type="text"
>
<INPUT
	type="button"
	value="検索"
	onClick="Call TextSearch()"
>
折り返さない <input
	type="checkbox"
	onClick='if me.checked then document.getElementById("text").wrap="off" else document.getElementById("text").wrap="soft" end if'
>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type=button value="リンク" language="javascript" onClick='createURLLink()'
	onMouseover='document.getElementById("message").value = "選択部分に対してURLを入力してリンクを作成します ( そのままクリックすると _blank で作成。SHIFTキーを押しながら開くと _self で作成)"'
	onMouseout='document.getElementById("message").value = ""'
>
<input type=button value="pre" language="javascript" onClick='insertPre()'>
<input type=button value="bold" language="javascript" onClick='createBold()'>
<input type=button value="table" language="javascript" onClick='createTable()'>
 / <input type=button value="色" language="javascript" onClick='insertStyle()'
	onMouseover='document.getElementById("message").value = "通常 : 赤、SHIFTキー : 青、CTRLキー : 緑"'
	onMouseout='document.getElementById("message").value = ""'
>
<input type=button value="br" language="javascript" onClick='insertBr()'>
<input type=button value="iframe" language="javascript" onClick='createIFrame()'
	onMouseover='document.getElementById("message").value = "IFRAME を挿入します"'
	onMouseout='document.getElementById("message").value = ""'
>
 / <input type=button value="&amp;lt;" language="javascript" onClick='convertHtml()'>
<input type=button value="HTML" language="javascript" onClick='decodeHtml()'>
<input type=button value="改行削除" language="javascript" onClick='removeSpace()'
	onMouseover='document.getElementById("message").value = "選択した文字列を、replace(/\s+/g, \" \" ) で置き換えます"'
	onMouseout='document.getElementById("message").value = ""'
>
 / <input type=button value="プレビュー" language="javascript" onClick='easyPreview()'
	onMouseover='document.getElementById("message").value = "SHIFT : PRE 要素内にセット、CTRL : URL 部分のみリンク変換、CTRL+SHIFT : URL部分のみ取り出してリンク変換"'
	onMouseout='document.getElementById("message").value = ""'
>

&nbsp;&nbsp;&nbsp;&nbsp;<INPUT style='' type="button" id="maxw" value="最大化" onClick='maxWindow()' >

<br>
<TEXTAREA
	id="text"
	name="text"
	style='width:100%;height:550px;'
	onKeydown='Call SetTab()'
></TEXTAREA>
<IFRAME
	id="preview"
	frameborder="yes"
	scrolling="yes"
	width="100%"
	height="205"
	style='display:none'
></IFRAME>
<INPUT style='display:none;float:right;' type="button" id="preclose" value="プレビュー結果を閉じる" onClick='closePreview()' >
<INPUT id="message" type=text style='border:none 0px #000000;width:700px;background-color:#C5D1A5' />

</BODY>
</html>
<SCRIPT for=window event=onload language="VBScript">

' onload 処理
Set doc = obj.document

set objTextArea = doc.selection
set objFirstTextRange = objTextArea.createRange( )
on error resume next
strData = objFirstTextRange.text
nFirstLen = Len( strData )
on error goto 0

if nFirstLen <> 0 then
	document.getElementById("text").value = strData
else
	strTag = obj.tagName
	if UCase( strTag ) = "A" then
		strWork = obj.innerText
		strWork = strWork & vbCrLf & obj.href
		document.getElementById("text").value = strWork
	else
		if UCase( obj.tagName ) = "TEXTAREA" then
			document.getElementById("text").value = obj.innerText
			strCommand = "document.getElementById(""text"").scrollTop = 0"
			Call window.setTimeout( strCommand, 100 )
			Call window.setTimeout( "Call CursorTop()", 500 )
		else
			if UCase( obj.tagName ) = "INPUT" then
				if UCase( obj.type ) = "TEXT" then
					document.getElementById("text").value = obj.value
				end if
			else
				document.getElementById("text").value = obj.innerHTML
			end if
		end if
	end if
end if

</SCRIPT>

関連する記事

Seesaa のカテゴリをソート(通常と逆順と記事が多い順)する VBScript で記述された IE 拡張


posted by at 21:07 | Comment(0) | IE拡張 | このブログの読者になる | 更新情報をチェックする


この記事へのコメント
コメントを書く
お名前:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。