(win, extensionId, version)
| 126 | * @return {!Element} Script object |
| 127 | */ |
| 128 | export function createExtensionScript(win, extensionId, version) { |
| 129 | const scriptElement = win.document.createElement('script'); |
| 130 | scriptElement.async = true; |
| 131 | if (isIntermediateExtension(extensionId)) { |
| 132 | version = ''; |
| 133 | } else { |
| 134 | scriptElement.setAttribute( |
| 135 | CUSTOM_TEMPLATES.indexOf(extensionId) >= 0 |
| 136 | ? 'custom-template' |
| 137 | : 'custom-element', |
| 138 | extensionId |
| 139 | ); |
| 140 | } |
| 141 | scriptElement.setAttribute('data-script', extensionId); |
| 142 | scriptElement.setAttribute('i-amphtml-inserted', ''); |
| 143 | if (mode.isEsm()) { |
| 144 | scriptElement.setAttribute('type', 'module'); |
| 145 | } |
| 146 | propagateNonce(win.document, scriptElement); |
| 147 | |
| 148 | // Allow error information to be collected |
| 149 | // https://github.com/ampproject/amphtml/issues/7353 |
| 150 | scriptElement.setAttribute('crossorigin', 'anonymous'); |
| 151 | let loc = win.location; |
| 152 | if (getMode(win).test && win.testLocation) { |
| 153 | loc = win.testLocation; |
| 154 | } |
| 155 | const scriptSrc = calculateExtensionScriptUrl( |
| 156 | loc, |
| 157 | extensionId, |
| 158 | version, |
| 159 | getMode(win).localDev |
| 160 | ); |
| 161 | |
| 162 | let policy = { |
| 163 | createScriptURL: function (url) { |
| 164 | // Only allow trusted URLs |
| 165 | if ( |
| 166 | regexURL.test(url) || |
| 167 | ((getMode().test || getMode().localDev) && |
| 168 | testRegexURL.test(new URL(url).hostname)) || |
| 169 | new URL(url).host === 'fonts.googleapis.com' |
| 170 | ) { |
| 171 | return url; |
| 172 | } else { |
| 173 | return ''; |
| 174 | } |
| 175 | }, |
| 176 | }; |
| 177 | |
| 178 | if (self.trustedTypes && self.trustedTypes.createPolicy) { |
| 179 | policy = self.trustedTypes.createPolicy( |
| 180 | 'extension-script#createExtensionScript', |
| 181 | policy |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | scriptElement.src = policy.createScriptURL(scriptSrc); |
no test coverage detected