【问题标题】:amp-script Script hash not found. amp-script[script="hello-world"]amp-script 未找到脚本哈希。放大器脚本[脚本=“你好世界”]
【发布时间】:2020-07-04 06:59:09
【问题描述】:

我正在尝试使用 amp 脚本,但出现此错误:

"[amp-script] 找不到脚本哈希。amp-script[script="hello-world"].js 必须在 meta[name="amp-script-src"] 中包含“sha384-BdjJFLYaZaVK-HgidJ2OFtpYsczYQc4N42NgKo7MOkF88iPbpdDWPgf86Y6PyEKO” . 见https://amp.dev/documentation/components/amp-script/#security-features。”

error image

<!doctype html>
<html amp lang="en">
<head>
    <meta charset="utf-8">
    <meta class="trackPerformanceTag" content="AMP">
    <script src="https://cdn.ampproject.org/v0.js" async></script>
    <script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
    <meta name="amp-script-src" content="sha384-generated-sha">
    <title>title</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

</head>
<body>

    <amp-script layout="container" script="hello-world" class="amp-script-sample">
        <button id="hello2">Click!</button>
    </amp-script>

    <script id="hello-world" type="text/plain" target="amp-script">
        const button = document.getElementById('hello2');
        button.addEventListener('click', () => {
            const h1 = document.createElement('h1');
            h1.textContent = 'Hello World 2!';
            document.body.appendChild(h1);
        });
    </script>

</body>
</html>

【问题讨论】:

  • 如果你用这个替换:&lt;meta name="amp-script-src" content="sha384-generated-sha"&gt; ... 怎么办? &lt;meta name="amp-script-src" content="sha384-BdjJFLYaZaVK-HgidJ2OFtpYsczYQc4N42NgKo7MOkF88iPbpdDWPgf86Y6PyEKO"&gt;

标签: javascript amp-html


【解决方案1】:

您需要在开发工具控制台中复制错误消息中显示的完整哈希。

在这种情况下;

sha384-BdjJFLYaZaVK-HgidJ2OFtpYsczYQc4N42NgKo7MOkF88iPbpdDWPgf86Y6PyEKO

并将其粘贴到标题中的元:

<meta name="amp-script-src" content="PUT_THE_SHA_HERE">

每次您更改脚本中的某些内容时,都会生成新的哈希值,您需要再次复制/粘贴它。

See the screen with error form the dev tools console

【讨论】:

  • 每次复制/粘贴是否有其他解决方法?
  • 真的吗?每次生病都要做吗?
  • 并非总是如此,我们可以在项目结束前使用模式开发。
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 2016-12-13
  • 2013-11-17
相关资源
最近更新 更多