-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (38 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const activeWindow = require('active-win');
const moment = require('moment');
const fs = require('fs');
let lastWindow = null;
const getActiveWindow = async () => {
const window = await activeWindow({});
// normalize
const path = window?.owner?.path?.replace(/\t/g, '_').replace(/\r\n/g, '_').replace(/\n/g, '_');
const title = window?.title?.replace(/\t/g, '_').replace(/\r\n/g, '_').replace(/\n/g, '_');
const fullTitle = path + '\t' + title;
if (!lastWindow) {
lastWindow = {
fullTitle: fullTitle,
startTime: getCurrentTime(),
endTime: getCurrentTime()
};
} else if (lastWindow.fullTitle !== fullTitle) {
const line = lastWindow.fullTitle + '\t' + lastWindow.startTime.format('HH:mm:ss') + '\t' + lastWindow.endTime.format('HH:mm:ss') + /*'\t' + JSON.stringify(window) +*/ '\n';
const lineToPrint = path + '__\\t__' + title + '__\\t__' + lastWindow.startTime.format('HH:mm:ss') + '__\\t__' + lastWindow.endTime.format('HH:mm:ss') + /*'\t' + JSON.stringify(window) +*/ '\n';
console.log(`Switched window. Writing to file: ${lineToPrint}`);
const dateStr = getCurrentTime().format('YYYY.MM.DD');
fs.promises.appendFile(`./test-${dateStr}.txt`, line);
lastWindow = {
fullTitle: fullTitle,
startTime: getCurrentTime(),
endTime: getCurrentTime()
};
} else {
console.log('Still on: ' + fullTitle);
lastWindow.endTime = getCurrentTime();
}
}
function getCurrentTime() {
return moment();
}
setInterval(() => {
getActiveWindow()
}, 10000);