NewsCrawler.org

GAS: getContent() escapes < in raw JS and breaks embedded libraries

GASで素のJSを埋め込むと < がエスケープされて動かない:getContent() の落とし穴と直し方
Image: zenn.dev

After the author embedded the QR-reading library jsQR into a Google Apps Script (GAS[1]) web app, iPhone showed ReferenceError: Can't find variable: jsQR — Safari's wording for this failure — and in the delivered HTML every `<` in the library had been replaced with the entity `&lt;`[2]. The cause is HtmlService.createHtmlOutputFromFile('jsQR').getContent(): raw JS without a `<script>` wrapper, loaded as an HTML file, comes back with `<` converted to `&lt;`, and injecting it into `<script><?!= jsqr ?></script>` sends broken JS to the browser. The fix is to wrap the library file itself in `<script>…</script>` and keep only `<?!= ?>` on the page — this scriptlet[3] outputs the value unescaped, while `<?= ?>` escapes it.

A minimal reproduction with a sumTo(n) function showed that `for (let i = 0; i < n; i++)` turns into `i &lt; n`, the for parentheses split into four parts, the script fails with a SyntaxError and never runs, so sumTo stays undefined and the page reports ReferenceError: sumTo is not defined. When `<` appears only outside the loop, as in `return a < b && b > 0;`, the code loads without a syntax error but throws ReferenceError: lt is not defined at call time. Comparing five extraction methods showed that getContent() escapes `<` whether the source is a file (createHtmlOutputFromFile), a string (createHtmlOutput) or a template's evaluate(), while `&&`, `>` and `>=` pass through untouched; createTemplateFromFile(...).getRawContent() returns the content unchanged. Wrapping the file in `<script>` prevents the escaping but strips both `//` comments and `/*! … */` license comments, so license notices need to live elsewhere; the pattern matches HTML parsing, where text outside `<script>` is treated as body text, though Google's documentation does not describe this behavior.

Fix 1 follows the official HTML Service: Best Practices, whose include() example puts the `<script>` tag in the loaded file — these are HTML snippets rather than standalone .css or .js files and must include `<style>` and `<script>` tags; the author applied it to a library of about 10,000 lines. Fix 2, getRawContent(), leaves the file untouched but breaks if the library contains the string `</script>`, which closes the tag early; the author prefers Fix 1 because the file then shows its intent at a glance. Switching `<?= ?>` to `<?!= ?>` does not help because the escaping already happened at getContent(); verification must use the delivered HTML, since a GAS web app renders inside an iframe and dev tools show the entity only in the iframe's source; testing used the V8 runtime, clasp[4] 3.4.1, a /dev test deployment and Chrome, checked on 2026/10/27.

Read the original zenn.dev

Sources

  • Zenn JA

    GASで素のJSを埋め込むと < がエスケープされて動かない:getContent() の落とし穴と直し方

    1 like0 bookmarks0 comments

Published: First seen: Читать по-русски

Open as a page