示例工程 22-security-service-worker

22-security-service-worker/03-csp/index.html

html · 0.6 KB

运行:22 · CSP 拦截内联脚本

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Security-Policy" content="script-src 'self'; object-src 'none'; base-uri 'none'">
  <title>22 · CSP 拦截内联脚本</title>
 
  <script>
    // 这段内联脚本会被 CSP 拦截,不会执行
    window.__inlineRan = true;
    console.log('内联脚本执行了(不应出现)');
  </script>
</head>
<body>
  <p>打开 Console:应看到 CSP 拦截错误,且 <code>__inlineRan</code> 未定义。</p>
  <script type="module" src="./page.js"></script>
</body>
</html>