JSCRIPTでGUIのパスワード入力フォームを生成する
前項の続きですが、結局はこれがやりたかったのです。
↓適当なファイル名(passwordsample.js)で保存して画面が出ればOK。
function createHtaWindow(){ var jscommand = "javascript:"; jscommand += "new ActiveXObject('InternetExplorer.Application');"; jscommand += "new ActiveXObject('Shell.Application').Windows().item(0).PutProperty('HtaWindowObject',window);"; for(var col = new Enumerator( new ActiveXObject("Shell.Application").Windows() ); ! col.atEnd(); col.moveNext()) { col.item().PutProperty("HtaWindowObject",undefined); } new ActiveXObject("WScript.Shell").Run("mshta.exe \""+jscommand+"\""); var hta = null; while(hta == null){ for(var col = new Enumerator( new ActiveXObject("Shell.Application").Windows() ); ! col.atEnd(); col.moveNext()) { if(typeof(col.item().GetProperty("HtaWindowObject"))!=undefined){ hta = col.item().GetProperty("HtaWindowObject"); col.item().PutProperty("HtaWindowObject",undefined); break; } } WScript.Sleep(500); } return hta; } function getHiddenInput(title,name,value){ var width = 350; var height = 130; var bgcolor = "#D4D0C8"; var ftsize = "9pt"; var hta=createHtaWindow(); hta.document.title=title; hta.resizeTo(width,height); hta.moveTo((hta.screen.width - width) / 2,(hta.screen.height - height) / 2); hta.document.onkeydown = function () { if (hta.event.keyCode == 116) { hta.event.keyCode = null; return false; } }; hta.document.oncontextmenu = function(){return false;}; hta.document.body.style.backgroundColor = bgcolor; hta.document.body.style.overflow = "hidden"; hta.document.body.style.borderStyle = "none"; hta.document.body.style.fontSize = ftsize; hta.document.body.style.padding = "0px"; var form = hta.document.createElement('FORM'); form.onsubmit = function(){ hta.document.getElementById('pressedButton').value=1; return false; }; hta.document.body.appendChild(form); var table1 = hta.document.createElement('TABLE'); var tablebody1 = hta.document.createElement("TBODY"); var tr1 = hta.document.createElement('TR'); var td1 = hta.document.createElement('TD'); var td2 = hta.document.createElement('TD'); table1.setAttribute("width","100%"); td1.setAttribute("align","left"); td1.style.verticalAlign = "top"; td2.setAttribute("align","right"); tr1.appendChild(td1); tr1.appendChild(td2); tablebody1.appendChild(tr1); table1.appendChild(tablebody1); form.appendChild(table1); var table2 = hta.document.createElement('TABLE'); var tablebody2 = hta.document.createElement("TBODY"); var tr2 = hta.document.createElement('TR'); var tr3 = hta.document.createElement('TR'); var td3 = hta.document.createElement('TD'); var td4 = hta.document.createElement('TD'); tr2.appendChild(td3); tr3.appendChild(td4); tablebody2.appendChild(tr2); tablebody2.appendChild(tr3); table2.appendChild(tablebody2); td2.appendChild(table2); var div = hta.document.createElement('DIV'); div.style.fontSize = ftsize; div.innerText = name; td1.appendChild(div); var button1 = hta.document.createElement('INPUT'); button1.type = "button"; button1.value = "OK"; button1.style.width = "80px"; button1.style.fontSize = ftsize; button1.onclick = function(){ hta.document.getElementById('pressedButton').value=1; } td3.appendChild(button1); var button2 = hta.document.createElement('INPUT'); button2.type = "button"; button2.value = "キャンセル"; button2.style.width = "80px"; button2.style.fontSize =ftsize; button2.onclick = function(){ hta.document.getElementById('pressedButton').value=-1; } td4.appendChild(button2); var input1 = hta.document.createElement('INPUT'); input1.type = "password"; input1.id = "password"; input1.value = (value!=undefined)?value:""; input1.style.fontSize =ftsize; input1.style.width = "100%"; form.appendChild(input1); var input2 = hta.document.createElement('INPUT'); input2.id = "pressedButton"; input2.type = "hidden"; input2.value = ""; form.appendChild(input2); hta.document.getElementById('password').focus() var rtn = null; try{ while (hta.document.getElementById("pressedButton").value == ""){ WScript.Sleep(100); } if(hta.document.getElementById("pressedButton").value == 1){ rtn = hta.document.getElementById("password").value; }else{ rtn = null; } hta.window.close(); }catch(e){} return rtn; } WScript.Echo(getHiddenInput("パスワード入力","パスワード","default"));