【问题标题】:Chrome Extension: Refused to load the script, because it violates the following Content Security Policy directive: "script-src 'self'"Chrome 扩展:拒绝加载脚本,因为它违反了以下内容安全策略指令:“script-src 'self'”
【发布时间】:2021-10-25 18:17:57
【问题描述】:

我正在尝试将游戏作为扩展程序添加到 Chrome 网上应用店,但我遇到了一些问题。游戏是用 Unity3D 制作的。

错误:

拒绝加载脚本“blob:chrome-extension://laacabdjcfgafjkeclplanjohdbpapgn/88f06275-2339-4218-a7c4-ab954cbaafdc”,因为它违反了以下内容安全策略指令:“script-src 'self'”。请注意,'script-src-elem' 没有显式设置,因此 'script-src' 用作备用。

触发错误的代码:

function f() {
        return d("frameworkUrl").then(function(e) {
            var t = URL.createObjectURL(new Blob([e], {
                type: "application/javascript"
            }));
            return new Promise(function(e, n) {
                var r = document.createElement("script");
                r.src = t, r.onload = function() {
                    var n = unityFramework;
                    unityFramework = null, r.onload = null, URL.revokeObjectURL(t), e(n)
                }, document.body.appendChild(r), c.deinitializers.push(function() {
                    document.body.removeChild(r)
                })
            })
        })
    }

确切的行:

 }, document.body.appendChild(r), c.deinitializers.push(function() {

图片:

该扩展正在使用 Manifest Version 3。没有使用外部脚本/资源。我尝试了几天来解决这个问题,但我无法做到。

有没有人尝试过这样的事情并可以帮助我?或者你有什么想法……我愿意接受。

谢谢! :)

【问题讨论】:

    标签: unity3d google-chrome-extension manifest content-security-policy manifest.json


    【解决方案1】:

    您的代码尝试将外部脚本t 添加到扩展页面。 出于安全原因,它在清单 V3 中在概念上是被禁止的。 实际上,CSP 指令script-src 'self' 禁止这样做。

    https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#content-security-policy

    您也不能在内容脚本中执行外部代码。

    https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#executing-arbitrary-strings

    如果要执行外部代码,可以在沙盒页面中执行。

    https://developer.chrome.com/docs/extensions/mv3/manifest/sandbox/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2016-04-29
      • 2013-07-13
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多