MCPcopy
hub / github.com/ampproject/amphtml / loadScript

Function loadScript

src/validator-integration.js:43–80  ·  view source on GitHub ↗
(doc, url)

Source from the content-addressed store, hash-verified

41 * @return {!Promise}
42 */
43export function loadScript(doc, url) {
44 const script = /** @type {!HTMLScriptElement} */ (
45 doc.createElement('script')
46 );
47 // Make script.src assignment Trusted Types compatible for compatible browsers
48 if (self.trustedTypes && self.trustedTypes.createPolicy) {
49 const policy = self.trustedTypes.createPolicy(
50 'validator-integration#loadScript',
51 {
52 createScriptURL: function (url) {
53 // Only allow trusted URLs
54 // Using explicit cdn domain as no other AMP Cache hosts validator_
55 // wasm so we can assume the explicit cdn domain is cdn.ampproject.org
56 // instead of using the dynamic cdn value from src/config/urls.js
57 // eslint-disable-next-line local/no-forbidden-terms
58 if (url === 'https://cdn.ampproject.org/v0/validator_wasm.js') {
59 return url;
60 } else {
61 return '';
62 }
63 },
64 }
65 );
66 script.src = policy.createScriptURL(url);
67 } else {
68 script.src = url;
69 }
70 propagateNonce(doc, script);
71
72 const promise = loadPromise(script).then(
73 () => {
74 doc.head.removeChild(script);
75 },
76 () => {}
77 );
78 doc.head.appendChild(script);
79 return promise;
80}

Callers 3

buildCallbackMethod · 0.90
maybeValidateFunction · 0.70

Calls 3

propagateNonceFunction · 0.90
loadPromiseFunction · 0.90
thenMethod · 0.45

Tested by

no test coverage detected