import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom"; import { Call, CallAction, Message, MessageEvent } from "./../common/types"; import { openPhonePopup } from "./../utils"; export const ContentApp = () => { const [selectedText, setSelectedText] = useState(""); const [counting, setCounting] = useState(0); const [position, setPosition] = useState({ x: 0, y: 0 }); useEffect(() => { function mouseUpListener(e: any) { const text = window.getSelection()?.toString().trim(); if (text && text.length > 0) { const number = validateAndFormatNumber(text); setSelectedText(number || ""); setPosition({ x: e.pageX, y: e.pageY }); } else { setSelectedText(""); } } document.addEventListener("mouseup", mouseUpListener); return () => { document.removeEventListener("mouseup", mouseUpListener); }; }, []); const validateAndFormatNumber = (input: string): string | null => { const strippedInput = input.replace(/\s+|\.|-|\+|\(|\)/g, ""); // remove all spaces // check if the final string contains only numbers if (/^\d+$/.test(strippedInput)) { return strippedInput; } else { return null; } }; const onClick = () => { setSelectedText(""); const msg: Message = { event: MessageEvent.Call, data: { action: CallAction.OUTBOUND, number: selectedText || "", }, }; chrome.runtime.sendMessage(msg); }; if (!selectedText) { return null; } return (

Number:{" "}

{selectedText}
); }; // Create a new div and attach it to the DOM const root = document.createElement("div"); document.body.appendChild(root); ReactDOM.render(, root);