This project uses Rust and napi-rs to make it easy to obtain the active window or an array of open windows. It works on Microsoft Windows (10, 11), Linux (with X server), and macOS 10.6+.
# With npm
npm i @miniben90/x-win
# With yarn
yarn add @miniben90/x-win
exemple.ts
:
import { activeWindow, type WindowInfo } from '@miniben90/x-win';
const currentWindow: WindowInfo = activeWindow();
console.log(currentWindow);
response
:
{
id: 26148,
info: {
execName: "Code",
name: "Visual Studio Code",
path: "C:\\Users\\miniben\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe",
processId: 26148
},
os: "win32",
position: {
height: 1048,
width: 1936,
x: -8,
y: -8
},
title: "● README.md - x-win - Visual Studio Code",
usage: {
memory: 113270784
},
url: ""
}
You can also use asynchronous methods instead. To do so, you just have to use activeWindowAsync
, for example:
import { activeWindowAsync, type WindowInfo } from '@miniben90/x-win';
activeWindow()
.then((currentWindow: WindowInfo) => {
console.log(currentWindow);
});
exemple.ts
:
import { openWindows, type WindowInfo } from '@miniben90/x-win';
const windows: WindowInfo[] = openWindows();
console.log(windows);
response
:
[{
id: 26148,
info: {
execName: "Code",
name: "Visual Studio Code",
path: "C:\\Users\\miniben\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe",
processId: 26148
},
os: "win32",
position: {
height: 1048,
width: 1936,
x: -8,
y: -8
},
title: "● README.md - x-win - Visual Studio Code",
usage: {
memory: 113270784
},
url: ""
}]
You can also use asynchronous methods instead. To do so, you just have to use openWindowsAsync
, for example:
import { openWindowsAsync, type WindowInfo } from '@miniben90/x-win';
openWindowsAsync()
.then((windows: WindowInfo[]) => {
console.log(windows);
});
Thread will be start to check every 100ms
for a new active window (checking window title, window id and process id).
subscribeActiveWindow
: Create a subscription with a callback functionunsubscribeActiveWindow
: Remove a specific subscriptionunsubscribeAllActiveWindow
: Remove all threads
import { subscribeActiveWindow, unsubscribeActiveWindow, unsubscribeAllActiveWindow } from '@miniben90/x-win';
const a = subscribeActiveWindow(activeWindow => {
console.log('test a', activeWindow);
});
const b = subscribeActiveWindow(activeWindow => {
console.log('test b', activeWindow);
});
const c = subscribeActiveWindow(activeWindow => {
console.log('test c', activeWindow);
});
setTimeout(() => unsubscribeActiveWindow(c), 5000);
setTimeout(() => unsubscribeAllActiveWindow(), 10000);
Dependencies are required to be installed for development purposes.
sudo apt install libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxcb-shm0-dev
⚠️ Warning Recovery url is not available on linux
macOS requires you to grant access for screen recording. If your project does not have it, the title will be an empty value.
It is possible to get URL of browsers window for macOS and Windows.
Browser name | Tested |
---|---|
firefox | ✅ |
firefox developer edition | ✅ |
google chrome | ✅ |
microsoft edge | ✅ |
opera software - opera | ✅ |
opera software - opera GX | ✅ |
brave | |
vivaldi | |
iron | |
epic | |
chromium | ✅ |
ucozmedia | |
blisk | |
maxthon | |
beaker | |
beaker browser |
It will use AppleScript to get informations for chromium browsers and safari
For the moment Firefox and firefox developer edition are not supported
Browser name | Tested |
---|---|
Safari | ✅ |
Safari Technology Preview | |
google Chrome | ✅ |
google Chrome beta | |
google Chrome dev | |
google Chrome canary | |
brave Browser | |
brave Browser beta | |
brave Browser nightly | |
microsoft edge | ✅ |
microsoft edge Beta | |
microsoft edge Dev | |
microsoft edge Canary | |
mighty | |
ghost browser | |
bookry wavebox | |
pushplaylabs sidekick | |
opera software - Opera | ✅ |
opera software - OperaNext | |
opera software - OperaDeveloper | |
opera software - OperaGX | ✅ |
Vivaldi |
- To prevent potential crashes and issues, it's recommended to execute recovery operations within a worker thread (https://nodejs.org/api/worker_threads.html)
- For macOS, you can utilize functions to check and request screen permissions (https://www.electronjs.org/fr/docs/latest/api/system-preferences)
Project Inspirations:
- active-win
- active-win-pos-rs
- This project was generated with @napi-rs/cli