Fortunately, there is code that already does this on Wakefield Morys-Carter’s Cribsheet: PsychoPy Python to Javascript crib sheet 2021 - Google Docs
This is the code, just pop it in the begin experiment section of a javascript only code component:
// Automatically exit if the experiment is running on a mobile device
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
quitPsychoJS('Mobile device detected. Goodbye!', false)
}