import { Recipient } from '@proton/shared/lib/interfaces' ; import { protonizer , sanitizeString } from '@proton/shared/lib/sanitize' ; import { Message } from '@proton/shared/lib/interfaces/mail/Message' ; import { MAILTO_PROTOCOL_HANDLER_PATH } from '../constants' ; import { PartialMessageState } from '../logic/messages/messagesTypes' ; param emailsStr export const toAddresses = ( emailsStr ) : Recipient [ ] => { const emails = sanitizeString ( emailsStr ) . split ( ',' ) ; return emails . map ( ( Address ) => ( { Address , Name : Address } ) ) ; } ; param Mailto return export const mailtoParser = ( mailto ) : PartialMessageState => { if ( mailto . toLowerCase ( ) . indexOf ( 'mailto:' ) !== 0 ) { return { } ; } let j = mailto . indexOf ( '?' ) ; if ( j < 0 ) { j = mailto . length ; } const to = sanitizeString ( mailto . substring ( 7 , j ) ) ; const url = new URL ( mailto ) ; const searchObject = { subject : url . searchParams . get ( 'subject' ) , cc : url . searchParams . get ( 'cc' ) , bcc : url . searchParams . get ( 'bcc' ) , body : url . searchParams . get ( 'body' ) , } ; const message = { } ; let decryptedBody ; if ( to ) { message . ToList = toAddresses ( to ) ; } if ( searchObject . subject ) { message . Subject = decodeURIComponent ( sanitizeString ( searchObject . subject ) ) ; } if ( searchObject . cc ) { message . CCList = toAddresses ( searchObject . cc ) ; } if ( searchObject . bcc ) { message . BCCList = toAddresses ( searchObject . bcc ) ; } if ( searchObject . body ) { decryptedBody = decodeURIComponent ( protonizer ( searchObject . body , true ) . innerHTML ) ; } return { data : message , decryption : { decryptedBody } } ; } ; export const registerMailToProtocolHandler = ( ) => { if ( 'registerProtocolHandler' in navigator ) { try { navigator . registerProtocolHandler ( 'mailto' , `${ window . location . origin }${ MAILTO_PROTOCOL_HANDLER_PATH }` , 'ProtonMail' ) ; } catch ( e ) { console . error ( e ) ; } } } ;