import { JsonRpcEngine } from 'json-rpc-engine' ; import messages from './messages' ; import { ConsoleLike , NOOP } from './utils' ; param engine param log export default async function sendSiteMetadata ( engine , log ) { try { const domainMetadata = await getSiteMetadata ( ) ; engine . handle ( { jsonrpc : '2.0' , id : 1 , method : 'metamask_sendDomainMetadata' , params : domainMetadata , } , NOOP ) ; } catch ( error ) { log . error ( { message : messages . errors . sendSiteMetadata ( ) , originalError : error , } ) ; } } /**
 * Gets site metadata and returns it
 *
 */ async function getSiteMetadata ( ) { return { name : getSiteName ( window ) , icon : await getSiteIcon ( window ) , } ; } /**
 * Extracts a name for the site from the DOM
 */ function getSiteName ( windowObject ) { const { document } = windowObject ; const siteName = document . querySelector ( 'head > meta[property="og:site_name"]' ) ; if ( siteName ) { return siteName . content ; } const metaTitle = document . querySelector ( 'head > meta[name="title"]' ) ; if ( metaTitle ) { return metaTitle . content ; } if ( document . title && document . title . length > 0 ) { return document . title ; } return window . location . hostname ; } returns async function getSiteIcon ( windowObject ) { const { document } = windowObject ; const icons = document . querySelectorAll ( 'head > link[rel~="icon"]' ) ; for ( const icon of icons ) { if ( icon && ( await imgExists ( icon . href ) ) ) { return icon . href ; } } return null ; } param url returns function imgExists ( url ) { return new Promise ( ( resolve , reject ) => { try { const img = document . createElement ( 'img' ) ; img . onload = ( ) => resolve ( true ) ; img . onerror = ( ) => resolve ( false ) ; img . src = url ; } catch ( e ) { reject ( e ) ; } } ) ; }