小编Wax*_*orp的帖子

加载 jQuery 等外部脚本时出现问题

从今天早上开始,我就遇到了一个问题,使用 Apps 脚本部署的 web 应用程序以前运行良好。当然,没有做出任何改变来证明这个问题的合理性。

外部脚本不会从 HTML 端加载,并且控制台中会出现新错误。

为了有一个可重现的例子:

代码.gs

function doGet() {

  var template = HtmlService.createTemplateFromFile('index');

  return template.evaluate()
                .setTitle('TEST')
                .addMetaTag('viewport', 'width=device-width, initial-scale=1')
                .setSandboxMode(HtmlService.SandboxMode.IFRAME)
}
Run Code Online (Sandbox Code Playgroud)

索引.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>
  <body>
        <div id="title">Hello</div>
  </body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

  <script>
    window.onload = function() {

      console.log('loaded');
      console.log($('#title').html());

    }
  </script>
</html>
Run Code Online (Sandbox Code Playgroud)

控制台中的结果:

[ERROR] This document requires 'TrustedHTML' assignment. (jquery.min.js:2)

[ERROR] Uncaught TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment. (jquery.min.js:2)

[LOG] loaded …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery google-apps-script google-workspace

17
推荐指数
2
解决办法
2443
查看次数