UNPKG

@metamask/providers

Version:

A JavaScript Ethereum provider that connects to the wallet over a stream

1 lines 4.99 kB
{"version":3,"file":"siteMetadata.mjs","sourceRoot":"","sources":["../src/siteMetadata.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,uBAAmB;AAElC,OAAO,EAAE,IAAI,EAAE,oBAAgB;AAE/B;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,MAAqB,EACrB,GAAgB;IAEhB,IAAI,CAAC;QACH,MAAM,cAAc,GAAG,MAAM,eAAe,EAAE,CAAC;QAC/C,mEAAmE;QACnE,MAAM,CAAC,MAAM,CACX;YACE,OAAO,EAAE,KAAK;YACd,EAAE,EAAE,CAAC;YACL,MAAM,EAAE,6BAA6B;YACrC,MAAM,EAAE,cAAc;SACvB,EACD,IAAI,CACL,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,KAAK,CAAC;YACR,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,gBAAgB,EAAE;YAC3C,aAAa,EAAE,KAAK;SACrB,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,eAAe;IAC5B,OAAO;QACL,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC;QACzB,IAAI,EAAE,MAAM,WAAW,CAAC,MAAM,CAAC;KAChC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,YAA2B;IAC9C,MAAM,EAAE,QAAQ,EAAE,GAAG,YAAY,CAAC;IAElC,MAAM,QAAQ,GAA2B,QAAQ,CAAC,aAAa,CAC7D,sCAAsC,CACvC,CAAC;IACF,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED,MAAM,SAAS,GAA2B,QAAQ,CAAC,aAAa,CAC9D,2BAA2B,CAC5B,CAAC;IACF,IAAI,SAAS,EAAE,CAAC;QACd,O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@metamask/json-rpc-engine';\n\nimport messages from './messages';\nimport type { ConsoleLike } from './utils';\nimport { NOOP } from './utils';\n\n/**\n * Sends site metadata over an RPC request.\n *\n * @param engine - The JSON RPC Engine to send metadata over.\n * @param log - The logging API to use.\n */\nexport async function sendSiteMetadata(\n engine: JsonRpcEngine,\n log: ConsoleLike,\n): Promise<void> {\n try {\n const domainMetadata = await getSiteMetadata();\n // call engine.handle directly to avoid normal RPC request handling\n engine.handle(\n {\n jsonrpc: '2.0',\n id: 1,\n method: 'metamask_sendDomainMetadata',\n params: domainMetadata,\n },\n NOOP,\n );\n } catch (error) {\n log.error({\n message: messages.errors.sendSiteMetadata(),\n originalError: error,\n });\n }\n}\n\n/**\n * Get site metadata.\n *\n * @returns The site metadata.\n */\nasync function getSiteMetadata() {\n return {\n name: getSiteName(window),\n icon: await getSiteIcon(window),\n };\n}\n\n/**\n * Extract a name for the site from the DOM.\n *\n * @param windowObject - The window object to extract the site name from.\n * @returns The site name.\n */\nfunction getSiteName(windowObject: typeof window): string {\n const { document } = windowObject;\n\n const siteName: HTMLMetaElement | null = document.querySelector(\n 'head > meta[property=\"og:site_name\"]',\n );\n if (siteName) {\n return siteName.content;\n }\n\n const metaTitle: HTMLMetaElement | null = document.querySelector(\n 'head > meta[name=\"title\"]',\n );\n if (metaTitle) {\n return metaTitle.content;\n }\n\n if (document.title && document.title.length > 0) {\n return document.title;\n }\n\n return window.location.hostname;\n}\n\n/**\n * Extract an icon for the site from the DOM.\n *\n * @param windowObject - The window object to extract the site icon from.\n * @returns An icon URL, if one exists.\n */\nasync function getSiteIcon(\n windowObject: typeof window,\n): Promise<string | null> {\n const { document } = windowObject;\n\n const icons: NodeListOf<HTMLLinkElement> = document.querySelectorAll(\n 'head > link[rel~=\"icon\"]',\n );\n for (const icon of Array.from(icons)) {\n if (icon && (await imgExists(icon.href))) {\n return icon.href;\n }\n }\n\n return null;\n}\n\n/**\n * Return whether the given image URL exists.\n *\n * @param url - The url of the image.\n * @returns Whether the image exists.\n */\nasync function imgExists(url: string): Promise<boolean> {\n return new Promise((resolve, reject) => {\n try {\n const img = document.createElement('img');\n img.onload = () => resolve(true);\n img.onerror = () => resolve(false);\n img.src = url;\n } catch (error) {\n reject(error);\n }\n });\n}\n"]}