active-window-listener
Advanced tools
Comparing version 1.0.1 to 1.0.2
const ActiveWindowListener = require("./main"); | ||
const listener = new ActiveWindowListener(["Discord.exe", "Telegram.exe", "Code.exe"]); | ||
const listener = new ActiveWindowListener.ProcessListen(["Discord.exe", "Telegram.exe", "Code.exe"]); | ||
@@ -10,1 +10,4 @@ listener.changed(data => { | ||
console.log(ActiveWindowListener.getWindows()) |
130
main.js
@@ -5,70 +5,82 @@ | ||
module.exports = class ProcessListen { | ||
module.exports = { | ||
ProcessListen: class ProcessListen { | ||
constructor(processArr) { | ||
this.eventEmitter = new EventEmitter.EventEmitter(); | ||
this.processArr = processArr; | ||
this.activeWindow = undefined; | ||
this.openedProcesses = []; | ||
setTimeout(() => { | ||
this.loop(true); | ||
}); | ||
} | ||
changed(cb) { | ||
this.eventEmitter.on("changed", data => { | ||
if (!data) return cb(undefined); | ||
cb({ | ||
title: data.getTitle(), | ||
pid: data.processId, | ||
constructor(processArr) { | ||
this.eventEmitter = new EventEmitter.EventEmitter(); | ||
this.processArr = processArr; | ||
this.activeWindow = undefined; | ||
this.openedProcesses = []; | ||
setTimeout(() => { | ||
this.loop(true); | ||
}); | ||
} | ||
changed(cb) { | ||
this.eventEmitter.on("changed", data => { | ||
if (!data) return cb(undefined); | ||
cb({ | ||
title: data.getTitle(), | ||
pid: data.processId, | ||
}) | ||
}) | ||
}) | ||
} | ||
async loop(started) { | ||
const processes = nodeProcessWindows.windowManager.getWindows(); | ||
const newOpenedProcesses = this.processArr.map(pa => { | ||
return processes.find(p => { | ||
return p.path.indexOf(pa, p.path.length - pa.length) != -1; | ||
}) | ||
}).filter(pa => pa) | ||
const closedProgramsArr = this.openedProcesses.filter(x => { | ||
return !newOpenedProcesses.find(a => a.path === x.path) | ||
}); | ||
if (started) { | ||
if (newOpenedProcesses.length) { | ||
this.eventEmitter.emit('changed', newOpenedProcesses[0]); | ||
this.activeWindow = newOpenedProcesses[0]; | ||
} | ||
} | ||
closedProgramsArr.forEach(cp => { | ||
if (cp.path === this.activeWindow.path) { | ||
async loop(started) { | ||
const processes = nodeProcessWindows.windowManager.getWindows(); | ||
const newOpenedProcesses = this.processArr.map(pa => { | ||
return processes.find(p => { | ||
return p.path.indexOf(pa, p.path.length - pa.length) != -1; | ||
}) | ||
}).filter(pa => pa) | ||
const closedProgramsArr = this.openedProcesses.filter(x => { | ||
return !newOpenedProcesses.find(a => a.path === x.path) | ||
}); | ||
if (started) { | ||
if (newOpenedProcesses.length) { | ||
this.eventEmitter.emit('changed', newOpenedProcesses[0]); | ||
this.activeWindow = newOpenedProcesses[0]; | ||
} else { | ||
this.eventEmitter.emit('changed', undefined); | ||
this.activeWindow = undefined | ||
} | ||
} | ||
closedProgramsArr.forEach(cp => { | ||
if (cp.path === this.activeWindow.path) { | ||
if (newOpenedProcesses.length) { | ||
this.eventEmitter.emit('changed', newOpenedProcesses[0]); | ||
this.activeWindow = newOpenedProcesses[0]; | ||
} else { | ||
this.eventEmitter.emit('changed', undefined); | ||
this.activeWindow = undefined | ||
} | ||
} | ||
}) | ||
this.openedProcesses = newOpenedProcesses; | ||
const activeWindow = nodeProcessWindows.windowManager.getActiveWindow(); | ||
if ((!this.activeWindow || this.activeWindow.processId !== activeWindow.processId) && this.processArr.find(pa => activeWindow.path.indexOf(pa, activeWindow.path.length - pa.length) != -1)) { | ||
this.eventEmitter.emit('changed', activeWindow); | ||
this.activeWindow = activeWindow; | ||
} | ||
setTimeout(() => this.loop(), 5000); | ||
} | ||
}, | ||
getWindows: function getWindows() { | ||
const filteredFolder = ["Windows"]; | ||
const windowsArr = nodeProcessWindows.windowManager.getWindows().filter((window, index, self) => { | ||
const path = window.path.split('\\'); | ||
if (window.getTitle() === "") return false; | ||
if (path[1] && filteredFolder.includes(path[1])) return false; | ||
return index === self.findIndex(w => (w.path === window.path)) | ||
}) | ||
this.openedProcesses = newOpenedProcesses; | ||
const activeWindow = nodeProcessWindows.windowManager.getActiveWindow(); | ||
if ((!this.activeWindow || this.activeWindow.processId !== activeWindow.processId) && this.processArr.find(pa => activeWindow.path.indexOf(pa, activeWindow.path.length - pa.length) != -1)) { | ||
this.eventEmitter.emit('changed', activeWindow); | ||
this.activeWindow = activeWindow; | ||
} | ||
setTimeout(() => this.loop(), 1000); | ||
return windowsArr | ||
} | ||
} | ||
} |
{ | ||
"name": "active-window-listener", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Listen to active windows by specifying them", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
@@ -10,3 +10,3 @@ # Active Window Listener | ||
const listener = new ActiveWindowListener(["Discord.exe", "Telegram.exe", "Code.exe"]); | ||
const listener = new ActiveWindowListener.ProcessListen(["Discord.exe", "Telegram.exe", "Code.exe"]); | ||
@@ -24,5 +24,12 @@ listener.changed(data => { | ||
``` | ||
There is also a function to get windows that are opened. I needed to filter out some of the components from the "Windows" Directory in order to get the proper list. | ||
```js | ||
const ActiveWindowListener = require("active-window-listener"); | ||
console.log(ActiveWindowListener.getWindows()) | ||
``` | ||
This should output an array of Window Class properties. See the package used below to view the methods. | ||
## Package used | ||
This package was mainly used to get all the active window information. | ||
https://github.com/sentialx/node-window-manager/ |
4697
74
34