From 0eb064df537c94e9f9e7119b05a94bae72f6cd9d Mon Sep 17 00:00:00 2001 From: Anush008 Date: Thu, 27 Apr 2023 22:39:03 +0530 Subject: [PATCH] refactor: Refactored inviteToOpenSauced.ts to avoid "as" and "!" --- src/utils/dom-utils/inviteToOpenSauced.ts | 33 ++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/utils/dom-utils/inviteToOpenSauced.ts b/src/utils/dom-utils/inviteToOpenSauced.ts index 10a8f268..e372fbd2 100644 --- a/src/utils/dom-utils/inviteToOpenSauced.ts +++ b/src/utils/dom-utils/inviteToOpenSauced.ts @@ -4,15 +4,30 @@ import { InviteToOpenSaucedModal } from "../../content-scripts/components/Invite import { getTwitterUsername, getLinkedInUsername } from "../urlMatchers"; const injectOpenSaucedInviteButton = (username: string) => { - const emailAddress: string | undefined = ( - document.querySelector(`a[href^="mailto:"]`)! - ).href.substr(7); - const twitterUrl: string | undefined = ( - document.querySelector(`a[href*="twitter.com"]`)! - ).href; - const linkedInUrl: string | undefined = ( - document.querySelector(`a[href*="linkedin.com"]`)! - ).href; + const emailAddress: string | undefined = (() => { + const element = document.querySelector(`a[href^="mailto:"]`); + + if (element instanceof HTMLAnchorElement) { + return element.href; + } + return undefined; + })(); + const twitterUrl: string | undefined = (() => { + const element = document.querySelector(`a[href*="twitter.com"]`); + + if (element instanceof HTMLAnchorElement) { + return element.href; + } + return undefined; + })(); + const linkedInUrl: string | undefined = (() => { + const element = document.querySelector(`a[href*="linkedin.com"]`); + + if (element instanceof HTMLAnchorElement) { + return element.href; + } + return undefined; + })(); if (!(emailAddress || twitterUrl || linkedInUrl)) { return;