Firefox自举扩展:获取浏览器窗口的原生HWND句柄 [英] Firefox bootstrapped extension: Get native HWND handle of the browser window
问题描述
我有一个外部应用程序,我希望它在浏览器窗口的顶部显示一些信息。我的引导程序扩展需要将浏览器窗口句柄(原生HWND)传递给我的应用程序,以及有关窗口的其他一些有用信息。我能够做他们之间的沟通,唯一缺少的是获得Firefox窗口的本地HWND的方式。
我读了很多关于它,虽然我相信这是可能的,我找不到一个工作的解决方案。这是我迄今为止所尝试的:
这个应该给我 nsIBaseWindow
,所以我可以得到 nsIBaseWindow.nativeHandle
或 nsIBaseWindow.ParentNativeWindow
,但没有成功:
var window = SomeDOMWindow; // Informative
var baseWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIXULWindow)
.docShell
.QueryInterface(Components.interfaces.nsIBaseWindow);
上面的代码在论坛上广泛传播,但我无法让它为我工作。
另一个看起来不太准确,因为它根据窗口的类和标题得到HWND,这可能导致错误的结果:
Components.utils.import(resource://gre/modules/ctypes.jsm);
var lib = ctypes.open(user32.dll);
var fww = lib.declare(FindWindowW,ctypes.winapi_abi,
ctypes.voidptr_t,ctypes.jschar.ptr,ctypes.jschar.ptr);
var sfw = lib.declare(SetForegroundWindow,ctypes.winapi_abi,
ctypes.int32_t,ctypes.voidptr_t);
var hwnd = fww(MozillaWindowClass,document.title);
setTimeout(function(){
sfw(hwnd);
lib.close();
},3000);
任何帮助将不胜感激。
问题是我正在查询主题
param在 xul-window-registered错误的接口
观察者。我需要得到 nsIDOMWindow
而不是 nsIXULWindow
,所以我的问题中提到的第一个代码工作。所以现在我正在做下面的一些代码@Noit建议:
observe:function(subject,topic,数据){
var newWindow = subject.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
var basewindow = newWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface Ci.nsIInterfaceRequestor)
.nsIBaseWindow;
var nativehandle = basewindow.nativeHandle;
$ / code>
而且行得通!
非常感谢您的帮助。
I have an external application and I want it to display some information on top of the browser window. My bootstrapped extension needs to pass the browser window handle (native HWND) to my application, along with some other useful information about the window. I'm able to do the communication between them, the only thing that is missing is a way to get the native HWND of the Firefox window.
I read a lot about it and although I belive it's possible, I couldn't find a working solution. Here's what I've tried so far:
This one should give me nsIBaseWindow
, so I could get nsIBaseWindow.nativeHandle
or nsIBaseWindow.ParentNativeWindow
, but no success:
var window = SomeDOMWindow; // Informative
var baseWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIXULWindow)
.docShell
.QueryInterface(Components.interfaces.nsIBaseWindow);
The above code is widely spread on forums, but I couldn't get it to work for me.
The other one does not seem to be much accurate since it gets the HWND based on the window's class and title, which can lead to wrong results:
Components.utils.import("resource://gre/modules/ctypes.jsm");
var lib = ctypes.open("user32.dll");
var fww = lib.declare("FindWindowW", ctypes.winapi_abi,
ctypes.voidptr_t, ctypes.jschar.ptr, ctypes.jschar.ptr);
var sfw = lib.declare("SetForegroundWindow", ctypes.winapi_abi,
ctypes.int32_t, ctypes.voidptr_t);
var hwnd = fww("MozillaWindowClass", document.title);
setTimeout(function() {
sfw(hwnd);
lib.close();
}, 3000);
Any help would be appreciated.
The problem was that I was querying the wrong interface from the subject
param in the xul-window-registered
observer. I need to get an nsIDOMWindow
instead of an nsIXULWindow
so the first code mentioned in my question works. So now I'm doing the following, with some piece of code @Noit suggested:
observe: function(subject, topic, data) {
var newWindow = subject.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
var basewindow = newWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.nsIBaseWindow;
var nativehandle = basewindow.nativeHandle;
}
And it works!
Thank you very much for your help.
这篇关于Firefox自举扩展:获取浏览器窗口的原生HWND句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!