|
| 1 | +/** |
| 2 | + * This source file is part of the Swift.org open source project |
| 3 | + * |
| 4 | + * Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | + * Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + * |
| 7 | + * See https://swift.org/LICENSE.txt for license information |
| 8 | + * See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import fetchText from 'docc-render/utils/fetch-text'; |
| 12 | +import { |
| 13 | + copyPresentProperties, |
| 14 | + copyPropertyIfPresent, |
| 15 | + has, |
| 16 | + mustNotHave, |
| 17 | +} from 'docc-render/utils/object-properties'; |
| 18 | +import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Returns whether the custom script should be run when the reader navigates to a subpage. |
| 22 | + * @param {object} customScript |
| 23 | + * @returns {boolean} Returns whether the custom script has a `run` property with a value of |
| 24 | + * "on-load" or "on-load-and-navigate". Also returns true if the `run` property is absent. |
| 25 | + */ |
| 26 | +function shouldRunOnPageLoad(customScript) { |
| 27 | + return !has(customScript, 'run') |
| 28 | + || customScript.run === 'on-load' || customScript.run === 'on-load-and-navigate'; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns whether the custom script should be run when the reader navigates to a topic. |
| 33 | + * @param {object} customScript |
| 34 | + * @returns {boolean} Returns whether the custom script has a `run` property with a value of |
| 35 | + * "on-navigate" or "on-load-and-navigate". |
| 36 | + */ |
| 37 | +function shouldRunOnNavigate(customScript) { |
| 38 | + return has(customScript, 'run') |
| 39 | + && (customScript.run === 'on-navigate' || customScript.run === 'on-load-and-navigate'); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Gets the URL for a local custom script given its name. |
| 44 | + * @param {string} customScriptName The name of the custom script as spelled in |
| 45 | + * custom-scripts.json. While the actual filename (in the custom-scripts directory) is always |
| 46 | + * expected to end in ".js", the name in custom-scripts.json may or may not include the ".js" |
| 47 | + * extension. |
| 48 | + * @returns {string} The absolute URL where the script is, accounting for baseURL. |
| 49 | + * @example |
| 50 | + * // if baseURL if '/foo' |
| 51 | + * urlGivenScriptName('hello-world') // http://localhost:8080/foo/hello-world.js |
| 52 | + * urlGivenScriptName('hello-world.js') // http://localhost:8080/foo/hello-world.js |
| 53 | + */ |
| 54 | +function urlGivenScriptName(customScriptName) { |
| 55 | + let scriptNameWithExtension = customScriptName; |
| 56 | + |
| 57 | + // If the provided name does not already include the ".js" extension, add it. |
| 58 | + if (customScriptName.slice(-3) !== '.js') { |
| 59 | + scriptNameWithExtension = `${customScriptName}.js`; |
| 60 | + } |
| 61 | + |
| 62 | + return resolveAbsoluteUrl(['', 'custom-scripts', scriptNameWithExtension]); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Add an HTMLScriptElement containing the custom script to the document's head, which runs the |
| 67 | + * script on page load. |
| 68 | + * @param {object} customScript The custom script, assuming it should be run on page load. |
| 69 | + */ |
| 70 | +function addScriptElement(customScript) { |
| 71 | + const scriptElement = document.createElement('script'); |
| 72 | + |
| 73 | + copyPropertyIfPresent('type', customScript, scriptElement); |
| 74 | + |
| 75 | + if (has(customScript, 'url')) { |
| 76 | + mustNotHave(customScript, 'name', 'Custom script cannot have both `url` and `name`.'); |
| 77 | + mustNotHave(customScript, 'code', 'Custom script cannot have both `url` and `code`.'); |
| 78 | + |
| 79 | + scriptElement.src = customScript.url; |
| 80 | + |
| 81 | + copyPresentProperties(['async', 'defer', 'integrity'], customScript, scriptElement); |
| 82 | + |
| 83 | + // If `integrity` is set on an external script, then CORS must be enabled as well. |
| 84 | + if (has(customScript, 'integrity')) { |
| 85 | + scriptElement.crossOrigin = 'anonymous'; |
| 86 | + } |
| 87 | + } else if (has(customScript, 'name')) { |
| 88 | + mustNotHave(customScript, 'code', 'Custom script cannot have both `name` and `code`.'); |
| 89 | + |
| 90 | + scriptElement.src = urlGivenScriptName(customScript.name); |
| 91 | + |
| 92 | + copyPresentProperties(['async', 'defer', 'integrity'], customScript, scriptElement); |
| 93 | + } else if (has(customScript, 'code')) { |
| 94 | + mustNotHave(customScript, 'async', 'Inline script cannot be `async`.'); |
| 95 | + mustNotHave(customScript, 'defer', 'Inline script cannot have `defer`.'); |
| 96 | + mustNotHave(customScript, 'integrity', 'Inline script cannot have `integrity`.'); |
| 97 | + |
| 98 | + scriptElement.innerHTML = customScript.code; |
| 99 | + } else { |
| 100 | + throw new Error('Custom script does not have `url`, `name`, or `code` properties.'); |
| 101 | + } |
| 102 | + |
| 103 | + document.head.appendChild(scriptElement); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Run the custom script using `eval`. Useful for running a custom script anytime after page load, |
| 108 | + * namely when the reader navigates to a subpage. |
| 109 | + * @param {object} customScript The custom script, assuming it should be run on navigate. |
| 110 | + */ |
| 111 | +async function evalScript(customScript) { |
| 112 | + let codeToEval; |
| 113 | + |
| 114 | + if (has(customScript, 'url')) { |
| 115 | + mustNotHave(customScript, 'name', 'Custom script cannot have both `url` and `name`.'); |
| 116 | + mustNotHave(customScript, 'code', 'Custom script cannot have both `url` and `code`.'); |
| 117 | + |
| 118 | + if (has(customScript, 'integrity')) { |
| 119 | + // External script with integrity. Must also use CORS. |
| 120 | + codeToEval = await fetchText(customScript.url, { |
| 121 | + integrity: customScript.integrity, |
| 122 | + crossOrigin: 'anonymous', |
| 123 | + }); |
| 124 | + } else { |
| 125 | + // External script without integrity. |
| 126 | + codeToEval = await fetchText(customScript.url); |
| 127 | + } |
| 128 | + } else if (has(customScript, 'name')) { |
| 129 | + mustNotHave(customScript, 'code', 'Custom script cannot have both `name` and `code`.'); |
| 130 | + |
| 131 | + const url = urlGivenScriptName(customScript.name); |
| 132 | + |
| 133 | + if (has(customScript, 'integrity')) { |
| 134 | + // Local script with integrity. Do not use CORS. |
| 135 | + codeToEval = await fetchText(url, { integrity: customScript.integrity }); |
| 136 | + } else { |
| 137 | + // Local script without integrity. |
| 138 | + codeToEval = await fetchText(url); |
| 139 | + } |
| 140 | + } else if (has(customScript, 'code')) { |
| 141 | + mustNotHave(customScript, 'async', 'Inline script cannot be `async`.'); |
| 142 | + mustNotHave(customScript, 'defer', 'Inline script cannot have `defer`.'); |
| 143 | + mustNotHave(customScript, 'integrity', 'Inline script cannot have `integrity`.'); |
| 144 | + |
| 145 | + codeToEval = customScript.code; |
| 146 | + } else { |
| 147 | + throw new Error('Custom script does not have `url`, `name`, or `code` properties.'); |
| 148 | + } |
| 149 | + |
| 150 | + // eslint-disable-next-line no-eval |
| 151 | + eval(codeToEval); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Run all custom scripts that pass the `predicate` using the `executor`. |
| 156 | + * @param {(customScript: object) => boolean} predicate |
| 157 | + * @param {(customScript: object) => void} executor |
| 158 | + * @returns {Promise<void>} |
| 159 | + */ |
| 160 | +async function runCustomScripts(predicate, executor) { |
| 161 | + const customScriptsFileName = 'custom-scripts.json'; |
| 162 | + const url = resolveAbsoluteUrl(`/${customScriptsFileName}`); |
| 163 | + |
| 164 | + const response = await fetch(url); |
| 165 | + if (!response.ok) { |
| 166 | + // If the file is absent, fail silently. |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + const customScripts = await response.json(); |
| 171 | + if (!Array.isArray(customScripts)) { |
| 172 | + throw new Error(`Content of ${customScriptsFileName} should be an array.`); |
| 173 | + } |
| 174 | + |
| 175 | + customScripts.filter(predicate).forEach(executor); |
| 176 | +} |
| 177 | + |
| 178 | +export async function runCustomPageLoadScripts() { |
| 179 | + await runCustomScripts(shouldRunOnPageLoad, addScriptElement); |
| 180 | +} |
| 181 | + |
| 182 | +export async function runCustomNavigateScripts() { |
| 183 | + await runCustomScripts(shouldRunOnNavigate, evalScript); |
| 184 | +} |
0 commit comments