首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内联脚本,因为它违反了以下内容安全策略指令:" script -src 'self'“

内联脚本,因为它违反了以下内容安全策略指令:" script -src 'self'“
EN

Stack Overflow用户
提问于 2019-10-12 12:53:11
回答 2查看 9.8K关注 0票数 8

我使用react-create-app构建我的铬扩展名。当我在react中使用npm run build时,我有错误:

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:" script -src 'self'“。要么是‘不安全-内联’关键字,要么是散列('sha256-5='),要么是“不安全-内联”关键字(‘nonce .’)需要启用内联执行。

index.html中的误差

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
      crossorigin="anonymous"
    />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

manifest.json

代码语言:javascript
复制
{
  "manifest_version": 2,
  "name": "IC Project chrome extension",
  "description": "This extension is a starting point to create a real Chrome extension",
  "version": "0.0.1",
  "browser_action": {
    "default_popup": "index.html",
    "default_title": "Open the popup"
  },
  "icons": {
    "16": "assets/icon-128.png",
    "48": "assets/icon-128.png",
    "128": "assets/icon-128.png"
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
EN

回答 2

Stack Overflow用户

发布于 2020-05-23 17:05:58

在经历了几个小时的挫折之后,我找到了一个可行的解决方案。Mac和PC在运行脚本方面存在差异。我发现的很多答案都与“设定”一致,而且没有。加上"&&“而没有..。没人为我工作。

TLDR:使用这样的“交叉env”npm包:

代码语言:javascript
复制
cross-env INLINE_RUNTIME_CHUNK=false react-scripts build

这在个人电脑上很可能也适用于Mac电脑。当然,您需要在此之前使用npm install --save-dev cross-env

票数 12
EN

Stack Overflow用户

发布于 2019-10-12 21:11:43

package.json中,将"build"脚本更新为:

代码语言:javascript
复制
"build": "INLINE_RUNTIME_CHUNK=false react-scripts build"
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58354362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档