* Injects a script tag into the current document * * @param {string} content - Code to be executed in the current document
(content)
| 22 | * @param {string} content - Code to be executed in the current document |
| 23 | */ |
| 24 | function injectScript(content) { |
| 25 | try { |
| 26 | const container = document.head || document.documentElement; |
| 27 | |
| 28 | // synchronously execute script in page context |
| 29 | const scriptTag = document.createElement('script'); |
| 30 | scriptTag.setAttribute('async', false); |
| 31 | scriptTag.textContent = content; |
| 32 | container.insertBefore(scriptTag, container.children[0]); |
| 33 | |
| 34 | // script executed; remove script element from DOM |
| 35 | container.removeChild(scriptTag); |
| 36 | } catch (err) { |
| 37 | console.error('MetaMask script injection failed', err); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Determines if the provider should be injected. |