f8g

ActiveX の Automagic

メモ。
http://d.hatena.ne.jp/arikui/20070411#1176217750

var Recog    = new ActiveXObject("Sapi.SpSharedRecognizer");
var Ctxt     = Recog.CreateRecoContext();
var GramDict = Ctxt.CreateGrammar(0);

Ctxt.EventInterests = 22;

GramDict.DictationLoad();
GramDict.DictationSetState(1);

function Ctxt::Recognition(StreamNum, StreamPos, RecogType, Result){
	var text = Result.PhraseInfo.GetText();
	alert(text);
}

これではエラーが出る。Ctxt::Recognition が function で定義されてるため、Ctxt のオブジェクトができる前に呼び出されてしまうから。ActiveXer にとっては常識なんでしょう、きっと。
多分こう書けば大丈夫。

<script type="text/javascript">
var Recog    = new ActiveXObject("Sapi.SpSharedRecognizer");
var Ctxt     = Recog.CreateRecoContext();
var GramDict = Ctxt.CreateGrammar(0);

Ctxt.EventInterests = 22;

GramDict.DictationLoad();
GramDict.DictationSetState(1);
</script>
</head>
<body>
<script type="text/javascript">
function Ctxt::Recognition(StreamNum, StreamPos, RecogType, Result){
	var text = Result.PhraseInfo.GetText();
	alert(text);
}
</script>
</body>

これだとjsファイルでやろうと思ったとき、2つ呼び出さなきゃならない、多分。

Ctxt::Recognition = function(StreamNum, StreamPos, RecogType, Result){
	var text = Result.PhraseInfo.GetText();
	alert(text);
}

これは文法エラー。

var Recog    = new ActiveXObject("Sapi.SpSharedRecognizer");
var Ctxt     = Recog.CreateRecoContext();
var GramDict = Ctxt.CreateGrammar(0);

Ctxt.EventInterests = 22;

GramDict.DictationLoad();
GramDict.DictationSetState(1);

(function(){
	function Ctxt::Recognition(StreamNum, StreamPos, RecogType, Result){
		var text = Result.PhraseInfo.GetText();
		alert(text);
	}
})()

これなら大丈夫。