Skip to content

Commit

Permalink
🚑 check process.stdin.setRawMode before setting up screenshot handler
Browse files Browse the repository at this point in the history
  • Loading branch information
smashah committed Mar 30, 2023
1 parent 591a4a7 commit 57c0d05
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ export async function initPage(sessionId?: string, config?:ConfigObject, qrManag
browser = await initBrowser(sessionId,config, spinner);
spinner?.info(`Browser launched: ${(now() - startBrowser).toFixed(0)}ms`)
waPage = await getWAPage(browser);
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
console.log('Press "s" to trigger an event. Press "Ctrl+C" to quit.');
process.stdin.on('keypress', async (str: string, key: readline.Key) => {
if (key.name === 's') {
const path = `./screenshot_${config.sessionId || "session"}_${Date.now()}.png`;
console.log(`Taking screenshot: ${path} ...`);
await waPage.screenshot({path})
}

if (key.ctrl && key.name === 'c') {
process.exit();
}
});
if(process.stdin.setRawMode && typeof process.stdin.setRawMode == "function") {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
console.log('Press "s" to take a screenshot. Press "Ctrl+C" to quit.');
process.stdin.on('keypress', async (str: string, key: readline.Key) => {
if (key.name === 's') {
const path = `./screenshot_${config.sessionId || "session"}_${Date.now()}.png`;
console.log(`Taking screenshot: ${path} ...`);
await waPage.screenshot({path})
}

if (key.ctrl && key.name === 'c') {
process.exit();
}
});
} else console.log("Unable to set raw mode on stdin. Keypress events will not be emitted. You cannot take a screenshot by pressing 's' during launch.")
}
//@ts-ignore
await (typeof waPage._client === 'function' && waPage._client() || waPage._client).send('Network.setBypassServiceWorker', {bypass: true})
Expand Down

0 comments on commit 57c0d05

Please sign in to comment.