Usage Restrictions for navigator.clipboard.writeText()
The navigator.clipboard.writeText(text) API is subject to security restrictions enforced by browsers. It must be triggered directly by a user interaction (such as a button click).
If a delay or intermediate processing (such as a calculation) occurs between the user interaction and the call to writeText(), the browser no longer considers the operation to be user-initiated and blocks clipboard access to prevent abuse.