使用Firefox插件带来Firefox前台 [英] bring firefox window to the front using firefox addon

查看:133
本文介绍了使用Firefox插件带来Firefox前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 firefox addon .i创建一个可以关注 firefox 的.vbs文件c $ c> [引入顶部],然后使用 nsIProcess 来执行这个exe。像这样

  file.initWithPath( C:\\Users\\madhawax\\Documents\\focusFirefox.vbs); 
var process = Cc [@ mozilla.org/process/util;1\"].createInstance(Ci.nsIProcess);
process.init(file);
process.run(false,args,args.length);

工作正常。但是现在由于某种原因,我想直接从插件代码关注Firefox另一个应用程序。我读了firefox 窗口 api,但我cound't无论如何找到关注窗口。我问的是我怎么能从附加代码焦点浏览器。



编辑..

这里是我用来关注Firefox的代码。

p> focusFirefox.vbs

 设置wshShell = CreateObject( Shell)
wshShell.AppActivate(Firefox)


解决方案

如果我们可以做 SetForegroundWindow winapi方法,但调用此方法的过程必须具有用户焦点。如果没有,那么它什么都不做,所以这里是强制它的方法:

附加到线程方法(推荐方法)

这个方法执行以下操作:
$ b $ ol

  • 获取当前在前台的窗口

  • 如果没有找到,常规的 SetForegroundWindow 可以这样做,并退出

  • 如果前景中的窗口发现它获取它的线程的ID
  • 然后将它与当前firefox(这是目标windows线程)的线程ID进行比较

  • 如果它是相同的,它使用常规 SetForegroundWindow 并退出

  • 如果它不相同,那么它使用 AttachThreadInput 将firefox的线程连接到当前在前台窗口的线程
  • 如果在步骤6中的调用失败,则放弃并退出该函数,它会抛出一个错误

  • 如果第6步中的调用没有失败,则调用 SetForegroundWin因为它会立即工作

  • 然后解除在步骤6中完成的附件,因此如果您执行 SetForegroundWindow 现在调用它会失败,因为它通常应该

  • 我不能想到一个原因 AttachThreadInput 会失败,但是如果是这样的话,这是这个方法唯一的不可靠的部分。如果有人知道为什么会失败,请分享失败原因的解决方案。不过,我将下面的 SetCursorPos 方法作为紧急回退。

      Cu .IMPORT( '资源://gre/modules/Services.jsm'); 
    Cu.import('resource://gre/modules/ctypes.jsm');

    if(ctypes.voidptr_t.size == 4 / * 32-bit * /){
    var is64bit = false;
    } else if(ctypes.voidptr_t.size == 8 / * 64-bit * /){
    var is64bit = true;
    } else {
    throw new Error('huh ??? not 32 or 64 bit?!?!');
    }

    var user32 = ctypes.open('user32.dll');

    var GetForegroundWindow = user32.declare('GetForegroundWindow',ctypes.winapi_abi,
    ctypes.voidptr_t // return
    );

    var DWORD = ctypes.uint32_t;
    var LPDWORD = DWORD.ptr;

    var GetWindowThreadProcessId = user32.declare('GetWindowThreadProcessId',ctypes.winapi_abi,
    DWORD,//返回
    ctypes.voidptr_t,// hWnd
    LPDWORD // lpdwProcessId
    );
    $ b $ var AttachThreadInput = user32.declare('AttachThreadInput',ctypes.winapi_abi,
    ctypes.bool,//返回
    DWORD,// idAttach
    DWORD,/ / idAttachTo
    ctypes.bool // fAttach
    );

    SetForegroundWindow = user32.declare('SetForegroundWindow',ctypes.winapi_abi,
    ctypes.bool,// return BOOL
    ctypes.voidptr_t // HWND
    ) ;

    函数forceFocus(){

    var hToDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
    if(!hToDOMWindow){
    throw new Error('No browser window found');


    var hToBaseWindow = hToDOMWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIWebNavigation)
    .QueryInterface(Ci.nsIDocShellTreeItem)
    .treeOwner
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIBaseWindow);

    var hToString = hToBaseWindow.nativeHandle;
    var hTo = ctypes.voidptr_t(ctypes.UInt64(hToString));


    var hFrom = GetForegroundWindow();
    if(hFrom.isNull()){
    var rez_SetSetForegroundWindow = SetForegroundWindow(hTo);
    console.log('rez_SetSetForegroundWindow:',rez_SetSetForegroundWindow);
    返回true;


    if(hTo.toString()== hFrom.toString()){
    console.log('window has already focused');
    返回true;
    }

    var pid = GetWindowThreadProcessId(hFrom,null);
    console.info('pid:',pid);

    var _threadid = GetWindowThreadProcessId(hTo,null); // _threadid是我的firefox id的线程,而hTo是我的firefox id,所以可以做
    console.info('_ threadid:',_threadid);

    if(pid == _threadid){
    var rez_SetSetForegroundWindow = SetForegroundWindow(hTo);
    console.log('rez_SetSetForegroundWindow:',rez_SetSetForegroundWindow);
    返回true;


    var rez_AttachThreadInput = AttachThreadInput(_threadid,pid,true)
    console.info('rez_AttachThreadInput:',rez_AttachThreadInput);
    if(!rez_AttachThreadInput){
    throw new Error('failed to attach thread input');
    }
    var rez_SetSetForegroundWindow = SetForegroundWindow(hTo);
    console.log('rez_SetSetForegroundWindow:',rez_SetSetForegroundWindow);

    var rez_AttachThreadInput = AttachThreadInput(_threadid,pid,false)
    console.info('rez_AttachThreadInput:',rez_AttachThreadInput);

    $ b $ setTimeout(function(){
    forceFocus();
    user32.close();
    },5000);



    窃取然后恢复光标位置方法(不推荐方法 - 不是愚蠢的证明,因为它模仿点击 - 如果用户有全屏窗口并且链接位于窗口的位置0,0,它可以点击链接之类的东西)



    这是一个使用js - 类型,并且只能在Windows操作系统上运行,它会执行以下操作:
    $ b $ ol <

  • 将目标窗口设置为始终位于顶部焦点不在窗口中)

  • 获取当前光标位置

  • 将光标位置设置到窗口的左上角

  • 点击给予焦点

  • 将光标位置恢复到原来的状态

  • 它存在的问题是同步鼠标的x和y坐标点在 SendInput 没有被尊重,这就是为什么我必须使用 SetCursorPos ,在未来的修订中,我想修复这个问题,所以它不会使用 SetCursorPos ,因为移动用户鼠标对用户来说是烦人的,但是远远胜过轻微的烦恼,因为它非常重要的窗口焦点,我暂时接受这个 SetCursorPos 方法,因为我做了一个恢复光标位置之前偷,所以这里是: p>

      Cu.import('resource://gre/modules/Services.jsm'); 
    Cu.import('resource://gre/modules/ctypes.jsm');

    函数myFocus(){

    //////// START Ctypes DECLARES

    if(ctypes.voidptr_t.size == 4 / * 32位* /){
    var is64bit = false;
    } else if(ctypes.voidptr_t.size == 8 / * 64-bit * /){
    var is64bit = true;
    } else {
    throw new Error('huh ??? not 32 or 64 bit?!?!');
    }

    var user32 = ctypes.open('user32.dll');

    var SetWindowPos = user32.declare('SetWindowPos',ctypes.winapi_abi,
    ctypes.bool,//返回
    ctypes.voidptr_t,// hwnd
    ctypes .voidptr_t,// hWndInsertAfter
    ctypes.int,// X
    ctypes.int,// Y
    ctypes.int,// cx
    ctypes.int,// cy
    ctypes.unsigned_int // uFlags
    );

    var RECT = ctypes.StructType('_ RECT',[
    {left:ctypes.long},
    {top:ctypes.long},
    :ctypes.long},
    {bottom:ctypes.long}
    ]);
    var LPRECT = RECT.ptr;
    var GetWindowRect = user32.declare('GetWindowRect',ctypes.winapi_abi,
    ctypes.bool,//返回
    ctypes.voidptr_t,// hwnd
    LPRECT // lpRect
    );

    var SetCursorPos = user32.declare('SetCursorPos',ctypes.winapi_abi,
    ctypes.bool,//返回
    ctypes.int,// x
    ctypes .int // y
    );

    var POINT = ctypes.StructType('_ tagPoint',[
    {x:ctypes.long},
    {y:ctypes.long}
    ]);
    var LPPOINT = POINT.ptr;

    var GetCursorPos = user32.declare('GetCursorPos',ctypes.winapi_abi,
    ctypes.bool,//返回
    LPPOINT // lpPoint
    );

    //发送鼠标的东西
    var ULONG_PTR = is64bit? ctypes.uint64_t:ctypes.unsigned_long;
    var DWORD = ctypes.uint32_t;
    var MOUSEINPUT = ctypes.StructType('tagMOUSEINPUT',[
    {'dx':ctypes.long},
    {'dy':ctypes.long},
    { $ d $'$'$ d $'$'$'$'$'$'$'$'$'$'$'$'

    var INPUT = ctypes.StructType('tagINPUT',[
    {'type':DWORD},
    {'mi':MOUSEINPUT} // union,选择哪一个你想要的,我们要键盘输入
    ]);
    var LPINPUT = INPUT.ptr;

    SendInput = user32.declare('SendInput',ctypes.winapi_abi,ctypes.unsigned_int,ctypes.unsigned_int,LPINPUT,ctypes.int);

    var INPUT_MOUSE = 0;
    var MOUSEEVENTF_LEFTDOWN = 2;
    var MOUSEEVENTF_LEFTUP = 4;
    var MOUSEEVENTF_ABSOLUTE = 0x8000;
    //结束发送鼠标的东西

    var HWND_TOP = ctypes.voidptr_t(-1); //ctypes.cast(ctypes.int(-1),ctypes.voidptr_t);
    var HWND_NOTOPMOST = ctypes.voidptr_t(-2);
    var SWP_NOMOVE = 2;
    var SWP_NOSIZE = 1;

    //////// END Ctypes DECLARES


    //选择一个我们的导航器:浏览器的Firefox窗口来关注
    var browserWindow = Services.wm.getMostRecentWindow('navigator:browser');
    if(!browserWindow){
    throw new Error('No browser window found');


    //将我们的DOMWindow转换为HWND
    var baseWindow = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIWebNavigation)
    .QueryInterface(Ci.nsIDocShellTreeItem)
    .treeOwner
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIBaseWindow);
    var hwndString = baseWindow.nativeHandle;
    var hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString));


    browserWindow.focus(); / /这是很重要的,与此,我不知道为什么,但窗口将不会成为从下一行的SetWindowPos代码始终在上面
    var rez_SetWindowPos = SetWindowPos(hwnd,HWND_TOP,0,0, 0,0,SWP_NOSIZE | SWP_NOMOVE);
    console.log('rez_SetWindowPos:',rez_SetWindowPos);

    var myRect = RECT();
    var rez_GetWindowRect = GetWindowRect(hwnd,myRect.address());
    console.log('rez_SetWindowPos:',rez_SetWindowPos);

    var myRectLeft = parseInt(myRect.left.toString());
    var myRectTop = parseInt(myRect.top.toString());
    console.log('myRect.left',myRectLeft);
    console.log('myRect.top',myRectTop);

    var myPoint = POINT();
    var rez_GetCursorPos = GetCursorPos(myPoint.address());
    console.log('rez_GetCursorPos:',rez_GetCursorPos);
    var myPointX = parseInt(myPoint.x.toString());
    var myPointY = parseInt(myPoint.y.toString());
    console.log('myPoint.x',myPointX);
    console.log('myPoint.y',myPointY);

    var rez_SetCursorPos = SetCursorPos(myRect.left,myRect.top);
    console.log('rez_SetWindowPos:',rez_SetWindowPos);

    //可能需要等待窗口才能到达
    //发送点击 - 我不知道为什么,但是这些发送点击的x和y坐标不被尊重,只是点击鼠标的地方,所以即时通讯使用SetCursorPos作为临时破解
    var js_pInputs = [
    INPUT(INPUT_MOUSE,MOUSEINPUT(myRectLeft,myRectTop,0,MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE,0,0)) ,
    INPUT(INPUT_MOUSE,MOUSEINPUT(myRectLeft,myRectTop,0,MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE,0,0))
    ];

    var pInputs = INPUT.array()(js_pInputs);

    var rez_SI = SendInput(pInputs.length,pInputs,INPUT.size);
    console.log('rez_SI:',rez_SI.toString());
    // end send click

    var rez_SetCursorPos = SetCursorPos(myPoint.x,myPoint.y);
    console.log('rez_SetWindowPos:',rez_SetWindowPos);

    var rez_SetWindowPos = SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE);
    console.log('rez_SetWindowPos:',rez_SetWindowPos);

    user32.close();

    $ b setTimeout(function(){
    myFocus();
    },5000);

    这是所需要的方法,因为 SetForegroundWindow

    经过测试,即使另一个窗口被设置为永远在上面因为其他窗口总是在最上面,所以在这个窗口焦点之后,这个窗口就是最上面的了。


    i want to focus firefox when a specific function run on my firefox addon .i created a .vbs file which can focus firefox[bring to the top] and then i execute that exe using nsIProcess.like this

    file.initWithPath("C:\\Users\\madhawax\\Documents\\focusFirefox.vbs");
    var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
    process.init(file);
    process.run(false, args, args.length);
    

    it works fine.but now for some reason i want to focus Firefox directly from addon code without help of a another application . i read firefox windows api but i coudn't find anyway to focus window.what i'm asking is how can i focus browser from addon code.

    edit ..

    here is the code i used to focus firefox .

    focusFirefox.vbs

    Set wshShell = CreateObject("WScript.Shell")
    wshShell.AppActivate("Firefox")
    

    解决方案

    These solutions use js-ctypes winapi. This is a Windows solution. OSX and Linux don't have this issue, it is easy for steal focus there.

    It would be easy if we could just do SetForegroundWindow winapi method, however the process calling this method must have user focus. If it does not, then it does nothing, so here are the ways to force it:

    Attach to thread method (recommended method)

    This method does the following:

    1. Gets the window currently in the foreground
    2. If none found, the the regular SetForegroundWindow will work so it does that and quits
    3. If window in foreground found it then gets the id of its thread
    4. It then compares that to the thread id of the current firefox (which is the target windows thread)
    5. If it is same then it uses regular SetForegroundWindow and quits
    6. If it is not same then it uses AttachThreadInput to attach the firefox's thread to the thread of the currently in foreground window
    7. If call in step 6 fails, then it gives up and quits the function, it throws an error
    8. If call in step 6 doesn't fail then it calls SetForegroundWindow as it WILL work now
    9. It then undoes the attachment done in step 6, so if you do a SetForegroundWindow call now it will fail as it normally should

    I can't think of a reason AttachThreadInput will fail, but if it does, that is the only non-foolproof part of this method. If anyone knows why that will fail then please share a solution for that failure reason. However I left the SetCursorPos method below as an emergency fallback.

    Cu.import('resource://gre/modules/Services.jsm');
    Cu.import('resource://gre/modules/ctypes.jsm');
    
    if (ctypes.voidptr_t.size == 4 /* 32-bit */ ) {
        var is64bit = false;
    } else if (ctypes.voidptr_t.size == 8 /* 64-bit */ ) {
        var is64bit = true;
    } else {
        throw new Error('huh??? not 32 or 64 bit?!?!');
    }
    
    var user32 = ctypes.open('user32.dll');
    
    var GetForegroundWindow = user32.declare('GetForegroundWindow', ctypes.winapi_abi,
        ctypes.voidptr_t // return
    );
    
    var DWORD = ctypes.uint32_t;
    var LPDWORD = DWORD.ptr;
    
    var GetWindowThreadProcessId = user32.declare('GetWindowThreadProcessId', ctypes.winapi_abi,
        DWORD, // return
        ctypes.voidptr_t, // hWnd
        LPDWORD // lpdwProcessId
    );
    
    var AttachThreadInput = user32.declare('AttachThreadInput', ctypes.winapi_abi,
        ctypes.bool, // return
        DWORD, // idAttach
        DWORD, // idAttachTo
        ctypes.bool // fAttach
    );
    
    var SetForegroundWindow = user32.declare('SetForegroundWindow', ctypes.winapi_abi,
        ctypes.bool, // return BOOL
        ctypes.voidptr_t // HWND
    );
    
    function forceFocus() {
    
        var hToDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
        if (!hToDOMWindow) {
            throw new Error('No browser window found');
        }
    
        var hToBaseWindow = hToDOMWindow.QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIWebNavigation)
            .QueryInterface(Ci.nsIDocShellTreeItem)
            .treeOwner
            .QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIBaseWindow);
    
        var hToString = hToBaseWindow.nativeHandle;
        var hTo = ctypes.voidptr_t(ctypes.UInt64(hToString));
    
    
        var hFrom = GetForegroundWindow();
        if (hFrom.isNull()) {
            var rez_SetSetForegroundWindow = SetForegroundWindow(hTo);
            console.log('rez_SetSetForegroundWindow:', rez_SetSetForegroundWindow);
            return true;
        }
    
        if (hTo.toString() == hFrom.toString()) {
            console.log('window is already focused');
            return true;
        }
    
        var pid = GetWindowThreadProcessId(hFrom, null);
        console.info('pid:', pid);
    
        var _threadid = GetWindowThreadProcessId(hTo, null); // _threadid is thread of my firefox id, and hTo is that of my firefox id so this is possible to do
        console.info('_threadid:', _threadid);
    
        if (pid == _threadid) {
            var rez_SetSetForegroundWindow = SetForegroundWindow(hTo);
            console.log('rez_SetSetForegroundWindow:', rez_SetSetForegroundWindow);
            return true;
        }
    
        var rez_AttachThreadInput = AttachThreadInput(_threadid, pid, true)
        console.info('rez_AttachThreadInput:', rez_AttachThreadInput);
        if (!rez_AttachThreadInput) {
            throw new Error('failed to attach thread input');
        }
        var rez_SetSetForegroundWindow = SetForegroundWindow(hTo);
        console.log('rez_SetSetForegroundWindow:', rez_SetSetForegroundWindow);
    
        var rez_AttachThreadInput = AttachThreadInput(_threadid, pid, false)
        console.info('rez_AttachThreadInput:', rez_AttachThreadInput);
    }
    
    setTimeout(function() {
        forceFocus();
        user32.close();
    }, 5000);
    

    Steal then restore cursor position method (not recommended method - is not fool proof, and since it simulates click - it may click on something like a link if user has window in fullscreen and the link was at position 0,0 of the window)

    This is a solution that uses js-ctypes and works only on Windows operating system, that does the following:

    1. Sets the target window to be "always on top" (but focus is not in the window yet)
    2. Gets the current cursor position
    3. Sets the cursor position to the top left of the window
    4. Clicks to give it focus
    5. Restores the cursor position back to what it was
    6. Sets the window back to normal (not "always on top")

    It has the issue where the x and y coords of the syntehsized mouse click in SendInput are not being respected, that's why I had to use SetCursorPos, in a future revision I want to fix this up so it doesn't use SetCursorPos as moving the users mouse is annoying to the user, but the benefit far outweights the slight annoyance, because its very important to give the window focus, I am temporarily accepting this SetCursorPos method because I do a "restore cursor position to before stealing", so here it is:

    Cu.import('resource://gre/modules/Services.jsm');
    Cu.import('resource://gre/modules/ctypes.jsm');
    
    function myFocus() {
    
        //////// START Ctypes DECLARES
    
        if (ctypes.voidptr_t.size == 4 /* 32-bit */ ) {
            var is64bit = false;
        } else if (ctypes.voidptr_t.size == 8 /* 64-bit */ ) {
            var is64bit = true;
        } else {
            throw new Error('huh??? not 32 or 64 bit?!?!');
        }
    
        var user32 = ctypes.open('user32.dll');
    
        var SetWindowPos = user32.declare('SetWindowPos', ctypes.winapi_abi,
            ctypes.bool, //return
            ctypes.voidptr_t, //hwnd
            ctypes.voidptr_t, //hWndInsertAfter
            ctypes.int, //X
            ctypes.int, //Y
            ctypes.int, //cx
            ctypes.int, //cy
            ctypes.unsigned_int //uFlags
        );
    
        var RECT = ctypes.StructType('_RECT', [
            {left: ctypes.long},
            {top: ctypes.long},
            {right: ctypes.long},
            {bottom: ctypes.long}
        ]);
        var LPRECT = RECT.ptr;
        var GetWindowRect = user32.declare('GetWindowRect', ctypes.winapi_abi,
            ctypes.bool, // return
            ctypes.voidptr_t, // hwnd
            LPRECT // lpRect
        );
    
        var SetCursorPos = user32.declare('SetCursorPos', ctypes.winapi_abi,
            ctypes.bool, // return
            ctypes.int, // x
            ctypes.int // y
        );
    
        var POINT = ctypes.StructType('_tagPoint', [
            {x: ctypes.long},
            {y: ctypes.long}
        ]);
        var LPPOINT = POINT.ptr;
    
        var GetCursorPos = user32.declare('GetCursorPos', ctypes.winapi_abi,
            ctypes.bool, // return
            LPPOINT // lpPoint
        );
    
        // send mouse stuff
        var ULONG_PTR = is64bit ? ctypes.uint64_t : ctypes.unsigned_long;
        var DWORD = ctypes.uint32_t;
        var MOUSEINPUT = ctypes.StructType('tagMOUSEINPUT', [
            {'dx': ctypes.long},
            {'dy': ctypes.long},
            {'mouseData': DWORD},
            {'dwFlags': DWORD},
            {'time': ULONG_PTR},
            {'dwExtraInfo': DWORD}
        ]);
    
        var INPUT = ctypes.StructType('tagINPUT', [
            {'type': DWORD},
            {'mi': MOUSEINPUT} // union, pick which one you want, we want keyboard input
        ]);
        var LPINPUT = INPUT.ptr;
    
        var SendInput = user32.declare('SendInput', ctypes.winapi_abi, ctypes.unsigned_int, ctypes.unsigned_int, LPINPUT, ctypes.int);
    
        var INPUT_MOUSE = 0;
        var MOUSEEVENTF_LEFTDOWN = 2;
        var MOUSEEVENTF_LEFTUP = 4;
        var MOUSEEVENTF_ABSOLUTE = 0x8000;
        // end send mouse stuff
    
        var HWND_TOP = ctypes.voidptr_t(-1); //ctypes.cast(ctypes.int(-1), ctypes.voidptr_t);
        var HWND_NOTOPMOST = ctypes.voidptr_t(-2);
        var SWP_NOMOVE = 2;
        var SWP_NOSIZE = 1;
    
        //////// END Ctypes DECLARES
    
    
        // pick a one of our navigator:browser firefox windows to focus
        var browserWindow = Services.wm.getMostRecentWindow('navigator:browser');
        if (!browserWindow) {
            throw new Error('No browser window found');
        }
    
        // convert our DOMWindow to a HWND
        var baseWindow = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIWebNavigation)
            .QueryInterface(Ci.nsIDocShellTreeItem)
            .treeOwner
            .QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIBaseWindow);
        var hwndString = baseWindow.nativeHandle;
        var hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString));
    
    
        browserWindow.focus(); // this is important, withou this, i dont know why, but the window will not become "always on top" from the SetWindowPos code on the next line
        var rez_SetWindowPos = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
        console.log('rez_SetWindowPos:', rez_SetWindowPos);
    
        var myRect = RECT();
        var rez_GetWindowRect = GetWindowRect(hwnd, myRect.address());
        console.log('rez_SetWindowPos:', rez_SetWindowPos);
    
        var myRectLeft = parseInt(myRect.left.toString());
        var myRectTop = parseInt(myRect.top.toString());
        console.log('myRect.left', myRectLeft);
        console.log('myRect.top', myRectTop);
    
        var myPoint = POINT();
        var rez_GetCursorPos = GetCursorPos(myPoint.address());
        console.log('rez_GetCursorPos:', rez_GetCursorPos);
        var myPointX = parseInt(myPoint.x.toString());
        var myPointY = parseInt(myPoint.y.toString());
        console.log('myPoint.x', myPointX);
        console.log('myPoint.y', myPointY);
    
        var rez_SetCursorPos = SetCursorPos(myRect.left, myRect.top);
        console.log('rez_SetWindowPos:', rez_SetWindowPos);
    
        // may need to wait for the window to come to top
        // send click - i dont know why but the x and y coords of these send clicks is not being respected, it is just clicking where the mouse is, so im having to use SetCursorPos as temp hack
        var js_pInputs = [
            INPUT(INPUT_MOUSE, MOUSEINPUT(myRectLeft, myRectTop, 0, MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE, 0, 0)),
            INPUT(INPUT_MOUSE, MOUSEINPUT(myRectLeft, myRectTop, 0, MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 0, 0))
        ];
    
        var pInputs = INPUT.array()(js_pInputs);
    
        var rez_SI = SendInput(pInputs.length, pInputs, INPUT.size);
        console.log('rez_SI:', rez_SI.toString());
        // end send click
    
        var rez_SetCursorPos = SetCursorPos(myPoint.x, myPoint.y);
        console.log('rez_SetWindowPos:', rez_SetWindowPos);
    
        var rez_SetWindowPos = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
        console.log('rez_SetWindowPos:', rez_SetWindowPos);
    
        user32.close();
    
    }
    setTimeout(function() {
        myFocus();
    }, 5000);
    

    This is the approach that was needed, because SetForegroundWindow does nothing if the process calling it is not the currently focused one.

    Tested and this works even if another window is set to be "always on top", but because the other window is always on top, that will be the topmost after focus this window.

    这篇关于使用Firefox插件带来Firefox前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆