XPCOM: Javascript function call
Mozilla May 26th, 2007
Sometimes we need to call javascript function form a c++ xpcom. The following is a method for this.
1. Define your javascript callback interface in a IDL file.
1 2 3 | interface JSCallback : nsISupports { boolean call(in PRUint32 bogus, in PRUint32 aData); } |
And another interface looks like:
1 2 3 | interface TestJSCallback : nsISupports { void sum(in PRUint32 first, in PRUint32 second, in JSCallBack aCallback); } |
2. Complete the sum function’s cpp code.
1 2 3 4 5 6 7 8 | NS_IMETHODIMP TestJSCallback::Sum(PRUint32 first, PRUint32 second, JSCallback *aCallback) { PRBool ret = true; nsCOMPtr<JSCallback> js_callback = aCallback; js_callback->Call(0, first+second, &ret); return NS_OK; } |
3. Interface call.
1 2 3 4 5 6 | var test = function(sum) { alert(sum); }; var com = Components.classes["XXXXXX"].getService(Components.interfaces.TestJSCallback); com.sum(1, 2, test); |
It will pop up a dialog shows “3″ … Cheers…
Tags: JavaScript, xpcom
Yes, the last exam is finished. I will be graduate when undergraduate thesis project done~ This will take me so much time for it.
But, the next mission is the FireTray’s update.
TODO:
1. Fix display problem on KDE environment.
2. Add options for ‘always minimize to system tray’ and ‘close to system tray’.
3. Add a keyboard shortcut.
Tags: firetray
虽然有个 sb 补考横在中间, 但是这个五一还是过得蛮不错的. 不过话说回来, 似乎除了大二那次以外, 每年我的五一总是过得那么颓废… 除了吃, 玩游戏, 就不干嘛了… 估计已经被划入宅男的行列了…
不过, 这个假期还是做了些比较有意思的事的:
1. 俺的 FireTray 在这段时间又加了不少东西, 现在已经比较实用了. 不过不知道为什么, 在接口里定义的 AUTF8String 不能正常工作, 结果 Unicode 的支持也没办法… 还有就是不知道 GTK+ 的 minimize event 是啥, 所以有些常用的功能也没加. 14 号还有数字电路的补考, 然后毕业设计也得收尾, 估计这儿的开发就得往后拖了…
2. 俺总算见到了幽月大帅哥了~~ 俺当初的 WOW 五人固定小组的盗贼… 可惜真人似乎并不猥琐… 还一起去撮了顿 巫山烤全鱼, 真赞. 在这祝丫能进入国家电力下属的研究所工作吧, 这样大家可以常常 happy 了. 等资料片 <<迟到的远征>> 出了, 大家再一起奋战吧.
3. 看清楚了一些人, 一些事… 还是想说那句话: 不过如此, 不外如是… 现在想起来, 也很有意思啊…
Tags: firetray