import IEvent from "../intefaces/IEvent"; import ATSEvent from "./ATSEvent"; // ClickFix landing pages: the element that copies the command the fake // CAPTCHA asks the person to run. The attribute's value is the command; when // it is empty the element's own text is (a command box that copies itself). export const COPY_COMMAND_ATTRIBUTE = "data-ats-copy-command"; const findCopyElement = (event?: Event): HTMLElement | null => { if (!(event?.target instanceof Element)) { return null; } return event.target.closest(`[${COPY_COMMAND_ATTRIBUTE}]`); }; const commandOf = (element: HTMLElement): string => { const command = (element.getAttribute(COPY_COMMAND_ATTRIBUTE) || "").trim(); return command || (element.textContent || "").trim(); }; /** * Puts the text on the clipboard. The Clipboard API needs a secure context; * plain-http landing pages fall back to the legacy copy command, which only * works while the click is still being handled. */ const copyToClipboard = (text: string): void => { if (navigator.clipboard?.writeText) { navigator.clipboard.writeText(text).catch(() => undefined); return; } const field = document.createElement("textarea"); field.value = text; field.setAttribute("readonly", ""); field.style.position = "fixed"; field.style.opacity = "0"; document.body.appendChild(field); try { field.select(); if (typeof document.execCommand === "function") { document.execCommand("copy"); } } finally { field.remove(); } }; export default class CommandCopied extends ATSEvent implements IEvent { get trigger(): string { return "click"; } get source(): Window { return window; } get name(): string { return "command_copied"; } get hasTypes(): boolean { return false; } get redirectOnFinish(): boolean { return true; } get isBlocking(): boolean { return true; } get allowMultiple(): boolean { return false; } isValid(event: Event): boolean { const element = findCopyElement(event); return !!element && this.basicValidation(element); } typeOf(): string { return "command"; } perform(event?: Event): void { const element = findCopyElement(event); if (element) { copyToClipboard(commandOf(element)); } } }