diff --git a/apps/desktop/src/autofill/main/main-desktop-autotype.service.ts b/apps/desktop/src/autofill/main/main-desktop-autotype.service.ts index 4e137ae411e..592d1e1b7a8 100644 --- a/apps/desktop/src/autofill/main/main-desktop-autotype.service.ts +++ b/apps/desktop/src/autofill/main/main-desktop-autotype.service.ts @@ -4,6 +4,7 @@ import { autotype } from "@bitwarden/desktop-napi"; import { LogService } from "@bitwarden/logging"; import { WindowMain } from "../../main/window.main"; +import { stringIsNotUndefinedNullAndEmpty } from "../../utils"; export class MainDesktopAutotypeService { keySequence: string = "Alt+CommandOrControl+I"; @@ -26,12 +27,8 @@ export class MainDesktopAutotypeService { const { response } = data; if ( - response.username != undefined && - response.username != null && - response.username.length > 0 && - response.password != undefined && - response.password != null && - response.password.length > 0 + stringIsNotUndefinedNullAndEmpty(response.username) && + stringIsNotUndefinedNullAndEmpty(response.password) ) { this.doAutotype(response.username, response.password); } @@ -62,10 +59,10 @@ export class MainDesktopAutotypeService { private doAutotype(username: string, password: string) { const inputPattern = username + "\t" + password; - const inputArray = new Array(); + const inputArray = new Array(inputPattern.length); for (let i = 0; i < inputPattern.length; i++) { - inputArray.push(inputPattern.charCodeAt(i)); + inputArray[i] = inputPattern.charCodeAt(i); } autotype.typeInput(inputArray); diff --git a/apps/desktop/src/utils.ts b/apps/desktop/src/utils.ts index c798faac36e..c15bb94eea9 100644 --- a/apps/desktop/src/utils.ts +++ b/apps/desktop/src/utils.ts @@ -98,3 +98,11 @@ export function cleanUserAgent(userAgent: string): string { .replace(userAgentItem("Bitwarden", " "), "") .replace(userAgentItem("Electron", " "), ""); } + +/** + * Returns `true` if the provided string is not undefined, not null, and not empty. + * Otherwise, returns `false`. + */ +export function stringIsNotUndefinedNullAndEmpty(str: string): boolean { + return str != undefined && str != null && str.length > 0; +}