* Resolves an AMPHTML URL from a canonical URL. If AMPHTML is canonical, same * URL is returned. * @param {string} urlSuffix URL without protocol or google.com/amp/s/... * @param {string=} protocol 'https' or 'http'. 'https' retries using 'http'. * @return {!Promise }
(urlSuffix, protocol = 'https')
| 195 | * @return {!Promise<string>} |
| 196 | */ |
| 197 | async function requestAmphtmlDocUrl(urlSuffix, protocol = 'https') { |
| 198 | const defaultUrl = `${protocol}://${urlSuffix}`; |
| 199 | logWithoutTimestamp(`Fetching URL: ${defaultUrl}`); |
| 200 | |
| 201 | const response = await fetch(defaultUrl); |
| 202 | if (!response.ok) { |
| 203 | if (protocol == 'https') { |
| 204 | return requestAmphtmlDocUrl(urlSuffix, 'http'); |
| 205 | } |
| 206 | throw new Error(`Status: ${response.status}`); |
| 207 | } |
| 208 | |
| 209 | const {window} = new jsdom.JSDOM(await response.text()); |
| 210 | const linkRelAmphtml = window.document.querySelector('link[rel=amphtml]'); |
| 211 | const amphtmlUrl = linkRelAmphtml && linkRelAmphtml.getAttribute('href'); |
| 212 | return amphtmlUrl || defaultUrl; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Intercept Recaptcha frame for, |
no test coverage detected