ブラウザでは動的にscriptタグを追加するテクニックがありますが、HTAで試したらうまく動作しませんでした。
上記2つの理由により、HTAファイル内に記載したscriptをWSFファイルなどと共有するサンプルを作成しました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<title>外部スクリプト</title> | |
<body> | |
</body> | |
<!-- script id=scriptTAG src="script.js"></script>--> | |
<script> | |
resizeTo(200,200) | |
scr = document.createElement('script') | |
scr.src = 'script.js' | |
document.body.insertBefore(scr) | |
scr.onreadystatechange = function(){ | |
// onloadイベントは発生しなかった。 | |
// 「loading」しか発生しない。。 | |
confirm(scr.readyState) | |
// 試しに以下の行を無効化して以降の処理を継続させてみたが、fsというオブジェクトは宣言されていないというエラーが出てしまう。。 | |
if(scr.readyState!='complete'){return} | |
confirm('開始') | |
// pathScript = fs.GetAbsolutePathName(scriptTAG.src) | |
pathScript = fs.GetAbsolutePathName(scr.src) | |
path = gsf2+'\\wsf.wsf' | |
pathDQ = function(path){return 0<path.indexOf(' ') ? ('"'+path+'"') : path} | |
ws = fs.CreateTextFile(path) | |
ws.Write(['<-job>','<-script src="'+pathScript+'"><-/script>','<-script>','!'+function(){ WScript.Echo(gsf2) }+'()','<-/script>','<-/job>'].join('\r\n').replace(/<-/g,'<')) | |
ws.Close() | |
shell.Run('cmd /C '+pathDQ(path), 0, true) | |
fs.DeleteFile(path) | |
} | |
document.body.innerText = document.body.outerHTML | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AXO=function(name){return new ActiveXObject(name)} | |
fs = AXO('Scripting.FileSystemObject') | |
shell = AXO('WScript.Shell') | |
gsf2 = fs.GetSpecialFolder(2) |
onreadystatechangeイベントが一度しか発生せず、発生時のreadyStateの中身は「loding」でした。
以下、動いたソース。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<title>scriptCode共有</title> | |
<body> | |
</body> | |
<script id=scriptCode共有用> | |
AXO=function(name){return new ActiveXObject(name)} | |
fs = AXO('Scripting.FileSystemObject') | |
shell = AXO('WScript.Shell') | |
gsf2 = fs.GetSpecialFolder(2) | |
</script> | |
<script> | |
resizeTo(200,200) | |
path = gsf2+'\\wsf.wsf' | |
pathDQ = function(path){return 0<path.indexOf(' ') ? ('"'+path+'"') : path} | |
ws = fs.CreateTextFile(path) | |
ws.Write(['<-job>',scriptCode共有用.outerHTML,'<-script>','!'+function(){ WScript.Echo(gsf2) }+'()','<-/script>','<-/job>'].join('\r\n').replace(/<-/g,'<')) | |
ws.Close() | |
shell.Run('cmd /C '+pathDQ(path), 0, true) | |
fs.DeleteFile(path) | |
</script> | |
</html> |
ScriptControllを使う方法も検討しましたが、それだとメイン画面側でもScriptControll経由で各関数を呼ばなければならず不便なので、scriptタグにidをつけて共有用Codeを分ける形式にすることにしました。
0 件のコメント:
コメントを投稿