Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Noitidart/5e37c86a6c6da1642344 to your computer and use it in GitHub Desktop.
Save Noitidart/5e37c86a6c6da1642344 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-FocusWindowHwndCTypes - Given a window's handle, this function focuses the window using WinAPI js-ctypes.
Cu.import('resource://gre/modules/ctypes.jsm');
var user32 = ctypes.open('user32.dll');
/* http://msdn.microsoft.com/en-us/library/ms633539%28v=vs.85%29.aspx
* BOOL WINAPI SetForegroundWindow(
* __in HWND hWnd
* );
*/
var SetForegroundWindow = user32.declare('SetForegroundWindow', ctypes.winapi_abi, ctypes.bool,
ctypes.int32_t
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522%28v=vs.85%29.aspx
* DWORD WINAPI GetWindowThreadProcessId(
* __in_ HWND hWnd,
* __out_opt_ LPDWORD lpdwProcessId
* );
*/
var GetWindowThreadProcessId = user32.declare('GetWindowThreadProcessId', ctypes.winapi_abi, ctypes.unsigned_long, //DWORD
ctypes.int32_t, //HWND
ctypes.unsigned_long.ptr //LPDWORD
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx
* HWND WINAPI GetLastActivePopup(
* __in HWND hWnd
* );
*/
var GetLastActivePopup = user32.declare('GetLastActivePopup', ctypes.winapi_abi, ctypes.int32_t, // HWND
ctypes.int32_t // HWND
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx
* BOOL WINAPI IsIconic(
* __in HWND hWnd
* );
*/
var IsIconic = user32.declare('IsIconic', ctypes.winapi_abi, ctypes.bool, // BOOL
ctypes.int32_t // HWND
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx
* BOOL WINAPI ShowWindow(
* __in HWND hWnd
* __in INT nCmdShow
* );
*/
var ShowWindow = user32.declare('ShowWindow', ctypes.winapi_abi, ctypes.bool, // BOOL
ctypes.int32_t, // HWND
ctypes.int // INT
);
var SW_RESTORE = 9;
var me = Services.ww.activeWindow;
me.setTimeout(function() {
console.log('execing now');
var tWin = Services.wm.getMostRecentWindow('navigator:browser'); // tWin means target_window
var tBaseWin = tWin.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIBaseWindow);
var cHwnd = ctypes.int32_t(ctypes.UInt64(tBaseWin.nativeHandle));
var lastActivePopupHwnd = cHwnd; //GetLastActivePopup(cHwnd);
console.log('lastActivePopupHwnd=', lastActivePopupHwnd, uneval(lastActivePopupHwnd), lastActivePopupHwnd.toString());
if (!lastActivePopupHwnd) {
throw new Error('failed to get hwnd of last active popup');
}
var rez = FocusWindow(lastActivePopupHwnd);
console.log('rez=', rez);
if (!rez) {
throw new Error('failed to focus window');
}
}, 5000);
function FocusWindow(hwnd) {
if (IsIconic(hwnd)) {
console.warn('its minimized so un-minimize it');
//its minimized so unminimize it
var rez = ShowWindow(hwnd, SW_RESTORE);
if (!rez) {
throw new Error('Failed to un-minimize window');
}
}
var rez = SetForegroundWindow(hwnd);
if (!rez) {
console.log('could not set to foreground window for a reason other than minimized, maybe process is not foreground, lets try that now');
var cPid = ctypes.cast(ctypes.voidptr_t(0), ctypes.unsigned_long);
var rez = GetWindowThreadProcessId(hwnd, cPid.address());
if (!rez) {
throw new Error('Failed to get PID');
} else {
console.log('trying to set pid to foreground process');
return false;
}
} else {
return rez;
}
}
@Noitidart
Copy link
Author

README

Rev1

  • Works if window is minimized
  • Does not work if process is not foreground, I'm trying to bring the process of window to foreground
Forked Rev1 to _ff-addon-snippet-FocusMostRecentWindowOfPID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment