I'm making an Electron auto-typer, and I'm using the module @nut-tree/nut-js to do my auto-typing.
I watched Fireship's video on setting up the electron app. Then, I created a file called typer.js and referenced it in HTML.
To test the auto-typer, I created a button in the HTML file to initialize the script (button has an ID of lol, don't ask). I then tested my auto-typer in (1) a text box in the HTML file and (2) a document in my browser.
The code works fine in the app. However, when I test it in my browser, it types the words really slowly. It should type a letter every 15 milliseconds, but in the browser it's more like a letter every second.
Here's the code:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<h1> Hello World!</h1>
<p>Welcome to your Electron application.</p>
<button type='button' id='lol'>submit</button>
<form>
<input type='text'></input>
</form>
<script defer src='typer.js'></script>
</body>
</html>
index.js (the file that initializes the Electron app, correct me if I said this wrong)
const { app, BrowserWindow } = require('electron');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
typer.js:
//just an async function to wait some milliseconds before executing another line
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
//the button that i was talking about
const lol = document.getElementById('lol');
//what i type
input =
`how to solve this problem`;
lol.onclick = async() => {
const { keyboard } = require('@nut-tree/nut-js');
keyboard.config.autoDelayMs = 15;
await delay(3000)
await keyboard.type(input);
}
Here's a video replay. (Oh, and uh, just realized I revealed my real name, but whatever)